Skip to content

Commit

Permalink
- added explain services
Browse files Browse the repository at this point in the history
- added explain to profiler
- some refactoring
- redefined some bundle requirements, extension version
- custom mongodb install in travis ci
  • Loading branch information
Alessandro Galli committed Aug 30, 2017
1 parent 7860dc6 commit 597f804
Show file tree
Hide file tree
Showing 28 changed files with 868 additions and 166 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ cache:
directories:
- $HOME/.composer/cache/files

services:
- mongodb

matrix:
fast_finish: true
include:
Expand Down Expand Up @@ -50,13 +47,18 @@ before_install:
- if [ "$SYMFONY" != "" ]; then composer require "symfony/symfony:${SYMFONY}" --no-update; fi;

install:
- wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.14.tgz
- tar xzf mongodb-linux-x86_64-3.0.14.tgz
- ${PWD}/mongodb-linux-x86_64-3.0.14/bin/mongod --version
- if [ "$MONGO_EXT_VERSION" != "" ]; then pecl install -f mongodb-${MONGO_EXT_VERSION}; fi;
- if [ "$MONGO_EXT_VERSION" = "" ]; then echo "extension=mongodb.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;
- mkdir --parents "${HOME}/bin"
- composer update --prefer-dist --prefer-stable --no-interaction ${COMPOSER_FLAGS}

before_script:
- echo "zend_extension=xdebug.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- mkdir ${PWD}/mongodb-linux-x86_64-3.0.14/data
- ${PWD}/mongodb-linux-x86_64-3.0.14/bin/mongod --dbpath ${PWD}/mongodb-linux-x86_64-3.0.14/data --logpath ${PWD}/mongodb-linux-x86_64-3.0.14/mongodb.log --fork

script:
- composer validate
Expand Down
4 changes: 3 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

