Skip to content

Commit f7a8d13

Browse files
committed
updated di logger
1 parent 5c7e754 commit f7a8d13

File tree

12 files changed

+55
-66
lines changed

12 files changed

+55
-66
lines changed

src/SplitIO/Component/Common/Di.php

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
<?php
22
namespace SplitIO\Component\Common;
33

4-
use Psr\Log\LoggerInterface;
5-
use SplitIO\Component\Cache\Pool;
4+
use SplitIO\Component\Log\Logger;
65

76
/**
87
* Class Di
98
* @package SplitIO\Common
109
*/
1110
class Di
1211
{
13-
const KEY_LOG = 'SPLIT-LOGGER';
12+
private \SplitIO\Component\Log\Logger|null $logger = null;
1413

15-
const KEY_FACTORY_TRACKER = 'FACTORY-TRACKER';
14+
private int $factoryTracker = 0;
15+
16+
private string|null $ipAddress = null;
1617

1718
/**
1819
* @var Singleton The reference to *Singleton* instance of this class
1920
*/
2021
private static $instance;
2122

22-
/**
23-
* @var array
24-
*/
25-
private $container = array();
26-
2723
/**
2824
* Returns the *Singleton* instance of this class.
2925
*
@@ -66,58 +62,77 @@ public function __wakeup()
6662
{
6763
}
6864

65+
private function __trackFactory()
66+
{
67+
$this->factoryTracker += 1;
68+
return $this->factoryTracker;
69+
}
70+
6971
/**
70-
* @param $key
71-
* @param $instance
72+
* @param \SplitIO\Component\Log\Logger $logger
7273
*/
73-
private function setKey($key, $instance)
74+
private function __setLogger(Logger $logger)
7475
{
75-
$this->container[$key] = $instance;
76+
if (is_null($this->logger)) {
77+
$this->logger = $logger;
78+
}
7679
}
7780

7881
/**
79-
* @param $key
80-
* @return mixed
82+
* @return null|\SplitIO\Component\Log\Logger
8183
*/
82-
private function getKey($key)
84+
private function __getLogger()
8385
{
84-
return (isset($this->container[$key])) ? $this->container[$key] : null;
86+
return $this->logger;
8587
}
8688

8789
/**
88-
* Set an object instance with its key
89-
* @param $key
90-
* @param $instance
90+
* @return null|string
9191
*/
92-
public static function set($key, $instance)
92+
private function __getIPAddress()
9393
{
94-
self::getInstance()->setKey($key, $instance);
94+
return $this->ipAddress;
9595
}
9696

9797
/**
98-
* Given a key returns the object instance associated with this.
99-
* @param $key
100-
* @return mixed
98+
* @return int
10199
*/
102-
public static function get($key)
100+
public static function trackFactory()
103101
{
104-
return self::getInstance()->getKey($key);
102+
return self::getInstance()->__trackFactory();
105103
}
106104

107-
108105
/**
109-
* @param LoggerInterface $logger
106+
* @param \SplitIO\Component\Log\Logger $logger
110107
*/
111-
public static function setLogger($logger)
108+
public static function setLogger(Logger $logger)
112109
{
113-
self::set(self::KEY_LOG, $logger);
110+
self::getInstance()->__setLogger($logger);
114111
}
115112

116113
/**
117-
* @return null|\Psr\Log\LoggerInterface
114+
* @return null|\SplitIO\Component\Log\Logger
118115
*/
119116
public static function getLogger()
120117
{
121-
return self::get(self::KEY_LOG);
118+
return self::getInstance()->__getLogger();
119+
}
120+
121+
/**
122+
* @param string $ip
123+
*/
124+
public static function setIPAddress(string $ip)
125+
{
126+
if (is_null($this->ip)) {
127+
$this->ip = $ip;
128+
}
129+
}
130+
131+
/**
132+
* @return null|string
133+
*/
134+
public static function getIPAddress()
135+
{
136+
return self::getInstance()->__getIPAddress();
122137
}
123138
}

src/SplitIO/Grammar/Condition/Matcher/ContainsString.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace SplitIO\Grammar\Condition\Matcher;
33

4-
use SplitIO\Split as SplitApp;
54
use SplitIO\Grammar\Condition\Matcher;
65

76
class ContainsString extends AbstractMatcher

src/SplitIO/Grammar/Condition/Matcher/EndsWith.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace SplitIO\Grammar\Condition\Matcher;
33

4-
use SplitIO\Split as SplitApp;
54
use SplitIO\Grammar\Condition\Matcher;
65

76
class EndsWith extends AbstractMatcher

src/SplitIO/Grammar/Condition/Matcher/EqualToBoolean.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace SplitIO\Grammar\Condition\Matcher;
33

