Skip to content

Commit 0478e4a

Browse files
authored
Merge pull request #475 from alcaeus/odm-2.0-support
Prepare bundle for ODM 2.0
2 parents cae4426 + c18f591 commit 0478e4a

File tree

57 files changed

+468
-1288
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+468
-1288
lines changed

.travis.yml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ language: php
22
sudo: false
33

44
php:
5-
- 5.6
6-
- 7.0
7-
- 7.1
85
- 7.2
96

107
env:
118
global:
129
- DRIVER_VERSION="stable"
13-
- ADAPTER_VERSION="^1.0.0"
1410
- COMPOSER_FLAGS="--prefer-dist"
1511

1612
matrix:
1713
include:
18-
- php: 5.6
19-
env: DRIVER_VERSION="1.5.8" COMPOSER_FLAGS="--prefer-dist --prefer-lowest" PHPUNIT_FLAGS="--coverage-clover=coverage.clover"
14+
- php: 7.2
15+
env: DRIVER_VERSION="stable" COMPOSER_FLAGS="--prefer-dist"
16+
- php: 7.2
17+
env: DRIVER_VERSION="1.5.0" COMPOSER_FLAGS="--prefer-dist --prefer-lowest" PHPUNIT_FLAGS="--coverage-clover=coverage.clover"
2018

2119
cache:
2220
directories:
@@ -26,9 +24,7 @@ services: mongodb
2624

2725
install:
2826
- composer self-update
29-
- if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." ]]; then yes '' | pecl -q install -f mongo-${DRIVER_VERSION}; fi
30-
- if [[ ${TRAVIS_PHP_VERSION:0:2} == "7." ]]; then pecl install -f mongodb-${DRIVER_VERSION}; fi
31-
- if [[ ${TRAVIS_PHP_VERSION:0:2} == "7." ]]; then composer config "platform.ext-mongo" "1.6.16" && composer require "alcaeus/mongo-php-adapter=${ADAPTER_VERSION}"; fi
27+
- pecl install -f mongodb-${DRIVER_VERSION}
3228
- composer update ${COMPOSER_FLAGS}
3329

3430
script:

APM/PSRCommandLogger.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Doctrine\Bundle\MongoDBBundle\APM;
6+
7+
use MongoDB\Driver\Monitoring\CommandFailedEvent;
8+
use MongoDB\Driver\Monitoring\CommandStartedEvent;
9+
use MongoDB\Driver\Monitoring\CommandSubscriber;
10+
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
11+
use Psr\Log\LoggerInterface;
12+
use function json_encode;
13+
use function MongoDB\Driver\Monitoring\addSubscriber;
14+
use function MongoDB\Driver\Monitoring\removeSubscriber;
15+
16+
final class PSRCommandLogger implements CommandSubscriber
17+
{
18+
/** @var bool */
19+
private $registered = false;
20+
21+
/** @var LoggerInterface|null */
22+
private $logger;
23+
24+
/** @var string */
25+
private $prefix;
26+
27+
public function __construct(?LoggerInterface $logger, string $prefix = 'MongoDB command: ')
28+
{
29+
$this->logger = $logger;
30+
$this->prefix = $prefix;
31+
}
32+
33+
public function register(): void
34+
{
35+
if ($this->logger === null || $this->registered) {
36+
return;
37+
}
38+
39+
$this->registered = true;
40+
addSubscriber($this);
41+
}
42+
43+
public function unregister(): void
44+
{
45+
if (! $this->registered) {
46+
return;
47+
}
48+
49+
removeSubscriber($this);
50+
$this->registered = false;
51+
}
52+
53+
public function commandStarted(CommandStartedEvent $event)
54+
{
55+
if (!$this->logger) {
56+
return;
57+
}
58+
59+
$this->logger->debug($this->prefix . json_encode($event->getCommand()));
60+
}
61+
62+
public function commandSucceeded(CommandSucceededEvent $event)
63+
{
64+
}
65+
66+
public function commandFailed(CommandFailedEvent $event)
67+
{
68+
}
69+
}

Command/InfoDoctrineODMCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5252
'You do not have any mapped Doctrine MongoDB ODM documents for any of your bundles. '.
5353
'Create a class inside the Document namespace of any of your bundles and provide '.
5454
'mapping information for it with Annotations directly in the classes doc blocks '.
55-
'or with XML/YAML in your bundles Resources/config/doctrine/metadata/mongodb directory.'
55+
'or with XML in your bundles Resources/config/doctrine/metadata/mongodb directory.'
5656
);
5757
}
5858

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Doctrine\Bundle\MongoDBBundle\DataCollector;
4+
5+
use Doctrine\ODM\MongoDB\APM\Command;
6+
use Doctrine\ODM\MongoDB\APM\CommandLogger;
7+
use Symfony\Component\HttpFoundation\Request;
8+
use Symfony\Component\HttpFoundation\Response;
9+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
10+
11+
class CommandDataCollector extends DataCollector
12+
{
13+
private $commandLogger;
14+
15+
public function __construct(CommandLogger $commandLogger)
16+
{
17+
$this->commandLogger = $commandLogger;
18+
}
19+
20+
public function collect(Request $request, Response $response, \Exception $exception = null)
21+
{
22+
$this->data = [
23+
'num_commands' => count($this->commandLogger),
24+
'commands' => array_map(
25+
function (Command $command): string {
26+
return json_encode($command->getCommand());
27+
},
28+
$this->commandLogger->getAll()
29+
),
30+
];
31+
}
32+
33+
public function reset()
34+
{
35+
$this->commandLogger->clear();
36+
$this->data = [
37+
'num_commands' => 0,
38+
'commands' => [],
39+
];
40+
}
41+
42+
public function getCommandCount(): int
43+
{
44+
return $this->data['num_commands'];
45+
}
46+
47+
/**
48+
* @return string[]
49+
*/
50+
public function getCommands(): array
51+
{
52+
return $this->data['commands'];
53+
}
54+
55+
public function getName()
56+
{
57+
return 'mongodb';
58+
}
59+
}

0 commit comments

Comments
 (0)