Skip to content
This repository has been archived by the owner on Dec 26, 2021. It is now read-only.

Commit

Permalink
ability to customize services (logger, client) via json config file
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Feb 2, 2015
1 parent f43c6aa commit ec0af83
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 43 deletions.
20 changes: 20 additions & 0 deletions bin/phpreflect.json-schema
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@
}
}
}
},
"services": {
"type": "array",
"description": "List of services available.",
"items": {
"type": "object",
"additionalProperties": true,
"properties": {
"client": {
"type": "string",
"description": "Client to interact with API.",
"required": false
},
"logger": {
"type": "string",
"description": "Fully qualified class name of a compatible PSR-3 logger.",
"required": false
}
}
}
}
}
}
4 changes: 3 additions & 1 deletion bin/phpreflect.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
],
"plugins": [
],
"analysers" : [
"analysers": [
],
"services": [
]
}
40 changes: 0 additions & 40 deletions src/Bartlett/Reflect/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Console Application.
*
Expand Down Expand Up @@ -151,16 +149,6 @@ public function getDispatcher()
return $this->eventDispatcher;
}

public function getLogger()
{
return $this->getContainer()->get($this->getName() . '.logger');
}

public function getClient()
{
return $this->getContainer()->get($this->getName() . '.client');
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -306,32 +294,4 @@ protected function getDefaultInputDefinition()
}
return $definition;
}

private function createContainer()
{
$container = new ContainerBuilder();

// client for interacting with the API
$container->register(
$this->getName() . '.client',
'Bartlett\Reflect\Client\LocalClient'
);

// PSR-3 compatible logger
$container->register(
$this->getName() . '.logger',
'Bartlett\Reflect\Plugin\Log\DefaultLogger'
);

return $container;
}

private function getContainer()
{
if ($this->container === null) {
$this->container = $this->createContainer();
}

return $this->container;
}
}
3 changes: 2 additions & 1 deletion src/Bartlett/Reflect/Console/CommandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Bartlett\Reflect\Client;
use Bartlett\Reflect\Events;
use Bartlett\Reflect\Environment;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
Expand Down Expand Up @@ -244,7 +245,7 @@ private function createCode($namespace, \ReflectionMethod $method)
return function (InputInterface $input, OutputInterface $output) use ($namespace, $method, $app) {
$methodName = $method->getName();

$client = new Client($app->getClient());
$client = new Client(Environment::getClient());
$api = $client->api(strtolower($namespace));
$api->setEventDispatcher($app->getDispatcher());

Expand Down
74 changes: 74 additions & 0 deletions src/Bartlett/Reflect/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace Bartlett\Reflect;

use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Application Environment.
*
Expand All @@ -27,6 +29,8 @@
*/
class Environment
{
protected static $container;

/**
* Search a json file on a list of scan directory pointed by
* the BARTLETT_SCAN_DIR env var.
Expand Down Expand Up @@ -68,4 +72,74 @@ public static function setScanDir()
putenv("BARTLETT_SCAN_DIR=" . implode(PATH_SEPARATOR, $dirs));
}
}

/**
* Gets a client to interact with the API
*
* @return \Bartlett\Reflect\Client\ClientInterface
*/
public static function getClient()
{
$prefix = str_replace(array('.json', '.dist'), '', getenv('BARTLETTRC'));
return self::getContainer()->get($prefix . '.client');
}

/**
* Gets a compatible PSR-3 logger
*
* @return \Psr\Log\LoggerInterface
*/
public static function getLogger()
{
$prefix = str_replace(array('.json', '.dist'), '', getenv('BARTLETTRC'));
return self::getContainer()->get($prefix . '.logger');
}

private static function createContainer()
{
// default values
$clientClass = 'Bartlett\Reflect\Client\LocalClient';
$loggerClass = 'Bartlett\Reflect\Plugin\Log\DefaultLogger';

$container = new ContainerBuilder();

$jsonFile = self::getJsonConfigFilename();
if ($jsonFile) {
$json = file_get_contents($jsonFile);
$var = json_decode($json, true);

if (isset($var['services'])) {
foreach ($var['services'] as $service) {
if (array_key_exists('client', $service)) {
if (class_exists($service['client'])) {
$clientClass = $service['client'];
}
}
if (array_key_exists('logger', $service)) {
if (class_exists($service['logger'])) {
$loggerClass = $service['logger'];
}
}
}
}
}

$prefix = str_replace(array('.json', '.dist'), '', getenv('BARTLETTRC'));

// client for interacting with the API
$container->register($prefix . '.client', $clientClass);

// PSR-3 compatible logger
$container->register($prefix . '.logger', $loggerClass);

return $container;
}

private static function getContainer()
{
if (self::$container === null) {
self::$container = self::createContainer();
}
return self::$container;
}
}
26 changes: 26 additions & 0 deletions tests/Environment/YourLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Bartlett\Tests\Reflect\Environment;

use Monolog\Logger;
use Monolog\Handler\RotatingFileHandler;

class YourLogger extends Logger
{
public function __construct($name = 'DEV')
{
$tempDir = sys_get_temp_dir() . '/bartlett/logs';

if (!file_exists($tempDir)) {
mkdir($tempDir, 0755, true);
}
$filename = sprintf('phpreflect-%s.log', date('Ymd'));

$stream = new RotatingFileHandler("$tempDir/$filename", 30);
$stream->setFilenameFormat('{filename}-{date}', 'Ymd');

$handlers = array($stream);

parent::__construct($name, $handlers);
}
}
17 changes: 17 additions & 0 deletions tests/Environment/phpreflect.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"source-providers": [
{
"in": ". as current",
"name": "/\\.(php|inc|phtml)$/"
}
],
"plugins": [
],
"analysers": [
],
"services": [
{
"logger": "Bartlett\\Tests\\Reflect\\Environment\\YourLogger"
}
]
}
86 changes: 85 additions & 1 deletion tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function setUpBeforeClass()
*/
public static function tearDownAfterClass()
{
unlink(__DIR__ . '/' . self::DIST_RC);
@unlink(__DIR__ . '/' . self::DIST_RC);
}