4-
use SplitIO\Split as SplitApp;
54
use SplitIO\Grammar\Condition\Matcher;
65

76
class EqualToBoolean extends AbstractMatcher

src/SplitIO/Grammar/Condition/Matcher/Regex.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace SplitIO\Grammar\Condition\Matcher;
33

4-
use SplitIO\Split as SplitApp;
54
use SplitIO\Grammar\Condition\Matcher;
65

76
class Regex extends AbstractMatcher

src/SplitIO/Grammar/Condition/Matcher/StartsWith.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace SplitIO\Grammar\Condition\Matcher;
33

4-
use SplitIO\Split as SplitApp;
54
use SplitIO\Grammar\Condition\Matcher;
65

76
class StartsWith extends AbstractMatcher

src/SplitIO/Grammar/Condition/Matcher/Whitelist.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace SplitIO\Grammar\Condition\Matcher;
33

4-
use SplitIO\Split as SplitApp;
54
use SplitIO\Grammar\Condition\Matcher;
65

76
class Whitelist extends AbstractMatcher

src/SplitIO/Sdk.php

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ public static function factory($apiKey = 'localhost', array $options = array())
3131
//Adding API Key into args array.
3232
$options['apiKey'] = $apiKey;
3333

34-
if (self::instanceExists()) {
35-
return null;
36-
}
3734
self::registerInstance();
3835

3936
if ($apiKey == 'localhost') {
@@ -93,31 +90,18 @@ private static function registerCache(array $options)
9390

9491
private static function setIP($ip)
9592
{
96-
\SplitIO\Component\Common\Di::set('ipAddress', $ip);
97-
}
98-
99-
/**
100-
* Check factory instance
101-
*/
102-
private static function instanceExists()
103-
{
104-
$value = Di::get(Di::KEY_FACTORY_TRACKER);
105-
/* TODO MULTIPLE ALLOW
106-
if (is_null($value) || !$value) {
107-
return false;
108-
}
109-
Di::getLogger()->critical("Factory Instantiation: creating multiple factories is not possible. "
110-
. "You have already created a factory.");
111-
return true;
112-
*/
113-
return false;
93+
\SplitIO\Component\Common\Di::setIPAddress('ipAddress', $ip);
11494
}
11595

11696
/**
11797
* Register factory instance
11898
*/
11999
private static function registerInstance()
120100
{
121-
Di::set(Di::KEY_FACTORY_TRACKER, true);
101+
if (Di::trackFactory() > 1) {
102+
Di::getLogger()->warning("Factory Instantiation: You already have an instance of the Split factory. "
103+
. "Make sure you definitely want this additional instance. We recommend keeping only one instance of "
104+
. "the factory at all times (Singleton pattern) and reusing it throughout your application.");
105+
}
122106
}
123107
}

src/SplitIO/functions.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace SplitIO;
33

4-
use SplitIO\Split as SplitApp;
54
use SplitIO\Grammar\Condition\Partition\TreatmentEnum;
65

76
function version()
@@ -72,7 +71,7 @@ function parseSplitsFile($fileContent)
7271

7372
function getHostIpAddress()
7473
{
75-
$diIpAddress = \SplitIO\Component\Common\Di::get('ipAddress');
74+
$diIpAddress = \SplitIO\Component\Common\Di::getIPAddress('ipAddress');
7675
if (!is_null($diIpAddress) && is_string($diIpAddress) && trim($diIpAddress)) {
7776
return $diIpAddress;
7877
} elseif (isset($_SERVER['SERVER_ADDR']) && is_string($_SERVER['SERVER_ADDR'])

tests/Suite/DynamicConfigurations/EvaluatorTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use SplitIO\Test\Suite\Redis\ReflectiveTools;
55
use SplitIO\Component\Cache\SplitCache;
66
use SplitIO\Component\Cache\SegmentCache;
7-
use SplitIO\Split as SplitApp;
87
use SplitIO\Grammar\Split;
98
use SplitIO\Sdk\Evaluator;
109

tests/Suite/DynamicConfigurations/SplitTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use SplitIO\Component\Common\Di;
55
use SplitIO\Test\Suite\Redis\ReflectiveTools;
66
use SplitIO\Component\Cache\SplitCache;
7-
use SplitIO\Split as SplitApp;
87
use SplitIO\Grammar\Split;
98

109
class SplitTest extends \PHPUnit\Framework\TestCase

tests/Suite/Engine/HashTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use SplitIO\Engine\Hash\HashAlgorithmEnum;
99
use SplitIO\Grammar\Split;
1010
use SplitIO\Test\Suite\Redis\ReflectiveTools;
11-
use SplitIO\Split as SplitApp;
1211
use SplitIO\Component\Common\Di;
1312

1413
use SplitIO\Test\Utils;

0 commit comments

Comments
 (0)