PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.0
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.0
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 / vendor / league / container / src / Container.php

Container.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.0, at vendor/league/container/src/Container.php

211 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Imagify\Dependencies\League\Container;
6
7 use Imagify\Dependencies\League\Container\Definition\{DefinitionAggregate, DefinitionInterface, DefinitionAggregateInterface};
8 use Imagify\Dependencies\League\Container\Exception\{NotFoundException, ContainerException};
9 use Imagify\Dependencies\League\Container\Inflector\{InflectorAggregate, InflectorInterface, InflectorAggregateInterface};
10 use Imagify\Dependencies\League\Container\ServiceProvider\{ServiceProviderAggregate,
11 ServiceProviderAggregateInterface,
12 ServiceProviderInterface};
13 use Imagify\Dependencies\Psr\Container\ContainerInterface;
14
15 class Container implements DefinitionContainerInterface
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 public function __construct(
43 ?DefinitionAggregateInterface $definitions = null,
44 ?ServiceProviderAggregateInterface $providers = null,
45 ?InflectorAggregateInterface $inflectors = null
46 ) {
47 $this->definitions = $definitions ?? new DefinitionAggregate();
48 $this->providers = $providers ?? new ServiceProviderAggregate();
49 $this->inflectors = $inflectors ?? new InflectorAggregate();
50
51 if ($this->definitions instanceof ContainerAwareInterface) {
52 $this->definitions->setContainer($this);
53 }
54
55 if ($this->providers instanceof ContainerAwareInterface) {
56 $this->providers->setContainer($this);
57 }
58
59 if ($this->inflectors instanceof ContainerAwareInterface) {
60 $this->inflectors->setContainer($this);
61 }
62 }
63
64 public function add(string $id, $concrete = null): DefinitionInterface
65 {
66 $concrete = $concrete ?? $id;
67
68 if (true === $this->defaultToShared) {
69 return $this->addShared($id, $concrete);
70 }
71
72 return $this->definitions->add($id, $concrete);
73 }
74
75 public function addShared(string $id, $concrete = null): DefinitionInterface
76 {
77 $concrete = $concrete ?? $id;
78 return $this->definitions->addShared($id, $concrete);
79 }
80
81 public function defaultToShared(bool $shared = true): ContainerInterface
82 {
83 $this->defaultToShared = $shared;
84 return $this;
85 }
86
87 public function extend(string $id): DefinitionInterface
88 {
89 if ($this->providers->provides($id)) {
90 $this->providers->register($id);
91 }
92
93 if ($this->definitions->has($id)) {
94 return $this->definitions->getDefinition($id);
95 }
96
97 throw new NotFoundException(sprintf(
98 'Unable to extend alias (%s) as it is not being managed as a definition',
99 $id
100 ));
101 }
102
103 public function addServiceProvider(ServiceProviderInterface $provider): DefinitionContainerInterface
104 {
105 $this->providers->add($provider);
106 return $this;
107 }
108
109 /**
110 * @template RequestedType
111 *
112 * @param class-string<RequestedType>|string $id
113 *
114 * @return RequestedType|mixed
115 */
116 public function get($id)
117 {
118 return $this->resolve($id);
119 }
120
121 /**
122 * @template RequestedType
123 *
124 * @param class-string<RequestedType>|string $id
125 *
126 * @return RequestedType|mixed
127 */
128 public function getNew($id)
129 {
130 return $this->resolve($id, true);
131 }
132
133 public function has($id): bool
134 {
135 if ($this->definitions->has($id)) {
136 return true;
137 }
138
139 if ($this->definitions->hasTag($id)) {
140 return true;
141 }
142
143 if ($this->providers->provides($id)) {
144 return true;
145 }
146
147 foreach ($this->delegates as $delegate) {
148 if ($delegate->has($id)) {
149 return true;
150 }
151 }
152
153 return false;
154 }
155
156 public function inflector(string $type, ?callable $callback = null): InflectorInterface
157 {
158 return $this->inflectors->add($type, $callback);
159 }
160
161 public function delegate(ContainerInterface $container): self
162 {
163 $this->delegates[] = $container;
164
165 if ($container instanceof ContainerAwareInterface) {
166 $container->setContainer($this);
167 }
168
169 return $this;
170 }
171
172 protected function resolve($id, bool $new = false)
173 {
174 if ($this->definitions->has($id)) {
175 $resolved = (true === $new) ? $this->definitions->resolveNew($id) : $this->definitions->resolve($id);
176 return $this->inflectors->inflect($resolved);
177 }
178
179 if ($this->definitions->hasTag($id)) {
180 $arrayOf = (true === $new)
181 ? $this->definitions->resolveTaggedNew($id)
182 : $this->definitions->resolveTagged($id);
183
184 array_walk($arrayOf, function (&$resolved) {
185 $resolved = $this->inflectors->inflect($resolved);
186 });
187
188 return $arrayOf;
189 }
190
191 if ($this->providers->provides($id)) {
192 $this->providers->register($id);
193
194 if (!$this->definitions->has($id) && !$this->definitions->hasTag($id)) {
195 throw new ContainerException(sprintf('Service provider lied about providing (%s) service', $id));
196 }
197
198 return $this->resolve($id, $new);
199 }
200
201 foreach ($this->delegates as $delegate) {
202 if ($delegate->has($id)) {
203 $resolved = $delegate->get($id);
204 return $this->inflectors->inflect($resolved);
205 }
206 }
207
208 throw new NotFoundException(sprintf('Alias (%s) is not being managed by the container or delegates', $id));
209 }
210 }
211