Skip to content

Commit

Permalink
Merge pull request #79 from pennyphp/feature/di-factory
Browse files Browse the repository at this point in the history
Create container factory
  • Loading branch information
Gianluca Arbezzano committed Oct 2, 2015
2 parents fcecbdf + 7455612 commit 726f3af
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 34 deletions.
33 changes: 5 additions & 28 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,37 +43,14 @@ class App
*/
public function __construct(ContainerInterface $container = null)
{
$this->container = $container ?: static::buildContainer(Loader::load());
$container = &$this->container;
if ($container === null) {
$container = Container\PHPDiFactory::buildContainer(Loader::load());
}
$this->container = $container;

if ($container->has('router') == false) {
throw new Exception('Define router config');
}

$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();
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/Container/PHPDiFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace GianArb\Penny\Container;

use DI;

class PHPDiFactory
{
/**
* 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);
$container = $builder->build();
$container->set('di', $container);

return $container;
}
}
3 changes: 2 additions & 1 deletion tests/AppLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +25,7 @@ public function setUp()
'name' => 'load',
]);
});
$this->container = App::buildContainer($config);
$this->container = Container\PHPDiFactory::buildContainer($config);
}

public function testCorrectInjection()
Expand Down
19 changes: 17 additions & 2 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -178,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());
}
}
3 changes: 2 additions & 1 deletion tests/DiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion tests/EventFlowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion tests/Http/SymfonyKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions tests/config/app.test.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
'router' => \FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/', ['TestApp\Controller\Index', 'index']);
})
];

0 comments on commit 726f3af

Please sign in to comment.