Skip to content

Commit

Permalink
Merge pull request #13 from facile-it/prod_env_clients_without_logger
Browse files Browse the repository at this point in the history
Refactored Client chain build
  • Loading branch information
Algatux authored Sep 9, 2016
2 parents df4770b + 39fbcd1 commit 81bf625
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 162 deletions.
12 changes: 7 additions & 5 deletions src/DependencyInjection/MongoDbBundleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Facile\MongoDbBundle\Services\ClientRegistry;
use Facile\MongoDbBundle\Services\Loggers\DataCollectorLoggerInterface;
use Facile\MongoDbBundle\Services\Loggers\MongoLogger;
use MongoDB\Database;
use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -37,28 +38,29 @@ public function load(array $configs, ContainerBuilder $container)
}

$this->defineLoggers();
$this->defineClientRegistry($config['clients']);
$this->defineClientRegistry($config['clients'], $container->getParameter("kernel.environment"));
$this->defineConnections($config['connections']);

return $config;
}

private function defineLoggers()
{
$loggerDefinition = new Definition(DataCollectorLoggerInterface::class);
$loggerDefinition->setFactory([new Reference('mongo.logger_factory'), 'createLogger']);
$loggerDefinition = new Definition(MongoLogger::class);
$this->containerBuilder->setDefinition('facile_mongo_db.logger', $loggerDefinition);
}

/**
* @param array $clientsConfig
* @param array $clientsConfig
* @param string $environment
*/
private function defineClientRegistry(array $clientsConfig)
private function defineClientRegistry(array $clientsConfig, string $environment)
{
$clientRegistryDefinition = new Definition(
ClientRegistry::class,
[
new Reference('facile_mongo_db.logger'),
$environment,
]
);
foreach ($clientsConfig as $name => $conf) {
Expand Down
6 changes: 0 additions & 6 deletions src/Resources/config/factory.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
<argument type="service" id="mongo.client_registry" />
</service>

<service id="mongo.logger_factory"
class="Facile\MongoDbBundle\Services\LoggerFactory"
public="false">
<argument>%kernel.environment%</argument>
</service>

</services>

</container>
29 changes: 25 additions & 4 deletions src/Services/ClientRegistry.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

declare(strict_types=1);
declare(strict_types = 1);

namespace Facile\MongoDbBundle\Services;

use Facile\MongoDbBundle\Capsule\Client;
use Facile\MongoDbBundle\Capsule\Client as LoggerClient;
use Facile\MongoDbBundle\Models\ClientConfiguration;
use Facile\MongoDbBundle\Services\Loggers\DataCollectorLoggerInterface;
use MongoDB\Client;

/**
* Class ClientRegistry.
Expand All @@ -19,17 +20,21 @@ class ClientRegistry
private $configurations;
/** @var DataCollectorLoggerInterface */
private $logger;
/** @var string */
private $environment;

/**
* ClientRegistry constructor.
*
* @param DataCollectorLoggerInterface $logger
* @param string $environment
*/
public function __construct(DataCollectorLoggerInterface $logger)
public function __construct(DataCollectorLoggerInterface $logger, string $environment)
{
$this->clients = [];
$this->configurations = [];
$this->logger = $logger;
$this->environment = $environment;
}

/**
Expand Down Expand Up @@ -66,13 +71,29 @@ public function getClient(string $name, string $databaseName = null): Client
$conf = $this->configurations[$name];
$uri = sprintf('mongodb://%s:%d', $conf->getHost(), $conf->getPort());
$options = array_merge(['database' => $databaseName], $conf->getOptions());
$this->clients[$clientKey] = new Client($uri, $options, [], $this->logger);
$this->clients[$clientKey] = $this->buildClient($uri, $options, []);
$this->logger->addConnection($clientKey);
}

return $this->clients[$clientKey];
}

/**
* @param $uri
* @param array $options
* @param array $driverOptions
*
* @return Client
*/
private function buildClient($uri, array $options, array $driverOptions): Client
{
if ('dev' === $this->environment) {
return new LoggerClient($uri, $options, $driverOptions, $this->logger);
}

return new Client($uri, $options, $driverOptions);
}

/**
* @param array $conf
*
Expand Down
42 changes: 0 additions & 42 deletions src/Services/LoggerFactory.php

This file was deleted.

55 changes: 0 additions & 55 deletions src/Services/Loggers/NullLogger.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Facile\MongoDbBundle\DependencyInjection\MongoDbBundleExtension;
use Facile\MongoDbBundle\Services\Loggers\DataCollectorLoggerInterface;
use Facile\MongoDbBundle\Services\Loggers\MongoLogger;
use Facile\MongoDbBundle\Services\Loggers\NullLogger;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use MongoDB\Database;
use Facile\MongoDbBundle\Capsule\Database as LoggerDatabase;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\Stopwatch\Stopwatch;
Expand Down Expand Up @@ -46,16 +46,18 @@ public function test_load()
$this->assertContainerBuilderHasService('mongo.connection', Database::class);
$defaultConnection = $this->container->get('mongo.connection');
$this->assertInstanceOf(Database::class, $defaultConnection);
$this->assertInstanceOf(LoggerDatabase::class, $defaultConnection);
$this->assertSame('testdb', $defaultConnection->getDatabaseName());

// 'test_db' connection
$this->assertContainerBuilderHasService('mongo.connection.test_db', Database::class);
$defaultConnection = $this->container->get('mongo.connection.test_db');

$this->assertInstanceOf(Database::class, $defaultConnection);
$this->assertInstanceOf(LoggerDatabase::class, $defaultConnection);
$this->assertSame('testdb', $defaultConnection->getDatabaseName());

$this->assertContainerBuilderHasService('facile_mongo_db.logger', DataCollectorLoggerInterface::class);
$this->assertContainerBuilderHasService('facile_mongo_db.logger', MongoLogger::class);
$logger = $this->container->get('facile_mongo_db.logger');
$this->assertInstanceOf(MongoLogger::class, $logger);
}
Expand Down Expand Up @@ -83,9 +85,13 @@ public function test_load_env_prod()
);
$this->compile();

$this->assertContainerBuilderHasService('facile_mongo_db.logger', DataCollectorLoggerInterface::class);
$logger = $this->container->get('facile_mongo_db.logger');
$this->assertInstanceOf(NullLogger::class, $logger);
// 'test_db' connection
$this->assertContainerBuilderHasService('mongo.connection.test_db', Database::class);
$defaultConnection = $this->container->get('mongo.connection.test_db');

$this->assertInstanceOf(Database::class, $defaultConnection);
$this->assertNotInstanceOf(LoggerDatabase::class, $defaultConnection);
$this->assertSame('testdb', $defaultConnection->getDatabaseName());
}

public function test_load_multiple()
Expand Down
16 changes: 0 additions & 16 deletions tests/Unit/DataCollector/MongoDbDataCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,6 @@

class MongoDbDataCollectorTest extends \PHPUnit_Framework_TestCase
{
public function test_construction_null_logger()
{
$collector = new MongoDbDataCollector();
$collector->setLogger(new NullLogger());
$collector->collect(new Request(), new Response());

self::assertEquals(0, $collector->getQueryCount());
self::assertEmpty($collector->getQueries());

self::assertEquals(0, $collector->getTime());

self::assertEmpty($collector->getConnections());
self::assertEquals(0, $collector->getConnectionsCount());

self::assertEquals('mongodb', $collector->getName());
}

public function test_construction_logger()
{
Expand Down
29 changes: 0 additions & 29 deletions tests/Unit/Services/Loggers/NullLoggerTest.php

This file was deleted.

0 comments on commit 81bf625

Please sign in to comment.