PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
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 / ReflectionContainer.php

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

108 lines 3.2 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\Argument\{ArgumentResolverInterface, ArgumentResolverTrait};
8 use Imagify\Dependencies\League\Container\Exception\ContainerException;
9 use Imagify\Dependencies\League\Container\Exception\NotFoundException;
10 use Imagify\Dependencies\Psr\Container\ContainerInterface;
11 use ReflectionClass;
12 use ReflectionFunction;
13 use ReflectionMethod;
14
15 class ReflectionContainer implements ArgumentResolverInterface, ContainerInterface
16 {
17 use ArgumentResolverTrait;
18 use ContainerAwareTrait;
19
20 /**
21 * @var boolean
22 */
23 protected $cacheResolutions;
24
25 /**
26 * @var array
27 */
28 protected $cache = [];
29
30 public function __construct(bool $cacheResolutions = false)
31 {
32 $this->cacheResolutions = $cacheResolutions;
33 }
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 : $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 public function has($id): bool
69 {
70 return class_exists($id);
71 }
72
73 public function call(callable $callable, array $args = [])
74 {
75 if (is_string($callable) && strpos($callable, '::') !== false) {
76 $callable = explode('::', $callable);
77 }
78
79 if (is_array($callable)) {
80 if (is_string($callable[0])) {
81 // if we have a definition container, try that first, otherwise, reflect
82 try {
83 $callable[0] = $this->getContainer()->get($callable[0]);
84 } catch (ContainerException $e) {
85 $callable[0] = $this->get($callable[0]);
86 }
87 }
88
89 $reflection = new ReflectionMethod($callable[0], $callable[1]);
90
91 if ($reflection->isStatic()) {
92 $callable[0] = null;
93 }
94
95 return $reflection->invokeArgs($callable[0], $this->reflectArguments($reflection, $args));
96 }
97
98 if (is_object($callable)) {
99 $reflection = new ReflectionMethod($callable, '__invoke');
100 return $reflection->invokeArgs($callable, $this->reflectArguments($reflection, $args));
101 }
102
103 $reflection = new ReflectionFunction(\Closure::fromCallable($callable));
104
105 return $reflection->invokeArgs($this->reflectArguments($reflection, $args));
106 }
107 }
108