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 / ReflectionContainer.php

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

132 lines 3.5 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\Argument\{ArgumentResolverInterface, ArgumentResolverTrait};
6 use Imagify\Dependencies\League\Container\Exception\NotFoundException;
7 use Imagify\Dependencies\Psr\Container\ContainerInterface;
8 use ReflectionClass;
9 use ReflectionException;
10 use ReflectionFunction;
11 use ReflectionMethod;
12
13 class ReflectionContainer implements ArgumentResolverInterface, ContainerInterface
14 {
15 use ArgumentResolverTrait;
16 use ContainerAwareTrait;
17
18 /**
19 * @var boolean
20 */
21 protected $cacheResolutions = false;
22
23 /**
24 * Cache of resolutions.
25 *
26 * @var array
27 */
28 protected $cache = [];
29
30 /**
31 * {@inheritdoc}
32 *
33 * @throws ReflectionException
34 */
35 public function get($id, array $args = [])
36 {
37 if ($this->cacheResolutions === true && array_key_exists($id, $this->cache)) {
38 return $this->cache[$id];
39 }
40
41 if (! $this->has($id)) {
42 throw new NotFoundException(
43 sprintf('Alias (%s) is not an existing class and therefore cannot be resolved', $id)
44 );
45 }
46
47 $reflector = new ReflectionClass($id);
48 $construct = $reflector->getConstructor();
49
50 if ($construct && !$construct->isPublic()) {
51 throw new NotFoundException(
52 sprintf('Alias (%s) has a non-public constructor and therefore cannot be instantiated', $id)
53 );
54 }
55
56 $resolution = $construct === null
57 ? new $id
58 : $resolution = $reflector->newInstanceArgs($this->reflectArguments($construct, $args))
59 ;
60
61 if ($this->cacheResolutions === true) {
62 $this->cache[$id] = $resolution;
63 }
64
65 return $resolution;
66 }
67
68 /**
69 * {@inheritdoc}
70 */
71 public function has($id)
72 {
73 return class_exists($id);
74 }
75
76 /**
77 * Invoke a callable via the container.
78 *
79 * @param callable $callable
80 * @param array $args
81 *
82 * @return mixed
83 *
84 * @throws ReflectionException
85 */
86 public function call(callable $callable, array $args = [])
87 {
88 if (is_string($callable) && strpos($callable, '::') !== false) {
89 $callable = explode('::', $callable);
90 }
91
92 if (is_array($callable)) {
93 if (is_string($callable[0])) {
94 $callable[0] = $this->getContainer()->get($callable[0]);
95 }
96
97 $reflection = new ReflectionMethod($callable[0], $callable[1]);
98
99 if ($reflection->isStatic()) {
100 $callable[0] = null;
101 }
102
103 return $reflection->invokeArgs($callable[0], $this->reflectArguments($reflection, $args));
104 }
105
106 if (is_object($callable)) {
107 $reflection = new ReflectionMethod($callable, '__invoke');
108
109 return $reflection->invokeArgs($callable, $this->reflectArguments($reflection, $args));
110 }
111
112 $reflection = new ReflectionFunction(\Closure::fromCallable($callable));
113
114 return $reflection->invokeArgs($this->reflectArguments($reflection, $args));
115 }
116
117 /**
118 * Whether the container should default to caching resolutions and returning
119 * the cache on following calls.
120 *
121 * @param boolean $option
122 *
123 * @return self
124 */
125 public function cacheResolutions(bool $option = true) : ContainerInterface
126 {
127 $this->cacheResolutions = $option;
128
129 return $this;
130 }
131 }
132