Bundle service integration of official [mongodb/mongo-php-library](https://github.com/mongodb/mongo-php-library) driver library, ([mongodb/mongodb](https://packagist.org/packages/mongodb/mongodb) on packagist)

[![PHP Version](https://img.shields.io/badge/php-%5E7.0-blue.svg)](https://img.shields.io/badge/php-%5E7.0-blue.svg)
[![PHP](https://img.shields.io/badge/php-%5E7.0-blue.svg)](https://img.shields.io/badge/php-%5E7.0-blue.svg)
[![MongoDB](https://img.shields.io/badge/MongoDB-%5E3.0-lightgrey.svg)](https://img.shields.io/badge/MongoDB-%5E3.0-lightgrey.svg)
[![ext-mongodb](https://img.shields.io/badge/ext_mongodb-%5E1.1.5-orange.svg)](https://img.shields.io/badge/ext_mongodb-%5E1.1.5-orange.svg)
[![Latest Stable Version](https://poser.pugx.org/facile-it/mongodb-bundle/v/stable)](https://packagist.org/packages/facile-it/mongodb-bundle)
[![Latest Unstable Version](https://poser.pugx.org/facile-it/mongodb-bundle/v/unstable)](https://packagist.org/packages/facile-it/mongodb-bundle) [![Total Downloads](https://poser.pugx.org/facile-it/mongodb-bundle/downloads)](https://packagist.org/packages/facile-it/mongodb-bundle)
[![License](https://poser.pugx.org/facile-it/mongodb-bundle/license)](https://packagist.org/packages/facile-it/mongodb-bundle)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"require": {
"php" : "^7.0",
"ext-mongodb": "^1.1.0",
"ext-mongodb": "^1.1.5",
"mongodb/mongodb": "^1.0",
"symfony/framework-bundle": "^2.8 || ^3.0"
},
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ services:
container_name: mb_php
mongo:
image: mongo:3.4.2
ports:
- 27017:27017
entrypoint:
- mongod
- --quiet
Expand Down
20 changes: 15 additions & 5 deletions src/Capsule/Client.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);

namespace Facile\MongoDbBundle\Capsule;

Expand All @@ -13,22 +13,32 @@ final class Client extends MongoClient
{
/** @var EventDispatcherInterface */
private $eventDispatcher;
/** @var string */
private $clientName;

/**
* Client constructor.
*
* @param string $uri
* @param array $uriOptions
* @param array $driverOptions
* @param string $clientName
* @param EventDispatcherInterface $eventDispatcher
*
* @internal param DataCollectorLoggerInterface $logger
*/
public function __construct($uri = 'mongodb://localhost:27017', array $uriOptions = [], array $driverOptions = [], EventDispatcherInterface $eventDispatcher)
{
public function __construct(
$uri = 'mongodb://localhost:27017',
array $uriOptions = [],
array $driverOptions = [],
string $clientName,
EventDispatcherInterface $eventDispatcher
) {
parent::__construct($uri, $uriOptions, $driverOptions);
$this->eventDispatcher = $eventDispatcher;
$this->clientName = $clientName;
}

/**
* {@inheritdoc}
*/
Expand All @@ -39,7 +49,7 @@ public function selectDatabase($databaseName, array $options = [])
'typeMap' => $debug['typeMap'],
];

return new Database($debug['manager'], $databaseName, $options, $this->eventDispatcher);
return new Database($debug['manager'], $this->clientName, $databaseName, $options, $this->eventDispatcher);
}

/**
Expand All @@ -52,6 +62,6 @@ public function selectCollection($databaseName, $collectionName, array $options
'typeMap' => $debug['typeMap'],
];

return new Collection($debug['manager'], $databaseName, $collectionName, $options, $this->eventDispatcher);
return new Collection($debug['manager'], $this->clientName, $databaseName, $collectionName, $options, $this->eventDispatcher);
}
}
37 changes: 34 additions & 3 deletions src/Capsule/Collection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);

namespace Facile\MongoDbBundle\Capsule;

Expand All @@ -17,22 +17,35 @@ final class Collection extends MongoCollection
{
/** @var EventDispatcherInterface */
private $eventDispatcher;
/** @var string */
private $clientName;
/** @var string */
private $databaseName;

/**
* Collection constructor.
*
* @param Manager $manager
* @param string $clientName
* @param string $databaseName
* @param string $collectionName
* @param array $options
* @param EventDispatcherInterface $eventDispatcher
*
* @internal param DataCollectorLoggerInterface $logger
*/
public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [], EventDispatcherInterface $eventDispatcher)
{
public function __construct(
Manager $manager,
string $clientName,
string $databaseName,
string $collectionName,
array $options = [],
EventDispatcherInterface $eventDispatcher
) {
parent::__construct($manager, $databaseName, $collectionName, $options);
$this->eventDispatcher = $eventDispatcher;
$this->clientName = $clientName;
$this->databaseName = $databaseName;
}

/**
Expand Down Expand Up @@ -194,6 +207,8 @@ private function prepareQuery(string $method, $filters = null, $data = null, arr
$query->setData($data);
$query->setOptions($options);
$query->setMethod($method);
$query->setClient($this->getClientName());
$query->setDatabase($this->getDatabaseName());
$query->setCollection($this->getCollectionName());
$query->setReadPreference(
$this->translateReadPreference($options['readPreference'] ?? $this->__debugInfo()['readPreference'])
Expand Down Expand Up @@ -238,5 +253,21 @@ private function notifyQueryExecution(Query $queryLog)

$this->eventDispatcher->dispatch(QueryEvent::QUERY_EXECUTED, new QueryEvent($queryLog));
}

/**
* @return string
*/
public function getClientName(): string
{
return $this->clientName;
}

/**
* @return string
*/
public function getDatabaseName(): string
{
return $this->databaseName;
}
}

29 changes: 24 additions & 5 deletions src/Capsule/Database.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);

namespace Facile\MongoDbBundle\Capsule;

Expand All @@ -14,21 +14,33 @@ final class Database extends MongoDatabase
{
/** @var EventDispatcherInterface */
private $eventDispatcher;
/** @var string */
private $clientName;
/** @var string */
private $databaseName;

/**
* Database constructor.
*
* @param Manager $manager
* @param string $clientName
* @param string $databaseName
* @param array $options
* @param EventDispatcherInterface $eventDispatcher
*
* @internal param DataCollectorLoggerInterface $logger
*/
public function __construct(Manager $manager, $databaseName, array $options = [], EventDispatcherInterface $eventDispatcher)
{
public function __construct(
Manager $manager,
string $clientName,
string $databaseName,
array $options = [],
EventDispatcherInterface $eventDispatcher
) {
parent::__construct($manager, $databaseName, $options);
$this->eventDispatcher = $eventDispatcher;
$this->clientName = $clientName;
$this->databaseName = $databaseName;
}

/**
Expand All @@ -44,7 +56,14 @@ public function selectCollection($collectionName, array $options = [])
'writeConcern' => $debug['writeConcern'],
];

return new Collection($debug['manager'], $debug['databaseName'], $collectionName, $options, $this->eventDispatcher);
return new Collection(
$debug['manager'],
$this->clientName,
$this->databaseName,
$collectionName,
$options,
$this->eventDispatcher
);
}

/**
Expand All @@ -60,6 +79,6 @@ public function withOptions(array $options = [])
'writeConcern' => $debug['writeConcern'],
];

return new self($debug['manager'], $debug['databaseName'], $options, $this->eventDispatcher);
return new self($debug['manager'], $this->clientName, $debug['databaseName'], $options, $this->eventDispatcher);
}
}
77 changes: 77 additions & 0 deletions src/Controller/ProfilerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php declare(strict_types=1);

namespace Facile\MongoDbBundle\Controller;

use Facile\MongoDbBundle\DataCollector\MongoQuerySerializer;
use MongoDB\BSON\UTCDateTime;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;

class ProfilerController implements ContainerAwareInterface
{
/** @var Container */
private $container;

/**
* Sets the container.
*
* @param ContainerInterface|null $container A ContainerInterface instance or null
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

public function explainAction($token, $queryNumber)
{
/** @var $profiler \Symfony\Component\HttpKernel\Profiler\Profiler */
$profiler = $this->container->get('profiler');
$profiler->disable();

$profile = $profiler->loadProfile($token);
$queries = $profile->getCollector('mongodb')->getQueries();

$query = $queries[$queryNumber];

$query->setFilters($this->walkAndConvertToUTCDatetime($query->getFilters()));

$service = $this->container->get('mongo.explain_query_service');

try{
$result = $service->execute($query);
} catch (\InvalidArgumentException $e) {
return new JsonResponse([
"err" => $e->getMessage()
]);
}

return new JsonResponse(MongoQuerySerializer::prepareItemData($result->toArray()));
}

/**
* @param array $data
*
* @return array
*/
private function walkAndConvertToUTCDatetime($data)
{
if (!is_array($data)) {
return $data;
}

foreach ($data as $key => $item) {

if (is_string($item) && preg_match('/^ISODate/', $item)) {
$time = str_replace(['ISODate("','")'], '', $item);
$dateTime = new \DateTime($time);
$item = new UTCDatetime($dateTime->getTimestamp() * 1000);
}

$data[$key] = $this->walkAndConvertToUTCDatetime($item);
}

return $data;
}
}
2 changes: 1 addition & 1 deletion src/DataCollector/MongoDbDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Class MongoDbDataCollector.
* @internal
*/
final class MongoDbDataCollector extends DataCollector
class MongoDbDataCollector extends DataCollector
{
const QUERY_KEYWORD = 'queries';
const CONNECTION_KEYWORD = 'connections';
Expand Down
2 changes: 1 addition & 1 deletion src/DataCollector/MongoQuerySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static function prepareUnserializableData($data)
*
* @return mixed
*/
private static function prepareItemData($item)
public static function prepareItemData($item)
{
if (method_exists($item, 'getArrayCopy')) {
return self::prepareUnserializableData($item->getArrayCopy());
Expand Down
14 changes: 14 additions & 0 deletions src/DependencyInjection/MongoDbBundleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Facile\MongoDbBundle\Event\QueryEvent;
use Facile\MongoDbBundle\Services\ClientRegistry;
use Facile\MongoDbBundle\Services\ConnectionFactory;
use Facile\MongoDbBundle\Services\Explain\ExplainQueryService;
use Facile\MongoDbBundle\Services\Loggers\MongoQueryLogger;
use Facile\MongoDbBundle\Twig\FacileMongoDbBundleExtension;
use MongoDB\Database;
Expand Down Expand Up @@ -47,8 +48,10 @@ public function load(array $configs, ContainerBuilder $container)
$this->attachDataCollectionListenerToEventManager();
$this->defineDataCollector();
$this->attachTwigExtesion();
$this->defineExplainQueryService();
}


return $config;
}

Expand Down Expand Up @@ -182,4 +185,15 @@ private function attachTwigExtesion()

$this->containerBuilder->setDefinition('facile_mongo_db.twig_extesion', $extesion);
}

private function defineExplainQueryService()
{
$explainServiceDefinition = new Definition(
ExplainQueryService::class,
[new Reference('mongo.client_registry')]
);
$explainServiceDefinition->setPublic(true);

$this->containerBuilder->setDefinition('mongo.explain_query_service', $explainServiceDefinition);
}
}
Loading

0 comments on commit 597f804

Please sign in to comment.