diff --git a/src/Container/AurynContainer.php b/src/Container/AurynContainer.php index cf4dd970..ae364403 100644 --- a/src/Container/AurynContainer.php +++ b/src/Container/AurynContainer.php @@ -17,7 +17,7 @@ * @author Matthew Turland * @author Rougin Gutib */ -class AurynContainer extends Injector implements ContainerInterface +class AurynContainer implements ContainerInterface { /** * @var array @@ -35,25 +35,11 @@ class AurynContainer extends Injector implements ContainerInterface protected $items = array(); /** - * Initializes the container instance. - * NOTE: To be removed in v1.0.0. It should use \Auryn\Injector::__construct. - * - * @param \Auryn\Injector|\Auryn\Reflector|null $data + * @param \Auryn\Injector $injector */ - public function __construct($data = null) + public function __construct(Injector $injector) { - $injector = $data instanceof Injector; - - $instance = $this; - - if ($injector) $instance = $data; - - if (! $injector) - { - parent::__construct($data); - } - - $this->injector = $instance; + $this->injector = $injector; } /** @@ -127,8 +113,55 @@ public function has($id) */ public function set($id, $concrete) { + if (! $concrete || is_array($concrete)) + { + $params = is_array($concrete) ? $concrete : array(); + + $this->items[$id] = $this->injector->make($id, $params); + + return $this; + } + $this->items[$id] = $concrete; + // Tries to set the instance as shareable --- + try + { + $this->injector->share($concrete); + } + // @codeCoverageIgnoreStart + catch (\Exception $error) {} + // @codeCoverageIgnoreEnd + // ------------------------------------------ + + // Also create an alias if available --- + try + { + /** @var object $concrete */ + $class = get_class($concrete); + + $this->injector->alias($id, $class); + } + // @codeCoverageIgnoreStart + catch (\Exception $error) {} + // @codeCoverageIgnoreEnd + // ------------------------------------- + return $this; } + + /** + * Calls methods from the \Auryn\Injector instance. + * + * @param string $method + * @param mixed[] $params + * @return mixed + */ + public function __call($method, $params) + { + /** @var callable $class */ + $class = array($this->injector, $method); + + return call_user_func_array($class, $params); + } } diff --git a/src/System.php b/src/System.php index 19df7ae4..cfb4f98c 100644 --- a/src/System.php +++ b/src/System.php @@ -2,9 +2,10 @@ namespace Rougin\Slytherin; -use Psr\Container\ContainerInterface; use Psr\Http\Message\ServerRequestInterface; +use Rougin\Slytherin\Component\Collection; use Rougin\Slytherin\Container\Container; +use Rougin\Slytherin\Container\ContainerInterface; use Rougin\Slytherin\Integration\ConfigurationInterface; use Rougin\Slytherin\System\Handler; @@ -18,7 +19,7 @@ */ class System { - const CONTAINER = 'Psr\Container\ContainerInterface'; + const CONTAINER = 'Rougin\Slytherin\Container\ContainerInterface'; const DISPATCHER = 'Rougin\Slytherin\Routing\DispatcherInterface'; @@ -49,18 +50,26 @@ class System /** * Initializes the application instance. * - * @param \Rougin\Slytherin\Container\ContainerInterface|null $container - * @param \Rougin\Slytherin\Integration\Configuration|null $config + * @param mixed|null $container + * @param \Rougin\Slytherin\Integration\Configuration|null $config */ - public function __construct(ContainerInterface $container = null, ConfigurationInterface $config = null) + public function __construct($container = null, ConfigurationInterface $config = null) { if (! $config) $config = new Configuration; $this->config = $config; + if ($container instanceof Collection) + { + $container = $container->getContainer(); + } + if (! $container) $container = new Container; - $this->container = $container; + if ($container instanceof ContainerInterface) + { + $this->container = $container; + } } /** diff --git a/tests/Application/AurynContainerTest.php b/tests/Application/AurynContainerTest.php index 9c013f7d..ab3a1522 100644 --- a/tests/Application/AurynContainerTest.php +++ b/tests/Application/AurynContainerTest.php @@ -2,6 +2,7 @@ namespace Rougin\Slytherin\Application; +use Auryn\Injector; use Rougin\Slytherin\Container\AurynContainer; use Rougin\Slytherin\Http\Response; use Rougin\Slytherin\Middleware\Dispatcher as Middleware; @@ -28,7 +29,7 @@ protected function doSetUp() $this->markTestSkipped('Auryn is not installed.'); } - $container = new AurynContainer; + $container = new AurynContainer(new Injector); $router = $this->router(); diff --git a/tests/Component/CollectionTest.php b/tests/Component/CollectionTest.php index 3742b42e..23277dc0 100644 --- a/tests/Component/CollectionTest.php +++ b/tests/Component/CollectionTest.php @@ -11,6 +11,7 @@ use Rougin\Slytherin\IoC\Vanilla\Container; use Rougin\Slytherin\Middleware\Interop; use Rougin\Slytherin\Middleware\VanillaMiddleware; +use Rougin\Slytherin\System; use Rougin\Slytherin\Template\TwigLoader; use Rougin\Slytherin\Template\TwigRenderer; use Rougin\Slytherin\Testcase; @@ -229,4 +230,14 @@ public function testSetTemplateMethod() $this->assertEquals($expected, $actual); } + + /** + * Tests the has() method. + * + * @return void + */ + public function testHasMethod() + { + $this->assertFalse($this->components->has(System::TEMPLATE)); + } } diff --git a/tests/Component/Server.php b/tests/Component/Server.php index e0ccfc91..8cbe4a13 100644 --- a/tests/Component/Server.php +++ b/tests/Component/Server.php @@ -9,11 +9,9 @@ $items = array(); -$items[] = 'Rougin\Slytherin\Fixture\Components\CollectionComponent'; $items[] = 'Rougin\Slytherin\Fixture\Components\DebuggerComponent'; $items[] = 'Rougin\Slytherin\Fixture\Components\DispatcherComponent'; $items[] = 'Rougin\Slytherin\Fixture\Components\HttpComponent'; -$items[] = 'Rougin\Slytherin\Fixture\Components\SingleComponent'; $components = Collector::get($items); diff --git a/tests/Previous/Builder.php b/tests/Previous/Builder.php new file mode 100644 index 00000000..27d7b0c7 --- /dev/null +++ b/tests/Previous/Builder.php @@ -0,0 +1,134 @@ + + */ +class Builder +{ + /** + * @var \Rougin\Slytherin\ComponentCollection + */ + protected $collection; + + public function __construct() + { + $this->collection = new ComponentCollection; + } + + public function make($method = null, $uri = null) + { + // Initialize the DependencyInjectorInterface --- + $this->setContainer(); + // ---------------------------------------------- + + // Initialize the ErrorHandlerInterface --- + $this->setDebugger(); + // ---------------------------------------- + + // Initialize the ServerRequestInterface and ResponseInterface --- + $this->setHttp($method, $uri); + // --------------------------------------------------------------- + + // Initialize the routing dispatcher interface ----- + $this->setRouting(); + // ------------------------------------------------- + + // Initialize the middleware --- + $this->setMiddleware(); + // ----------------------------- + + return new Application($this->collection); + } + + protected function setContainer() + { + // Initialize the RendererInterface ---------------------- + $views = (string) realpath(__DIR__ . '/Plates'); + $loader = new Twig_Loader_Filesystem($views); + $twig = new Twig(new Twig_Environment($loader)); + $renderer = 'Rougin\Slytherin\Template\RendererInterface'; + // ------------------------------------------------------- + + $auryn = new Auryn(new \Auryn\Injector); + + // Create an alias for the RendererInterface --- + $auryn->share($twig); + $auryn->alias($renderer, get_class($twig)); + // --------------------------------------------- + + $this->collection->setDependencyInjector($auryn); + + return $this; + } + + protected function setDebugger() + { + $whoops = new Whoops(new \Whoops\Run); + + $this->collection->setErrorHandler($whoops); + + return $this; + } + + protected function setHttp($method = null, $uri = null) + { + $request = ServerRequestFactory::fromGlobals(); + + if ($method && $uri) + { + $uri = new Uri('http://localhost:8000' . $uri); + + $request = $request->withUri($uri); + + $request = $request->withMethod($method); + } + + $response = new Response; + + $this->collection->setHttp($request, $response); + + return $this; + } + + protected function setMiddleware() + { + $pipe = new MiddlewarePipe; + + $middleware = new StratigilityMiddleware($pipe); + + $this->collection->setMiddleware($middleware); + + return $this; + } + + protected function setRouting() + { + $router = require realpath(__DIR__ . '/Router.php'); + + $response = $this->collection->getHttpResponse(); + + $dispatcher = new Dispatcher($router, $response); + + $this->collection->setDispatcher($dispatcher); + + return $this; + } +} diff --git a/tests/Previous/PreviousTest.php b/tests/Previous/PreviousTest.php new file mode 100644 index 00000000..fbcd4f34 --- /dev/null +++ b/tests/Previous/PreviousTest.php @@ -0,0 +1,79 @@ + + */ +class PreviousTest extends Testcase +{ + /** + * @var \Rougin\Slytherin\Previous\Builder + */ + protected $builder; + + protected function doSetUp() + { + if (! class_exists('Auryn\Injector')) + { + $this->markTestSkipped('Auryn is not installed.'); + } + + if (! class_exists('Zend\Stratigility\MiddlewarePipe')) + { + $this->markTestSkipped('Zend Stratigility is not installed.'); + } + + if (! class_exists('Zend\Diactoros\Response')) + { + $this->markTestSkipped('Zend Diactoros is not installed.'); + } + + if (! class_exists('Twig_Loader_Filesystem')) + { + $this->markTestSkipped('Twig v1.0~v2.0 is not installed.'); + } + + $this->builder = new Builder; + } + + /** + * @runInSeparateProcess + * + * @return void + */ + public function test_sample_text() + { + $this->expectOutputString('Hello world!'); + + $this->builder->make('GET', '/')->run(); + } + + /** + * @runInSeparateProcess + * + * @return void + */ + public function test_arguments_in_uri() + { + $this->expectOutputString('Hello Rougin!'); + + $this->builder->make('GET', '/hi/Rougin')->run(); + } + + /** + * @runInSeparateProcess + * + * @return void + */ + public function test_middleware_changing_the_response_parameter() + { + $this->expectOutputString('Hello from middleware'); + + $this->builder->make('GET', '/hello')->run(); + } +} diff --git a/tests/Previous/Router.php b/tests/Previous/Router.php index c2833a77..68b87273 100644 --- a/tests/Previous/Router.php +++ b/tests/Previous/Router.php @@ -10,10 +10,10 @@ $router->addRoute('GET', '/hi/:name', [ "$name\Hello", 'hi' ]); -// Add the middlewares to a specified route --------------- +// Add the middlewares to a specified route --------------------------- $items = array('Rougin\Slytherin\Previous\Handlers\Hello'); -$router->addRoute('GET', '/hello', function () {}, $items); -// -------------------------------------------------------- +$router->addRoute('GET', '/hello', function () { return ''; }, $items); +// -------------------------------------------------------------------- return $router; diff --git a/tests/Previous/Server.php b/tests/Previous/Server.php index 45387d6d..0c02d5d8 100644 --- a/tests/Previous/Server.php +++ b/tests/Previous/Server.php @@ -1,64 +1,11 @@ share($twig); -$auryn->alias($renderer, get_class($twig)); -// --------------------------------------------- -$component->setDependencyInjector($auryn); -// ---------------------------------------------- - -// Initialize the ErrorHandlerInterface --- -$whoops = new Whoops(new \Whoops\Run); -$component->setErrorHandler($whoops); -// ---------------------------------------- - -// Initialize the ServerRequestInterface and ResponseInterface --- -$request = ServerRequestFactory::fromGlobals(); -$response = new Response; -$component->setHttp($request, $response); -// --------------------------------------------------------------- - -// Initialize the routing dispatcher interface ----- -$router = require realpath(__DIR__ . '/Router.php'); -$dispatcher = new Dispatcher($router, $response); -$component->setDispatcher($dispatcher); -// ------------------------------------------------- - -// Initialize the middleware ------------------- -$pipe = new MiddlewarePipe; -$middleware = new StratigilityMiddleware($pipe); -$component->setMiddleware($middleware); -// --------------------------------------------- - -// Initialize then run the Application --- -$app = new Application($component); - -$app->run(); -// --------------------------------------- +$builder->make()->run(); diff --git a/tests/Sample/SampleTest.php b/tests/Sample/SampleTest.php index 9b44c8a0..713a35be 100644 --- a/tests/Sample/SampleTest.php +++ b/tests/Sample/SampleTest.php @@ -1,6 +1,6 @@ addPackage(new SamplePackage); $builder->addHandler(new Cors); -echo $builder->make()->run(); +$builder->make()->run();