From d1f4a1c516cdd9d3b8648b2933592587faeb3707 Mon Sep 17 00:00:00 2001 From: Gianluca Arbezzano Date: Thu, 1 Oct 2015 23:58:50 +0200 Subject: [PATCH 1/3] Create container factory Reference #77 --- src/App.php | 27 ++------------------------- src/Container/PHPDiFactory.php | 31 +++++++++++++++++++++++++++++++ tests/AppLoaderTest.php | 3 ++- tests/AppTest.php | 3 ++- tests/DiTest.php | 3 ++- tests/EventFlowTest.php | 3 ++- tests/Http/SymfonyKernelTest.php | 3 ++- 7 files changed, 43 insertions(+), 30 deletions(-) create mode 100644 src/Container/PHPDiFactory.php diff --git a/src/App.php b/src/App.php index b52e073..79d9108 100644 --- a/src/App.php +++ b/src/App.php @@ -2,9 +2,9 @@ namespace GianArb\Penny; -use DI; use Exception; use GianArb\Penny\Config\Loader; +use GianArb\Penny\Container; use GianArb\Penny\Event\HttpFlowEvent; use ReflectionClass; use Zend\Diactoros\Response; @@ -43,7 +43,7 @@ class App */ public function __construct(ContainerInterface $container = null) { - $this->container = $container ?: static::buildContainer(Loader::load()); + $this->container = $container ?: Container\PHPDiFactory::buildContainer(Loader::load()); $container = &$this->container; if ($container->has('router') == false) { @@ -53,29 +53,6 @@ public function __construct(ContainerInterface $container = null) $container->set('di', $container); } - /** - * Container compilation. - * - * @param mixed $config Configuration file/array. - * - * @link http://php-di.org/doc/php-definitions.html - * - * @return ContainerInterface - */ - public static function buildContainer($config = []) - { - $builder = new DI\ContainerBuilder(); - $builder->useAnnotations(true); - $builder->addDefinitions([ - 'event_manager' => DI\object('Zend\EventManager\EventManager'), - 'dispatcher' => DI\object('GianArb\Penny\Dispatcher') - ->constructor(DI\get('router')), - ]); - $builder->addDefinitions($config); - - return $builder->build(); - } - /** * Container getter. * diff --git a/src/Container/PHPDiFactory.php b/src/Container/PHPDiFactory.php new file mode 100644 index 0000000..74ecc7c --- /dev/null +++ b/src/Container/PHPDiFactory.php @@ -0,0 +1,31 @@ +useAnnotations(true); + $builder->addDefinitions([ + "event_manager" => DI\object('Zend\EventManager\EventManager'), + "dispatcher" => DI\object('GianArb\Penny\Dispatcher') + ->constructor(DI\get('router')), + ]); + $builder->addDefinitions($config); + + return $builder->build(); + } +} diff --git a/tests/AppLoaderTest.php b/tests/AppLoaderTest.php index bc21e71..db6ee8a 100644 --- a/tests/AppLoaderTest.php +++ b/tests/AppLoaderTest.php @@ -5,6 +5,7 @@ use DI\ContainerBuilder; use FastRoute; use GianArb\Penny\App; +use GianArb\Penny\Container; use GianArb\Penny\Config\Loader; use PHPUnit_Framework_TestCase; use Zend\Diactoros\Request; @@ -24,7 +25,7 @@ public function setUp() 'name' => 'load', ]); }); - $this->container = App::buildContainer($config); + $this->container = Container\PHPDiFactory::buildContainer($config); } public function testCorrectInjection() diff --git a/tests/AppTest.php b/tests/AppTest.php index 4b53fdb..cf0712f 100644 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -4,6 +4,7 @@ use FastRoute; use GianArb\Penny\App; +use GianArb\Penny\Container; use GianArb\Penny\Exception\MethodNotAllowed; use GianArb\Penny\Exception\RouteNotFound; use GianArb\Penny\Config\Loader; @@ -26,7 +27,7 @@ public function setUp() $r->addRoute('GET', '/dummy', ['TestApp\Controller\Index', 'dummy']); }); - $this->app = new App(App::buildContainer($config)); + $this->app = new App(Container\PHPDiFactory::buildContainer($config)); $this->app->getContainer()->get('event_manager')->attach('ERROR_DISPATCH', function ($e) { if ($e->getException() instanceof RouteNotFound) { diff --git a/tests/DiTest.php b/tests/DiTest.php index 8195e90..d274531 100644 --- a/tests/DiTest.php +++ b/tests/DiTest.php @@ -6,6 +6,7 @@ use GianArb\Penny\Config\Loader; use FastRoute; use GianArb\Penny\App; +use GianArb\Penny\Container; use PHPUnit_Framework_TestCase; use Zend\Diactoros\Request; use Zend\Diactoros\Response; @@ -23,7 +24,7 @@ public function setUp() $r->addRoute('GET', '/', ['TestApp\Controller\Index', 'diTest']); }); - $this->container = App::buildContainer($config); + $this->container = Container\PHPDiFactory::buildContainer($config); } public function testInjectionHttpFlow() diff --git a/tests/EventFlowTest.php b/tests/EventFlowTest.php index bccdb63..e968257 100644 --- a/tests/EventFlowTest.php +++ b/tests/EventFlowTest.php @@ -5,6 +5,7 @@ use FastRoute; use GianArb\Penny\Config\Loader; use GianArb\Penny\App; +use GianArb\Penny\Container; use GianArb\Penny\Exception\MethodNotAllowed; use GianArb\Penny\Exception\RouteNotFound; use PHPUnit_Framework_TestCase; @@ -23,7 +24,7 @@ public function setUp() $r->addRoute('GET', '/', ['TestApp\Controller\Index', 'index']); }); - $this->app = new App(App::buildContainer($config)); + $this->app = new App(Container\PHPDiFactory::buildContainer($config)); } public function testStopEventFlow() { diff --git a/tests/Http/SymfonyKernelTest.php b/tests/Http/SymfonyKernelTest.php index bda62d7..7a23197 100644 --- a/tests/Http/SymfonyKernelTest.php +++ b/tests/Http/SymfonyKernelTest.php @@ -2,6 +2,7 @@ namespace GianArb\PennyTest\Http; use GianArb\Penny\App; +use GianArb\Penny\Container; use GianArb\Penny\Config\Loader; use FastRoute; use Symfony\Component\HttpFoundation\Request; @@ -20,7 +21,7 @@ public function setUp() $config['dispatcher'] = \Di\object('GianArb\PennyTest\Utils\FastSymfonyDispatcher') ->constructor(\Di\get("router")); - $this->app = new App(App::buildContainer($config)); + $this->app = new App(Container\PHPDiFactory::buildContainer($config)); } public function testRunErrorReturnSameHttpObjects() From b1b5eed37cae27c363f7407c1e43ccfabc139270 Mon Sep 17 00:00:00 2001 From: Gianluca Arbezzano Date: Fri, 2 Oct 2015 00:02:14 +0200 Subject: [PATCH 2/3] Remove all container set --- src/App.php | 3 --- src/Container/PHPDiFactory.php | 16 ++++++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/App.php b/src/App.php index 79d9108..5aea751 100644 --- a/src/App.php +++ b/src/App.php @@ -44,13 +44,10 @@ class App public function __construct(ContainerInterface $container = null) { $this->container = $container ?: Container\PHPDiFactory::buildContainer(Loader::load()); - $container = &$this->container; if ($container->has('router') == false) { throw new Exception('Define router config'); } - - $container->set('di', $container); } /** diff --git a/src/Container/PHPDiFactory.php b/src/Container/PHPDiFactory.php index 74ecc7c..8e575ac 100644 --- a/src/Container/PHPDiFactory.php +++ b/src/Container/PHPDiFactory.php @@ -19,13 +19,17 @@ public static function buildContainer($config = []) { $builder = new DI\ContainerBuilder(); $builder->useAnnotations(true); - $builder->addDefinitions([ - "event_manager" => DI\object('Zend\EventManager\EventManager'), - "dispatcher" => DI\object('GianArb\Penny\Dispatcher') - ->constructor(DI\get('router')), - ]); + $builder->addDefinitions( + [ + "event_manager" => DI\object('Zend\EventManager\EventManager'), + "dispatcher" => DI\object('GianArb\Penny\Dispatcher') + ->constructor(DI\get('router')), + ] + ); $builder->addDefinitions($config); + $container = $builder->build(); + $container->set('di', $container); - return $builder->build(); + return $container; } } From 7455612335f6557f53b1f7de4380aec00f61946b Mon Sep 17 00:00:00 2001 From: Gianluca Arbezzano Date: Fri, 2 Oct 2015 09:20:59 +0200 Subject: [PATCH 3/3] Test default container factory --- src/App.php | 5 ++++- src/Container/PHPDiFactory.php | 4 ++-- tests/AppTest.php | 16 +++++++++++++++- tests/config/app.test.config.php | 7 +++++++ 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 tests/config/app.test.config.php diff --git a/src/App.php b/src/App.php index 5aea751..abec694 100644 --- a/src/App.php +++ b/src/App.php @@ -43,7 +43,10 @@ class App */ public function __construct(ContainerInterface $container = null) { - $this->container = $container ?: Container\PHPDiFactory::buildContainer(Loader::load()); + if ($container === null) { + $container = Container\PHPDiFactory::buildContainer(Loader::load()); + } + $this->container = $container; if ($container->has('router') == false) { throw new Exception('Define router config'); diff --git a/src/Container/PHPDiFactory.php b/src/Container/PHPDiFactory.php index 8e575ac..97ec8e1 100644 --- a/src/Container/PHPDiFactory.php +++ b/src/Container/PHPDiFactory.php @@ -21,8 +21,8 @@ public static function buildContainer($config = []) $builder->useAnnotations(true); $builder->addDefinitions( [ - "event_manager" => DI\object('Zend\EventManager\EventManager'), - "dispatcher" => DI\object('GianArb\Penny\Dispatcher') + 'event_manager' => DI\object('Zend\EventManager\EventManager'), + 'dispatcher' => DI\object('GianArb\Penny\Dispatcher') ->constructor(DI\get('router')), ] ); diff --git a/tests/AppTest.php b/tests/AppTest.php index cf0712f..da47675 100644 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -179,7 +179,21 @@ public function testDispatcherShouldBeCallable() }); $config['dispatcher'] = new \StdClass(); - $app = new App(App::buildContainer($config)); + $app = new App(Container\PHPDiFactory::buildContainer($config)); $app->run($request, $response); } + + public function testWithInternalContainerFactory() + { + chdir(dirname(__DIR__.'/../')); + $app = new App(); + + $request = (new Request()) + ->withUri(new Uri('/')) + ->withMethod('GET'); + $response = new Response(); + + $response = $app->run($request, $response); + $this->assertEquals(200, $response->getStatusCode()); + } } diff --git a/tests/config/app.test.config.php b/tests/config/app.test.config.php new file mode 100644 index 0000000..3c568e2 --- /dev/null +++ b/tests/config/app.test.config.php @@ -0,0 +1,7 @@ + \FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) { + $r->addRoute('GET', '/', ['TestApp\Controller\Index', 'index']); + }) +];