diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2017-08-31 17:30:54 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2017-08-31 17:30:54 +0200 |
commit | 8c81adc8e83969e90b4c54daf4a396b1094134ff (patch) | |
tree | ae5b7e5bd826a9287d8fa34627d5b67c58e6fb22 /src/Container | |
parent | 0a20883aa862779b48fd2a297456c2db04cffb95 (diff) |
Implemented container
Diffstat (limited to 'src/Container')
-rw-r--r-- | src/Container/Container.php | 105 | ||||
-rw-r--r-- | src/Container/ContainerException.php | 11 | ||||
-rw-r--r-- | src/Container/NotFoundException.php | 10 |
3 files changed, 126 insertions, 0 deletions
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 @@ +<?php + +namespace Engelsystem\Container; + +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; + +class Container implements ContainerInterface +{ + /** + * The globally available container + * + * @var static + */ + protected static $instance; + + /** + * Contains the shared instances + * + * @var mixed[] + */ + protected $instances = []; + + /** + * Finds an entry of the container by its identifier and returns it + * + * @param string $id Identifier of the entry to look for + * + * @throws NotFoundExceptionInterface No entry was found for **this** identifier + * @throws ContainerExceptionInterface Error while retrieving the entry + * + * @return mixed Entry + */ + public function get($id) + { + if ($this->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 @@ +<?php + +namespace Engelsystem\Container; + +use Exception; +use Psr\Container\ContainerExceptionInterface; + +class ContainerException extends Exception implements ContainerExceptionInterface +{ + +} diff --git a/src/Container/NotFoundException.php b/src/Container/NotFoundException.php new file mode 100644 index 00000000..a83be0b1 --- /dev/null +++ b/src/Container/NotFoundException.php @@ -0,0 +1,10 @@ +<?php + +namespace Engelsystem\Container; + +use Psr\Container\NotFoundExceptionInterface; + +class NotFoundException extends ContainerException implements NotFoundExceptionInterface +{ + +} |