From 8c81adc8e83969e90b4c54daf4a396b1094134ff Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Thu, 31 Aug 2017 17:30:54 +0200 Subject: Implemented container --- src/Container/Container.php | 105 +++++++++++++++++++++++++++++++++++ src/Container/ContainerException.php | 11 ++++ src/Container/NotFoundException.php | 10 ++++ 3 files changed, 126 insertions(+) create mode 100644 src/Container/Container.php create mode 100644 src/Container/ContainerException.php create mode 100644 src/Container/NotFoundException.php (limited to 'src/Container') diff --git a/src/Container/Container.php b/src/Container/Container.php new file mode 100644 index 00000000..df2f92fe --- /dev/null +++ b/src/Container/Container.php @@ -0,0 +1,105 @@ +has($id)) { + return $this->resolve($id); + } + + throw new NotFoundException(sprintf('The entry with the id "%s" could not be found')); + } + + /** + * Register a shared entry in the container + * + * @param string $abstract Identifier of the entry to set + * @param mixed $instance Entry + */ + public function instance($abstract, $instance) + { + $this->instances[$abstract] = $instance; + } + + /** + * Returns true if the container can return an entry for the given identifier + * Returns false otherwise + * + * `has($id)` returning true does not mean that `get($id)` will not throw an exception + * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface` + * + * @param string $id Identifier of the entry to look for + * + * @return bool + */ + public function has($id) + { + return isset($this->instances[$id]); + } + + /** + * Get the globally available instance of the container + * + * @return Container + */ + public static function getInstance() + { + if (is_null(static::$instance)) { + static::$instance = new static; + } + + return static::$instance; + } + + /** + * Set the globally available instance of the container + * + * @param Container $container + */ + public static function setInstance(Container $container) + { + static::$instance = $container; + } + + /** + * Resolve the requested object + * + * @param string $abstract + * @return mixed + */ + protected function resolve($abstract) + { + return $this->instances[$abstract]; + } +} diff --git a/src/Container/ContainerException.php b/src/Container/ContainerException.php new file mode 100644 index 00000000..3cdde506 --- /dev/null +++ b/src/Container/ContainerException.php @@ -0,0 +1,11 @@ + Date: Tue, 19 Sep 2017 14:50:20 +0200 Subject: Added Application --- includes/engelsystem_provider.php | 19 ++++++++----------- src/Application.php | 25 +++++++++++++++++++++++++ src/Container/Container.php | 35 +++++++++++++++++++++++------------ src/helpers.php | 8 ++++---- 4 files changed, 60 insertions(+), 27 deletions(-) create mode 100644 src/Application.php (limited to 'src/Container') diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index f3c161a6..e10fdba0 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -1,13 +1,12 @@ instance('container', $container); -$container->instance(ContainerInterface::class, $container); +$app = Application::getInstance(); /** * Load configuration */ $config = new Config(); -$container->instance('config', $config); +$app->instance('config', $config); $config->set(require __DIR__ . '/../config/config.default.php'); if (file_exists(__DIR__ . '/../config/config.php')) { @@ -48,7 +45,7 @@ date_default_timezone_set($config->get('timezone')); * @var Request $request */ $request = Request::createFromGlobals(); -$container->instance('request', $request); +$app->instance('request', $request); /** @@ -64,7 +61,7 @@ if ($config->get('maintenance')) { * Initialize renderer */ $renderer = new Renderer(); -$container->instance('renderer', $renderer); +$app->instance('renderer', $renderer); $renderer->addRenderer(new HtmlEngine()); @@ -72,7 +69,7 @@ $renderer->addRenderer(new HtmlEngine()); * Register error handler */ $errorHandler = new ExceptionHandler(); -$container->instance('error.handler', $errorHandler); +$app->instance('error.handler', $errorHandler); if (config('environment') == 'development') { $errorHandler->setEnvironment(ExceptionHandler::ENV_DEVELOPMENT); ini_set('display_errors', true); @@ -184,7 +181,7 @@ foreach ($includeFiles as $file) { * Init application */ $session = new Session(); -$container->instance('session', $session); +$app->instance('session', $session); $session->start(); $request->setSession($session); diff --git a/src/Application.php b/src/Application.php new file mode 100644 index 00000000..674b3869 --- /dev/null +++ b/src/Application.php @@ -0,0 +1,25 @@ +registerBaseBindings(); + } + + protected function registerBaseBindings() + { + self::setInstance($this); + Container::setInstance($this); + $this->instance('app', $this); + $this->instance('container', $this); + $this->instance(Container::class, $this); + $this->instance(Application::class, $this); + $this->instance(ContainerInterface::class, $this); + } +} diff --git a/src/Container/Container.php b/src/Container/Container.php index df2f92fe..9af5c1e6 100644 --- a/src/Container/Container.php +++ b/src/Container/Container.php @@ -48,6 +48,17 @@ class Container implements ContainerInterface * @param mixed $instance Entry */ public function instance($abstract, $instance) + { + $this->singleton($abstract, $instance); + } + + /** + * Register a shared entry as singleton in the container + * + * @param string $abstract + * @param mixed $instance + */ + public function singleton($abstract, $instance) { $this->instances[$abstract] = $instance; } @@ -68,10 +79,21 @@ class Container implements ContainerInterface return isset($this->instances[$id]); } + /** + * Resolve the requested object + * + * @param string $abstract + * @return mixed + */ + protected function resolve($abstract) + { + return $this->instances[$abstract]; + } + /** * Get the globally available instance of the container * - * @return Container + * @return self */ public static function getInstance() { @@ -91,15 +113,4 @@ class Container implements ContainerInterface { static::$instance = $container; } - - /** - * Resolve the requested object - * - * @param string $abstract - * @return mixed - */ - protected function resolve($abstract) - { - return $this->instances[$abstract]; - } } diff --git a/src/helpers.php b/src/helpers.php index 733b902d..b942068f 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,15 +1,15 @@ get($id); + return Application::getInstance()->get($id); } /** -- cgit v1.2.3-54-g00ecf From 2cb636b651c889243919d99eda8fa724d5c08392 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 19 Sep 2017 21:50:22 +0200 Subject: Added Container unit test --- src/Container/Container.php | 2 +- tests/Unit/Container/ContainerTest.php | 98 ++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Container/ContainerTest.php (limited to 'src/Container') diff --git a/src/Container/Container.php b/src/Container/Container.php index 9af5c1e6..59a17a04 100644 --- a/src/Container/Container.php +++ b/src/Container/Container.php @@ -38,7 +38,7 @@ class Container implements ContainerInterface return $this->resolve($id); } - throw new NotFoundException(sprintf('The entry with the id "%s" could not be found')); + throw new NotFoundException(sprintf('The entry with the id "%s" could not be found', $id)); } /** diff --git a/tests/Unit/Container/ContainerTest.php b/tests/Unit/Container/ContainerTest.php new file mode 100644 index 00000000..f0ba24e7 --- /dev/null +++ b/tests/Unit/Container/ContainerTest.php @@ -0,0 +1,98 @@ +instance('foo', $class); + $this->assertSame($class, $container->get('foo')); + } + + /** + * @covers \Engelsystem\Container\Container::get + * @expectedException \Engelsystem\Container\NotFoundException + */ + public function testGetException() + { + $container = new Container(); + + $container->get('not.registered.service'); + } + + /** + * @covers \Engelsystem\Container\Container::instance + * @covers \Engelsystem\Container\Container::resolve + */ + public function testInstance() + { + $container = new Container(); + $class = new class + { + }; + + $container->instance('foo', $class); + $this->assertSame($class, $container->get('foo')); + } + + /** + * @covers \Engelsystem\Container\Container::has + */ + public function testHas() + { + $container = new Container(); + + $this->assertFalse($container->has('test')); + + $class = new class + { + }; + + $container->instance('test', $class); + $this->assertTrue($container->has('test')); + } + + /** + * @covers \Engelsystem\Container\Container::singleton + */ + public function testSingleton() + { + $container = new Container(); + $class = new class + { + }; + + $container->singleton('foo', $class); + $this->assertSame($class, $container->get('foo')); + $this->assertSame($class, $container->get('foo')); + } + + /** + * @covers \Engelsystem\Container\Container::setInstance + * @covers \Engelsystem\Container\Container::getInstance + */ + public function testContainerSingleton() + { + $container0 = new Container(); + $container = Container::getInstance(); + + $this->assertNotSame($container0, $container); + + $container1 = new Container; + Container::setInstance($container1); + + $this->assertSame($container1, Container::getInstance()); + } +} -- cgit v1.2.3-54-g00ecf