Container.php
2 years ago
ContainerConfigurationInterface.php
2 years ago
WordPressPopularPostsConfiguration.php
2 years ago
Container.php
113 lines
| 1 | <?php |
| 2 | /** |
| 3 | * A Simple Dependency Injector Container (DIC). |
| 4 | * |
| 5 | * Largely based on Carl Alexander's Dependency Injection Container. |
| 6 | * @link https://carlalexander.ca/dependency-injection-wordpress/ |
| 7 | */ |
| 8 | |
| 9 | namespace WordPressPopularPosts\Container; |
| 10 | |
| 11 | class Container implements \ArrayAccess |
| 12 | { |
| 13 | /** |
| 14 | * Values stored inside the container. |
| 15 | * |
| 16 | * @var array |
| 17 | */ |
| 18 | private $values; |
| 19 | |
| 20 | /** |
| 21 | * Constructor. |
| 22 | * |
| 23 | * @param array $values |
| 24 | */ |
| 25 | public function __construct(array $values = []) |
| 26 | { |
| 27 | $this->values = $values; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Configure the container using the given container configuration objects. |
| 32 | * |
| 33 | * @param array $configurations |
| 34 | */ |
| 35 | public function configure(array $configurations) |
| 36 | { |
| 37 | foreach ($configurations as $configuration) { |
| 38 | if ( $configuration instanceof ContainerConfigurationInterface ) { |
| 39 | $configuration->modify($this); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Checks if there's a value in the container for the given key. |
| 46 | * |
| 47 | * @param mixed $key |
| 48 | * |
| 49 | * @return bool |
| 50 | */ |
| 51 | public function offsetExists($key) : bool /** @TODO: starting PHP 8.0 $key can be declared as mixed $key, see https://www.php.net/manual/en/language.types.declarations.php */ |
| 52 | { |
| 53 | return array_key_exists($key, $this->values); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Sets a value inside of the container. |
| 58 | * |
| 59 | * @param mixed $key |
| 60 | * @param mixed $value |
| 61 | */ |
| 62 | public function offsetSet($key, $value) : void /** @TODO: starting PHP 8.0 $key and $value can be declared as mixed $key, mixed $value */ |
| 63 | { |
| 64 | $this->values[$key] = $value; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Unset the value in the container for the given key. |
| 69 | * |
| 70 | * @param mixed $key |
| 71 | */ |
| 72 | public function offsetUnset($key) : void /** @TODO: starting PHP 8.0 $key can be declared as mixed $key */ |
| 73 | { |
| 74 | unset($this->values[$key]); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get a value from the container. |
| 79 | * |
| 80 | * @param mixed $key |
| 81 | * |
| 82 | * @return mixed |
| 83 | */ |
| 84 | #[\ReturnTypeWillChange] |
| 85 | public function offsetGet($key) /** @TODO: starting PHP 8.0 $key can be declared as mixed $key */ |
| 86 | { |
| 87 | if ( ! $this->offsetExists($key) ) { |
| 88 | throw new \InvalidArgumentException(sprintf('Container doesn\'t have a value stored for the "%s" key.', $key)); |
| 89 | } |
| 90 | return $this->values[$key] instanceof \Closure ? $this->values[$key]($this) : $this->values[$key]; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Creates a closure used for creating a service using the given callable. |
| 95 | * |
| 96 | * @param \Closure $closure |
| 97 | * |
| 98 | * @return \Closure |
| 99 | */ |
| 100 | public function service(\Closure $closure) |
| 101 | { |
| 102 | return function(Container $container) use ($closure) { |
| 103 | static $object; |
| 104 | |
| 105 | if ( null === $object ) { |
| 106 | $object = $closure($container); |
| 107 | } |
| 108 | |
| 109 | return $object; |
| 110 | }; |
| 111 | } |
| 112 | } |
| 113 |