1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Autarky\Container\Proxy;
12:
13: use Autarky\Container\ContainerInterface;
14:
15: abstract class AbstractProxy
16: {
17: 18: 19:
20: protected static $instances = [];
21:
22: 23: 24:
25: protected static $container;
26:
27: 28: 29:
30: private function __construct()
31: {
32:
33: }
34:
35: public static function setProxyContainer(ContainerInterface $container = null)
36: {
37: static::$instances = [];
38: static::$container = $container;
39: }
40:
41: public static function setProxyInstance($instance = null)
42: {
43: static::$instances[static::getProxyContainerKey()] = $instance;
44: }
45:
46: protected static function resolveProxyInstance()
47: {
48: return static::$container->resolve(static::getProxyContainerKey());
49: }
50:
51: public static function __callStatic($method, array $args)
52: {
53: $key = static::getProxyContainerKey();
54:
55: if (!array_key_exists($key, static::$instances)) {
56: static::$instances[$key] = static::resolveProxyInstance();
57: }
58:
59: return call_user_func_array([static::$instances[$key], $method], $args);
60: }
61:
62: 63: 64:
65: protected static function getProxyContainerKey()
66: {
67:
68: throw new \RuntimeException('Method '.__FUNCTION__.' must be implemented.');
69: }
70: }
71: