1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Proxy; |
4
|
|
|
|
5
|
|
|
use Anax\DI\Exception\NotFoundException; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Base class and template for Proxy\DI classes. Each $di service will be |
10
|
|
|
* represented by an subclass of this class. This class is the base and the |
11
|
|
|
* target base class is created with the autoloader of ProxyDIFactory. The |
12
|
|
|
* target class holds the name of the $di service it connects to, below the |
13
|
|
|
* namespace Anax\Proxy\DI\Service. As an example the $di->get("request") |
14
|
|
|
* could be represented as Anax\Proxy\DI\Request. |
15
|
|
|
*/ |
16
|
|
|
class ProxyDI |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var object $di The service container. |
20
|
|
|
*/ |
21
|
|
|
private static $di = null; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Inject $di and make it accessable for static proxy access. |
27
|
|
|
* |
28
|
|
|
* @param Psr\Container\ContainerInterface $di The service container |
|
|
|
|
29
|
|
|
* holding framework |
30
|
|
|
* services. |
31
|
|
|
* |
32
|
|
|
* @return void. |
|
|
|
|
33
|
|
|
*/ |
34
|
4 |
|
public static function setDI(ContainerInterface $di) : void |
35
|
|
|
{ |
36
|
4 |
|
self::$di = $di; |
37
|
4 |
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Catch all calls to static methods and forward them as actual |
43
|
|
|
* calls to the $di container. |
44
|
|
|
* |
45
|
|
|
* @param string $name The name of the static method called. |
46
|
|
|
* @param array $arguments The arguments sent to the method. |
47
|
|
|
* |
48
|
|
|
* @throws ProxyException when failing to forward the call to the |
49
|
|
|
* method in the $di service. |
50
|
|
|
* |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
3 |
|
public static function __callStatic($name, $arguments) |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
3 |
|
$serviceName = static::getServiceName(); |
|
|
|
|
57
|
3 |
|
$serviceObject = self::$di->get($serviceName); |
58
|
1 |
|
} catch (NotFoundException $e) { |
59
|
1 |
|
throw new ProxyException("The Proxy is trying to reach service '$serviceName' but it is not loaded as a service in \$di."); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
if (!is_callable([$serviceObject, $name])) { |
63
|
1 |
|
throw new ProxyException("The Proxy is trying to call method '$name' on service '$serviceName' but that method is not callable."); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return call_user_func_array([$serviceObject, $name], $arguments); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|