|
1 | 1 | <?php |
2 | 2 | namespace SplitIO\Component\Common; |
3 | 3 |
|
4 | | -use Psr\Log\LoggerInterface; |
5 | | -use SplitIO\Component\Cache\Pool; |
| 4 | +use SplitIO\Component\Log\Logger; |
6 | 5 |
|
7 | 6 | /** |
8 | 7 | * Class Di |
9 | 8 | * @package SplitIO\Common |
10 | 9 | */ |
11 | 10 | class Di |
12 | 11 | { |
13 | | - const KEY_LOG = 'SPLIT-LOGGER'; |
| 12 | + private \SplitIO\Component\Log\Logger $logger; |
14 | 13 |
|
15 | | - const KEY_FACTORY_TRACKER = 'FACTORY-TRACKER'; |
| 14 | + private int $factoryTracker = 0; |
| 15 | + |
| 16 | + private string $ipAddress = ""; |
16 | 17 |
|
17 | 18 | /** |
18 | 19 | * @var Singleton The reference to *Singleton* instance of this class |
19 | 20 | */ |
20 | 21 | private static $instance; |
21 | 22 |
|
22 | | - /** |
23 | | - * @var array |
24 | | - */ |
25 | | - private $container = array(); |
26 | | - |
27 | 23 | /** |
28 | 24 | * Returns the *Singleton* instance of this class. |
29 | 25 | * |
@@ -67,57 +63,54 @@ public function __wakeup() |
67 | 63 | } |
68 | 64 |
|
69 | 65 | /** |
70 | | - * @param $key |
71 | | - * @param $instance |
| 66 | + * @return int |
72 | 67 | */ |
73 | | - private function setKey($key, $instance) |
| 68 | + public static function trackFactory() |
74 | 69 | { |
75 | | - $this->container[$key] = $instance; |
| 70 | + self::getInstance()->factoryTracker += 1; |
| 71 | + return self::getInstance()->factoryTracker; |
76 | 72 | } |
77 | 73 |
|
78 | 74 | /** |
79 | | - * @param $key |
80 | | - * @return mixed |
| 75 | + * @param \SplitIO\Component\Log\Logger $logger |
81 | 76 | */ |
82 | | - private function getKey($key) |
| 77 | + public static function setLogger(Logger $logger) |
83 | 78 | { |
84 | | - return (isset($this->container[$key])) ? $this->container[$key] : null; |
85 | | - } |
86 | | - |
87 | | - /** |
88 | | - * Set an object instance with its key |
89 | | - * @param $key |
90 | | - * @param $instance |
91 | | - */ |
92 | | - public static function set($key, $instance) |
93 | | - { |
94 | | - self::getInstance()->setKey($key, $instance); |
| 79 | + if (!isset(self::getInstance()->logger)) { |
| 80 | + self::getInstance()->logger = $logger; |
| 81 | + return; |
| 82 | + } |
| 83 | + self::getInstance()->logger->debug("logger was set before, ignoring new instance provided"); |
95 | 84 | } |
96 | 85 |
|
97 | 86 | /** |
98 | | - * Given a key returns the object instance associated with this. |
99 | | - * @param $key |
100 | | - * @return mixed |
| 87 | + * @return \SplitIO\Component\Log\Logger |
101 | 88 | */ |
102 | | - public static function get($key) |
| 89 | + public static function getLogger() |
103 | 90 | { |
104 | | - return self::getInstance()->getKey($key); |
| 91 | + if (!isset(self::getInstance()->logger)) { |
| 92 | + throw new Exception("logger was not set yet"); |
| 93 | + } |
| 94 | + return self::getInstance()->logger; |
105 | 95 | } |
106 | 96 |
|
107 | | - |
108 | 97 | /** |
109 | | - * @param LoggerInterface $logger |
| 98 | + * @param string $ip |
110 | 99 | */ |
111 | | - public static function setLogger($logger) |
| 100 | + public static function setIPAddress(string $ip) |
112 | 101 | { |
113 | | - self::set(self::KEY_LOG, $logger); |
| 102 | + if (empty(self::getInstance()->ipAddress)) { |
| 103 | + self::getInstance()->ipAddress = $ip; |
| 104 | + return; |
| 105 | + } |
| 106 | + self::getInstance()->getLogger()->debug("IPAddress was set before, ignoring new instance provided"); |
114 | 107 | } |
115 | 108 |
|
116 | 109 | /** |
117 | | - * @return null|\Psr\Log\LoggerInterface |
| 110 | + * @return string |
118 | 111 | */ |
119 | | - public static function getLogger() |
| 112 | + public static function getIPAddress() |
120 | 113 | { |
121 | | - return self::get(self::KEY_LOG); |
| 114 | + return self::getInstance()->ipAddress; |
122 | 115 | } |
123 | 116 | } |
0 commit comments