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/Config/Config.php | 28 ---------- src/Container/Container.php | 105 +++++++++++++++++++++++++++++++++++ src/Container/ContainerException.php | 11 ++++ src/Container/NotFoundException.php | 10 ++++ src/Http/Request.php | 25 --------- src/Renderer/Renderer.php | 24 +------- src/Routing/UrlGenerator.php | 4 +- src/helpers.php | 29 ++++++++-- 8 files changed, 152 insertions(+), 84 deletions(-) create mode 100644 src/Container/Container.php create mode 100644 src/Container/ContainerException.php create mode 100644 src/Container/NotFoundException.php (limited to 'src') diff --git a/src/Config/Config.php b/src/Config/Config.php index 02080de4..34c21a78 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -2,15 +2,8 @@ namespace Engelsystem\Config; -use ErrorException; - class Config { - /** - * @var self - */ - protected static $instance; - /** * The config values * @@ -104,25 +97,4 @@ class Config { $this->remove($key); } - - /** - * @return Config - * @throws ErrorException - */ - public static function getInstance() - { - if (!self::$instance instanceof self) { - throw new ErrorException('Config not initialized'); - } - - return self::$instance; - } - - /** - * @param self $instance - */ - public static function setInstance($instance) - { - self::$instance = $instance; - } } 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 @@ +getUri()), '/'); } - - /** - * @return self - * @throws ErrorException - */ - public static function getInstance() - { - if (!self::$instance instanceof self) { - throw new ErrorException('Request not initialized'); - } - - return self::$instance; - } - - /** - * @param self $instance - */ - public static function setInstance($instance) - { - self::$instance = $instance; - } } diff --git a/src/Renderer/Renderer.php b/src/Renderer/Renderer.php index bf3d5609..5ed7cf31 100644 --- a/src/Renderer/Renderer.php +++ b/src/Renderer/Renderer.php @@ -2,13 +2,8 @@ namespace Engelsystem\Renderer; -use ErrorException; - class Renderer { - /** @var self */ - protected static $instance; - /** @var EngineInterface[] */ protected $renderer = []; @@ -29,7 +24,7 @@ class Renderer return $renderer->get($template, $data); } - engelsystem_error('Unable to find a renderer for template file «' . $template . '».'); + engelsystem_error('Unable to find a renderer for template file "' . $template . '".'); return ''; } @@ -42,21 +37,4 @@ class Renderer { $this->renderer[] = $renderer; } - - /** - * @return self - * @throws ErrorException - */ - public static function getInstance() - { - return self::$instance; - } - - /** - * @param self $instance - */ - public static function setInstance($instance) - { - self::$instance = $instance; - } } diff --git a/src/Routing/UrlGenerator.php b/src/Routing/UrlGenerator.php index 8dc464c6..33eef7b0 100644 --- a/src/Routing/UrlGenerator.php +++ b/src/Routing/UrlGenerator.php @@ -2,8 +2,6 @@ namespace Engelsystem\Routing; -use Engelsystem\Http\Request; - class UrlGenerator { /** @@ -14,7 +12,7 @@ class UrlGenerator public static function to($path, $parameters = []) { $path = '/' . ltrim($path, '/'); - $request = Request::getInstance(); + $request = app('request'); $uri = $request->getUriForPath($path); if (!empty($parameters) && is_array($parameters)) { diff --git a/src/helpers.php b/src/helpers.php index 24f93f2c..733b902d 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -2,11 +2,27 @@ // Some useful functions use Engelsystem\Config\Config; +use Engelsystem\Container\Container; use Engelsystem\Http\Request; use Engelsystem\Renderer\Renderer; use Engelsystem\Routing\UrlGenerator; use Symfony\Component\HttpFoundation\Session\SessionInterface; +/** + * Get the global container instance + * + * @param string $id + * @return mixed + */ +function app($id = null) +{ + if (is_null($id)) { + return Container::getInstance(); + } + + return Container::getInstance()->get($id); +} + /** * Get or set config values * @@ -16,15 +32,18 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface; */ function config($key = null, $default = null) { + $config = app('config'); + if (empty($key)) { - return Config::getInstance(); + return $config; } if (is_array($key)) { - Config::getInstance()->set($key); + $config->set($key); + return true; } - return Config::getInstance()->get($key, $default); + return $config->get($key, $default); } /** @@ -34,7 +53,7 @@ function config($key = null, $default = null) */ function request($key = null, $default = null) { - $request = Request::getInstance(); + $request = app('request'); if (is_null($key)) { return $request; @@ -66,7 +85,7 @@ function session($key = null, $default = null) */ function view($template = null, $data = null) { - $renderer = Renderer::getInstance(); + $renderer = app('renderer'); if (is_null($template)) { return $renderer; -- cgit v1.2.3-54-g00ecf