PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.1.0
Defender Security – Malware Scanner, Login Security & Firewall v3.1.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / php-di / invoker / src / CallableResolver.php

CallableResolver.php in Defender Security – Malware Scanner, Login Security & Firewall 3.1.0, at vendor/php-di/invoker/src/CallableResolver.php

134 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Invoker;
4
5 use Invoker\Exception\NotCallableException;
6 use Psr\Container\ContainerInterface;
7 use Psr\Container\NotFoundExceptionInterface;
8
9 /**
10 * Resolves a callable from a container.
11 *
12 * @author Matthieu Napoli <[email protected]>
13 */
14 class CallableResolver
15 {
16 /**
17 * @var ContainerInterface
18 */
19 private $container;
20
21 public function __construct(ContainerInterface $container)
22 {
23 $this->container = $container;
24 }
25
26 /**
27 * Resolve the given callable into a real PHP callable.
28 *
29 * @param callable|string|array $callable
30 *
31 * @return callable Real PHP callable.
32 *
33 * @throws NotCallableException
34 */
35 public function resolve($callable)
36 {
37 if (is_string($callable) && strpos($callable, '::') !== false) {
38 $callable = explode('::', $callable, 2);
39 }
40
41 $callable = $this->resolveFromContainer($callable);
42
43 if (! is_callable($callable)) {
44 throw NotCallableException::fromInvalidCallable($callable, true);
45 }
46
47 return $callable;
48 }
49
50 /**
51 * @param callable|string|array $callable
52 * @return callable
53 * @throws NotCallableException
54 */
55 private function resolveFromContainer($callable)
56 {
57 // Shortcut for a very common use case
58 if ($callable instanceof \Closure) {
59 return $callable;
60 }
61
62 $isStaticCallToNonStaticMethod = false;
63
64 // If it's already a callable there is nothing to do
65 if (is_callable($callable)) {
66 $isStaticCallToNonStaticMethod = $this->isStaticCallToNonStaticMethod($callable);
67 if (! $isStaticCallToNonStaticMethod) {
68 return $callable;
69 }
70 }
71
72 // The callable is a container entry name
73 if (is_string($callable)) {
74 try {
75 return $this->container->get($callable);
76 } catch (NotFoundExceptionInterface $e) {
77 if ($this->container->has($callable)) {
78 throw $e;
79 }
80 throw NotCallableException::fromInvalidCallable($callable, true);
81 }
82 }
83
84 // The callable is an array whose first item is a container entry name
85 // e.g. ['some-container-entry', 'methodToCall']
86 if (is_array($callable) && is_string($callable[0])) {
87 try {
88 // Replace the container entry name by the actual object
89 $callable[0] = $this->container->get($callable[0]);
90 return $callable;
91 } catch (NotFoundExceptionInterface $e) {
92 if ($this->container->has($callable[0])) {
93 throw $e;
94 }
95 if ($isStaticCallToNonStaticMethod) {
96 throw new NotCallableException(sprintf(
97 'Cannot call %s::%s() because %s() is not a static method and "%s" is not a container entry',
98 $callable[0],
99 $callable[1],
100 $callable[1],
101 $callable[0]
102 ));
103 }
104 throw new NotCallableException(sprintf(
105 'Cannot call %s on %s because it is not a class nor a valid container entry',
106 $callable[1],
107 $callable[0]
108 ));
109 }
110 }
111
112 // Unrecognized stuff, we let it fail later
113 return $callable;
114 }
115
116 /**
117 * Check if the callable represents a static call to a non-static method.
118 *
119 * @param mixed $callable
120 * @return bool
121 */
122 private function isStaticCallToNonStaticMethod($callable)
123 {
124 if (is_array($callable) && is_string($callable[0])) {
125 list($class, $method) = $callable;
126 $reflection = new \ReflectionMethod($class, $method);
127
128 return ! $reflection->isStatic();
129 }
130
131 return false;
132 }
133 }
134