/**
Expand Down Expand Up @@ -156,4 +156,88 @@ public function testSetDefaultScanDir()
"Environment variable BARTLETT_SCAN_DIR does not match."
);
}

/**
* @covers Bartlett\Reflect\Environment::getLogger
* @runInSeparateProcess
*
* @return void
*/
public function testDefaultLoggerAccessor()
{
$singleScanDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'bin';

putenv("BARTLETT_SCAN_DIR=$singleScanDir");
putenv("BARTLETTRC=" . self::DIST_RC);

$logger = Environment::getLogger();

$this->assertInstanceOf(
'Psr\Log\LoggerInterface',
$logger,
'This is not a compatible PSR-3 logger'
);

$this->assertEquals(
'Bartlett\Reflect\Plugin\Log\DefaultLogger',
get_class($logger),
'Default logger does not match.'
);
}

/**
* @covers Bartlett\Reflect\Environment::getLogger
* @runInSeparateProcess
*
* @return void
*/
public function testCustomLoggerAccessor()
{
$singleScanDir = __DIR__ . DIRECTORY_SEPARATOR . 'Environment';

putenv("BARTLETT_SCAN_DIR=$singleScanDir");
putenv("BARTLETTRC=phpreflect.json");

$logger = Environment::getLogger();

$this->assertInstanceOf(
'Psr\Log\LoggerInterface',
$logger,
'This is not a compatible PSR-3 logger'
);

$this->assertEquals(
'Bartlett\Tests\Reflect\Environment\YourLogger',
get_class($logger),
'Custom logger does not match.'
);
}

/**
* @covers Bartlett\Reflect\Environment::getClient
* @runInSeparateProcess
*
* @return void
*/
public function testDefaultClientAccessor()
{
$singleScanDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'bin';

putenv("BARTLETT_SCAN_DIR=$singleScanDir");
putenv("BARTLETTRC=" . self::DIST_RC);

$client = Environment::getClient();

$this->assertInstanceOf(
'Bartlett\Reflect\Client\ClientInterface',
$client,
'This is not a compatible Reflect API client'
);

$this->assertEquals(
'Bartlett\Reflect\Client\LocalClient',
get_class($client),
'Default client does not match.'
);
}
}
2 changes: 2 additions & 0 deletions tests/bootstrap.dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
=> __DIR__ . '/Analyser/BarAnalyser.php',
'Bartlett\Tests\Reflect\Model\GenericModelTest'
=> __DIR__ . '/Model/GenericModelTest.php',
'Bartlett\Tests\Reflect\Environment\YourLogger'
=> __DIR__ . '/Environment/YourLogger.php',
'Bartlett\LoggerTestListener'
=> __DIR__ . '/../vendor/bartlett/phpunit-loggertestlistener/src/Bartlett/LoggerTestListener.php',
'Monolog\Handler\GrowlHandler'
Expand Down
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
=> __DIR__ . '/Analyser/BarAnalyser.php',
'Bartlett\Tests\Reflect\Model\GenericModelTest'
=> __DIR__ . '/Model/GenericModelTest.php',
'Bartlett\Tests\Reflect\Environment\YourLogger'
=> __DIR__ . '/Environment/YourLogger.php',
)
);

0 comments on commit ec0af83

Please sign in to comment.