Advertisement
Guest User

Untitled

a guest
Nov 26th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.44 KB | None | 0 0
  1. <?php
  2. declare(strict_types=1);
  3.  
  4. namespace Container;
  5.  
  6. use Container\Exception\AlreadyExistsException;
  7. use Container\Exception\NotFoundException;
  8. use Psr\Container\ContainerInterface;
  9.  
  10. final class Container implements ContainerInterface
  11. {
  12.     /**
  13.      * @var array
  14.      */
  15.     private $entries;
  16.  
  17.     /**
  18.      * @var array
  19.      */
  20.     private $lazyEntries;
  21.  
  22.     public function __construct()
  23.     {
  24.         $this->entries = [];
  25.         $this->lazyEntries = [];
  26.     }
  27.  
  28.     /**
  29.      * @inheritdoc
  30.      */
  31.     public function get($id)
  32.     {
  33.         if (!$this->has($id)) {
  34.             throw new NotFoundException(sprintf('No entry was found for "%s" identifier.', $id));
  35.         }
  36.  
  37.         if (array_key_exists($id, $this->lazyEntries)) {
  38.             $factory = $this->lazyEntries[$id];
  39.             unset($this->lazyEntries[$id]);
  40.             $this->entries[$id] = $factory($this);
  41.         }
  42.  
  43.         return $this->entries[$id];
  44.     }
  45.  
  46.     /**
  47.      * @inheritdoc
  48.      */
  49.     public function has($id)
  50.     {
  51.         return array_key_exists($id, $this->entries) || array_key_exists($id, $this->lazyEntries);
  52.     }
  53.  
  54.     /**
  55.      * Add entry to container
  56.      *
  57.      * @param string $id
  58.      * @param mixed $entry
  59.      *
  60.      * @throws AlreadyExistsException Entry for **this** identifier already exists
  61.      */
  62.     public function add($id, $entry)
  63.     {
  64.         if ($this->has($id)) {
  65.             throw new AlreadyExistsException(sprintf('Entry for "%s" identifier already exists', $id));
  66.         }
  67.  
  68.         $this->entries[$id] = $entry;
  69.     }
  70.  
  71.     /**
  72.      * Add lazy entry to container
  73.      *
  74.      * @param string $id
  75.      * @param callable $factory
  76.      *
  77.      * @throws AlreadyExistsException Entry for **this** identifier already exists
  78.      */
  79.     public function addLazy($id, callable $factory)
  80.     {
  81.         if ($this->has($id)) {
  82.             throw new AlreadyExistsException(sprintf('Entry for "%s" identifier already exists', $id));
  83.         }
  84.  
  85.         $this->lazyEntries[$id] = $factory;
  86.     }
  87.  
  88.     /**
  89.      * Remove entry
  90.      *
  91.      * @param string $id
  92.      *
  93.      * @throws NotFoundException No entry was found for **this** identifier
  94.      */
  95.     public function remove($id)
  96.     {
  97.         if (!$this->has($id)) {
  98.             throw new NotFoundException(sprintf('No entry was found for "%s" identifier', $id));
  99.         }
  100.  
  101.         unset($this->entries[$id], $this->lazyEntries[$id]);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement