|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of DivineNii opensource projects. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.4 and above required |
|
9
|
|
|
* |
|
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
11
|
|
|
* @copyright 2021 DivineNii (https://divinenii.com/) |
|
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Rade\DI\Facade; |
|
19
|
|
|
|
|
20
|
|
|
use Psr\Container\ContainerInterface; |
|
21
|
|
|
use Rade\DI\{Container, Exceptions\ContainerResolutionException}; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Represents a Static Proxy logic using `__callStatic()`. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class Facade |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var array<string,string> |
|
32
|
|
|
* |
|
33
|
|
|
* @internal Do not use this property directly. |
|
34
|
|
|
*/ |
|
35
|
|
|
public static array $proxies = []; |
|
36
|
|
|
|
|
37
|
|
|
protected static ContainerInterface $container; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Sets the Container that will be used to retrieve the Proxies. |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public static function setContainer(ContainerInterface $container): void |
|
43
|
|
|
{ |
|
44
|
1 |
|
static::$container = $container; |
|
45
|
1 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Performs the proxying of the statically called method from the container. |
|
49
|
|
|
* |
|
50
|
|
|
* @param array<int|string,mixed> $arguments |
|
51
|
|
|
* |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public static function __callStatic(string $name, array $arguments = []) |
|
55
|
|
|
{ |
|
56
|
1 |
|
if (!\array_key_exists($name, self::$proxies)) { |
|
57
|
1 |
|
throw new ContainerResolutionException(\sprintf('Subject "%s" is not a supported proxy service.', $name)); |
|
58
|
|
|
} |
|
59
|
1 |
|
$di = self::$container; |
|
60
|
|
|
|
|
61
|
1 |
|
if (\is_callable($service = $di->get(self::$proxies[$name]))) { |
|
62
|
1 |
|
return $di instanceof Container ? $di->call($service, $arguments) : \call_user_func_array($service, $arguments); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
return $service; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|