PluginProbe
Elementor Website Builder – more than just a page builder / 3.27.4
Elementor Website Builder – more than just a page builder v3.27.4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / vendor_prefixed / php-di / invoker / src / CallableResolver.php

CallableResolver.php in Elementor Website Builder – more than just a page builder 3.27.4, at vendor_prefixed/php-di/invoker/src/CallableResolver.php

106 lines 3.6 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 namespace ElementorDeps\Invoker;
5
6 use Closure;
7 use ElementorDeps\Invoker\Exception\NotCallableException;
8 use ElementorDeps\Psr\Container\ContainerInterface;
9 use ElementorDeps\Psr\Container\NotFoundExceptionInterface;
10 use ReflectionException;
11 use ReflectionMethod;
12 /**
13 * Resolves a callable from a container.
14 */
15 class CallableResolver
16 {
17 /** @var ContainerInterface */
18 private $container;
19 public function __construct(ContainerInterface $container)
20 {
21 $this->container = $container;
22 }
23 /**
24 * Resolve the given callable into a real PHP callable.
25 *
26 * @param callable|string|array $callable
27 * @return callable Real PHP callable.
28 * @throws NotCallableException|ReflectionException
29 */
30 public function resolve($callable) : callable
31 {
32 if (\is_string($callable) && \strpos($callable, '::') !== \false) {
33 $callable = \explode('::', $callable, 2);
34 }
35 $callable = $this->resolveFromContainer($callable);
36 if (!\is_callable($callable)) {
37 throw NotCallableException::fromInvalidCallable($callable, \true);
38 }
39 return $callable;
40 }
41 /**
42 * @param callable|string|array $callable
43 * @return callable|mixed
44 * @throws NotCallableException|ReflectionException
45 */
46 private function resolveFromContainer($callable)
47 {
48 // Shortcut for a very common use case
49 if ($callable instanceof Closure) {
50 return $callable;
51 }
52 // If it's already a callable there is nothing to do
53 if (\is_callable($callable)) {
54 // TODO with PHP 8 that should not be necessary to check this anymore
55 if (!$this->isStaticCallToNonStaticMethod($callable)) {
56 return $callable;
57 }
58 }
59 // The callable is a container entry name
60 if (\is_string($callable)) {
61 try {
62 return $this->container->get($callable);
63 } catch (NotFoundExceptionInterface $e) {
64 if ($this->container->has($callable)) {
65 throw $e;
66 }
67 throw NotCallableException::fromInvalidCallable($callable, \true);
68 }
69 }
70 // The callable is an array whose first item is a container entry name
71 // e.g. ['some-container-entry', 'methodToCall']
72 if (\is_array($callable) && \is_string($callable[0])) {
73 try {
74 // Replace the container entry name by the actual object
75 $callable[0] = $this->container->get($callable[0]);
76 return $callable;
77 } catch (NotFoundExceptionInterface $e) {
78 if ($this->container->has($callable[0])) {
79 throw $e;
80 }
81 throw new NotCallableException(\sprintf('Cannot call %s() on %s because it is not a class nor a valid container entry', $callable[1], $callable[0]));
82 }
83 }
84 // Unrecognized stuff, we let it fail later
85 return $callable;
86 }
87 /**
88 * Check if the callable represents a static call to a non-static method.
89 *
90 * @param mixed $callable
91 * @throws ReflectionException
92 */
93 private function isStaticCallToNonStaticMethod($callable) : bool
94 {
95 if (\is_array($callable) && \is_string($callable[0])) {
96 [$class, $method] = $callable;
97 if (!\method_exists($class, $method)) {
98 return \false;
99 }
100 $reflection = new ReflectionMethod($class, $method);
101 return !$reflection->isStatic();
102 }
103 return \false;
104 }
105 }
106