PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / Dependencies / League / Container / Container.php

Container.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.2, at classes/Dependencies/League/Container/Container.php

249 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2
3 namespace Imagify\Dependencies\League\Container;
4
5 use Imagify\Dependencies\League\Container\Definition\{DefinitionAggregate, DefinitionInterface, DefinitionAggregateInterface};
6 use Imagify\Dependencies\League\Container\Exception\{NotFoundException, ContainerException};
7 use Imagify\Dependencies\League\Container\Inflector\{InflectorAggregate, InflectorInterface, InflectorAggregateInterface};
8 use Imagify\Dependencies\League\Container\ServiceProvider\{
9 ServiceProviderAggregate,
10 ServiceProviderAggregateInterface,
11 ServiceProviderInterface
12 };
13 use Imagify\Dependencies\Psr\Container\ContainerInterface;
14
15 class Container implements ContainerInterface
16 {
17 /**
18 * @var boolean
19 */
20 protected $defaultToShared = false;
21
22 /**
23 * @var DefinitionAggregateInterface
24 */
25 protected $definitions;
26
27 /**
28 * @var ServiceProviderAggregateInterface
29 */
30 protected $providers;
31
32 /**
33 * @var InflectorAggregateInterface
34 */
35 protected $inflectors;
36
37 /**
38 * @var ContainerInterface[]
39 */
40 protected $delegates = [];
41
42 /**
43 * Construct.
44 *
45 * @param DefinitionAggregateInterface|null $definitions
46 * @param ServiceProviderAggregateInterface|null $providers
47 * @param InflectorAggregateInterface|null $inflectors
48 */
49 public function __construct(
50 DefinitionAggregateInterface $definitions = null,
51 ServiceProviderAggregateInterface $providers = null,
52 InflectorAggregateInterface $inflectors = null
53 ) {
54 $this->definitions = $definitions ?? new DefinitionAggregate;
55 $this->providers = $providers ?? new ServiceProviderAggregate;
56 $this->inflectors = $inflectors ?? new InflectorAggregate;
57
58 if ($this->definitions instanceof ContainerAwareInterface) {
59 $this->definitions->setLeagueContainer($this);
60 }
61
62 if ($this->providers instanceof ContainerAwareInterface) {
63 $this->providers->setLeagueContainer($this);
64 }
65
66 if ($this->inflectors instanceof ContainerAwareInterface) {
67 $this->inflectors->setLeagueContainer($this);
68 }
69 }
70
71 /**
72 * Add an item to the container.
73 *
74 * @param string $id
75 * @param mixed $concrete
76 * @param boolean $shared
77 *
78 * @return DefinitionInterface
79 */
80 public function add(string $id, $concrete = null, bool $shared = null) : DefinitionInterface
81 {
82 $concrete = $concrete ?? $id;
83 $shared = $shared ?? $this->defaultToShared;
84
85 return $this->definitions->add($id, $concrete, $shared);
86 }
87
88 /**
89 * Proxy to add with shared as true.
90 *
91 * @param string $id
92 * @param mixed $concrete
93 *
94 * @return DefinitionInterface
95 */
96 public function share(string $id, $concrete = null) : DefinitionInterface
97 {
98 return $this->add($id, $concrete, true);
99 }
100
101 /**
102 * Whether the container should default to defining shared definitions.
103 *
104 * @param boolean $shared
105 *
106 * @return self
107 */
108 public function defaultToShared(bool $shared = true) : ContainerInterface
109 {
110 $this->defaultToShared = $shared;
111
112 return $this;
113 }
114
115 /**
116 * Get a definition to extend.
117 *
118 * @param string $id [description]
119 *
120 * @return DefinitionInterface
121 */
122 public function extend(string $id) : DefinitionInterface
123 {
124 if ($this->providers->provides($id)) {
125 $this->providers->register($id);
126 }
127
128 if ($this->definitions->has($id)) {
129 return $this->definitions->getDefinition($id);
130 }
131
132 throw new NotFoundException(
133 sprintf('Unable to extend alias (%s) as it is not being managed as a definition', $id)
134 );
135 }
136
137 /**
138 * Add a service provider.
139 *
140 * @param ServiceProviderInterface|string $provider
141 *
142 * @return self
143 */
144 public function addServiceProvider($provider) : self
145 {
146 $this->providers->add($provider);
147
148 return $this;
149 }
150
151 /**
152 * {@inheritdoc}
153 */
154 public function get($id, bool $new = false)
155 {
156 if ($this->definitions->has($id)) {
157 $resolved = $this->definitions->resolve($id, $new);
158 return $this->inflectors->inflect($resolved);
159 }
160
161 if ($this->definitions->hasTag($id)) {
162 $arrayOf = $this->definitions->resolveTagged($id, $new);
163
164 array_walk($arrayOf, function (&$resolved) {
165 $resolved = $this->inflectors->inflect($resolved);
166 });
167
168 return $arrayOf;
169 }
170
171 if ($this->providers->provides($id)) {
172 $this->providers->register($id);
173
174 if (!$this->definitions->has($id) && !$this->definitions->hasTag($id)) {
175 throw new ContainerException(sprintf('Service provider lied about providing (%s) service', $id));
176 }
177
178 return $this->get($id, $new);
179 }
180
181 foreach ($this->delegates as $delegate) {
182 if ($delegate->has($id)) {
183 $resolved = $delegate->get($id);
184 return $this->inflectors->inflect($resolved);
185 }
186 }
187
188 throw new NotFoundException(sprintf('Alias (%s) is not being managed by the container or delegates', $id));
189 }
190
191 /**
192 * {@inheritdoc}
193 */
194 public function has($id)
195 {
196 if ($this->definitions->has($id)) {
197 return true;
198 }
199
200 if ($this->definitions->hasTag($id)) {
201 return true;
202 }
203
204 if ($this->providers->provides($id)) {
205 return true;
206 }
207
208 foreach ($this->delegates as $delegate) {
209 if ($delegate->has($id)) {
210 return true;
211 }
212 }
213
214 return false;
215 }
216
217 /**
218 * Allows for manipulation of specific types on resolution.
219 *
220 * @param string $type
221 * @param callable|null $callback
222 *
223 * @return InflectorInterface
224 */
225 public function inflector(string $type, callable $callback = null) : InflectorInterface
226 {
227 return $this->inflectors->add($type, $callback);
228 }
229
230 /**
231 * Delegate a backup container to be checked for services if it
232 * cannot be resolved via this container.
233 *
234 * @param ContainerInterface $container
235 *
236 * @return self
237 */
238 public function delegate(ContainerInterface $container) : self
239 {
240 $this->delegates[] = $container;
241
242 if ($container instanceof ContainerAwareInterface) {
243 $container->setLeagueContainer($this);
244 }
245
246 return $this;
247 }
248 }
249