PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 7.4.0
WP Popular Posts v7.4.0
4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 5.0.0 5.0.1 5.0.2 5.1.0 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.2.0 6.2.1 6.3.0 6.3.1 6.3.2 6.3.3 6.3.4 6.4.0 6.4.1 6.4.2 7.0.0 7.0.1 7.1.0 7.2.0 7.3.0 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.3.7 7.3.8 7.4.0 trunk 2.3.7 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.2 4.0.3 4.0.5 4.0.6
wordpress-popular-posts / src / Container / Container.php
wordpress-popular-posts / src / Container Last commit date
Container.php 2 years ago ContainerConfigurationInterface.php 5 years ago WordPressPopularPostsConfiguration.php 8 months 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.', esc_html($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