PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 6.0.1
WP Popular Posts v6.0.1
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 3 years ago ContainerConfigurationInterface.php 3 years ago WordPressPopularPostsConfiguration.php 3 years ago
Container.php
111 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 * Checks if there's a value in the container for the given key.
45 *
46 * @param mixed $key
47 *
48 * @return bool
49 */
50 public function offsetExists($key) /** @TODO: starting PHP 8.0 $key can be declared as mixed $key, see https://www.php.net/manual/en/language.types.declarations.php */
51 {
52 return array_key_exists($key, $this->values);
53 }
54
55 /**
56 * Sets a value inside of the container.
57 *
58 * @param mixed $key
59 * @param mixed $value
60 */
61 public function offsetSet($key, $value) /** @TODO: starting PHP 8.0 $key and $value can be declared as mixed $key, mixed $value */
62 {
63 $this->values[$key] = $value;
64 }
65
66 /**
67 * Unset the value in the container for the given key.
68 *
69 * @param mixed $key
70 */
71 public function offsetUnset($key) /** @TODO: starting PHP 8.0 $key can be declared as mixed $key */
72 {
73 unset($this->values[$key]);
74 }
75
76 /**
77 * Get a value from the container.
78 *
79 * @param mixed $key
80 *
81 * @return mixed
82 */
83 public function offsetGet($key) /** @TODO: starting PHP 8.0 $key can be declared as mixed $key */
84 {
85 if ( ! $this->offsetExists($key) ) {
86 throw new \InvalidArgumentException(sprintf('Container doesn\'t have a value stored for the "%s" key.', $key));
87 }
88 return $this->values[$key] instanceof \Closure ? $this->values[$key]($this) : $this->values[$key];
89 }
90
91 /**
92 * Creates a closure used for creating a service using the given callable.
93 *
94 * @param \Closure $closure
95 *
96 * @return \Closure
97 */
98 public function service(\Closure $closure)
99 {
100 return function(Container $container) use ($closure) {
101 static $object;
102
103 if ( null === $object ) {
104 $object = $closure($container);
105 }
106
107 return $object;
108 };
109 }
110 }
111