Skip to content

Commit 1f403cc

Browse files
committed
updated Di component
1 parent e923e71 commit 1f403cc

21 files changed

+132
-72
lines changed

src/SplitIO/Component/Common/Di.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Di
1313

1414
private int $factoryTracker = 0;
1515

16-
private string|null $ipAddress = null;
16+
private string $ipAddress = "";
1717

1818
/**
1919
* @var Singleton The reference to *Singleton* instance of this class
@@ -75,7 +75,9 @@ private function __setLogger(Logger $logger)
7575
{
7676
if (is_null($this->logger)) {
7777
$this->logger = $logger;
78+
return;
7879
}
80+
$this->logger->debug("logger was set before, ignoring new instance provided");
7981
}
8082

8183
/**
@@ -86,6 +88,20 @@ private function __getLogger()
8688
return $this->logger;
8789
}
8890

91+
/**
92+
* @param string $ip
93+
*/
94+
private function __setIPAddress(string $ip)
95+
{
96+
if (empty($this->ipAddress)) {
97+
$this->ipAddress = $ip;
98+
return;
99+
}
100+
if (!(is_null($this->logger))) {
101+
$this->logger->debug("IPAddress was set before, ignoring new instance provided");
102+
}
103+
}
104+
89105
/**
90106
* @return null|string
91107
*/
@@ -123,9 +139,7 @@ public static function getLogger()
123139
*/
124140
public static function setIPAddress(string $ip)
125141
{
126-
if (is_null($this->ip)) {
127-
$this->ip = $ip;
128-
}
142+
self::getInstance()->__setIPAddress($ip);
129143
}
130144

131145
/**

src/SplitIO/Component/Common/ServiceProvider.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/SplitIO/Sdk.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static function factory($apiKey = 'localhost', array $options = array())
5959
private static function registerLogger(array $options)
6060
{
6161
$logger = LoggerFactory::setupLogger($options);
62-
ServiceProvider::registerLogger($logger);
62+
Di::setLogger($logger);
6363
}
6464

6565
private static function configureCache(array $options)
@@ -90,7 +90,7 @@ private static function configureCache(array $options)
9090

9191
private static function setIP($ip)
9292
{
93-
\SplitIO\Component\Common\Di::setIPAddress('ipAddress', $ip);
93+
\SplitIO\Component\Common\Di::setIPAddress($ip);
9494
}
9595

9696
/**

src/SplitIO/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function parseSplitsFile($fileContent)
7171

7272
function getHostIpAddress()
7373
{
74-
$diIpAddress = \SplitIO\Component\Common\Di::getIPAddress('ipAddress');
74+
$diIpAddress = \SplitIO\Component\Common\Di::getIPAddress();
7575
if (!is_null($diIpAddress) && is_string($diIpAddress) && trim($diIpAddress)) {
7676
return $diIpAddress;
7777
} elseif (isset($_SERVER['SERVER_ADDR']) && is_string($_SERVER['SERVER_ADDR'])

tests/Suite/Adapter/RedisAdapterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use SplitIO\Component\Cache\Storage\Exception\AdapterException;
77
use \Predis\Response\ServerException;
88
use \Predis\ClientException;
9-
use SplitIO\Component\Common\Di;
9+
use SplitIO\Test\Suite\Redis\ReflectiveTools;
1010

1111
class RedisAdapterTest extends \PHPUnit\Framework\TestCase
1212
{
@@ -20,7 +20,7 @@ private function getMockedLogger()
2020
'alert', 'notice', 'write', 'log'))
2121
->getMock();
2222

23-
Di::set(Di::KEY_LOG, $logger);
23+
ReflectiveTools::overrideLogger($logger);
2424

2525
return $logger;
2626
}

tests/Suite/Attributes/SdkAttributesTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ class SdkAttributesTest extends \PHPUnit\Framework\TestCase
1313
{
1414
public function testClient()
1515
{
16-
Di::set(Di::KEY_FACTORY_TRACKER, false);
17-
1816
//Testing version string
1917
$this->assertTrue(is_string(\SplitIO\version()));
2018

tests/Suite/Component/TrafficTypeTests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private function getMockedLogger()
1717
'alert', 'notice', 'write', 'log'))
1818
->getMock();
1919

20-
Di::set(Di::KEY_LOG, $logger);
20+
Di::setLogger($logger);
2121

2222
return $logger;
2323
}

tests/Suite/Engine/HashTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ public function testMurmur3HashFunction()
7979

8080
public function testAlgoField()
8181
{
82-
Di::set(Di::KEY_FACTORY_TRACKER, false);
83-
8482
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
8583
$options = array('prefix' => TEST_PREFIX);
8684

tests/Suite/Engine/SplitterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SplitterTest extends \PHPUnit\Framework\TestCase
1717
public function testDiLog()
1818
{
1919
$logger = LoggerFactory::setupLogger(array('adapter' => 'stdout', 'level' => 'error'));
20-
ServiceProvider::registerLogger($logger);
20+
Di::setLogger($logger);
2121

2222
$this->assertTrue(true);
2323
}

tests/Suite/InputValidation/FactoryTrackerTest.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace SplitIO\Test\Suite\InputValidation;
33

4-
use SplitIO\Component\Common\Di;
4+
use SplitIO\Test\Suite\Redis\ReflectiveTools;
55

66
class FactoryTrackerTest extends \PHPUnit\Framework\TestCase
77
{
@@ -31,29 +31,25 @@ private function getMockedLogger()
3131
'alert', 'notice', 'write', 'log'))
3232
->getMock();
3333

34-
Di::set(Di::KEY_LOG, $logger);
34+
ReflectiveTools::overrideLogger($logger);
3535

3636
return $logger;
3737
}
3838

3939
public function testMultipleClientInstantiation()
4040
{
41-
// @TODO FIX WHEN WE ALLOW MULTIPLE
42-
/*
43-
Di::set(Di::KEY_FACTORY_TRACKER, false);
4441
$splitFactory = $this->getFactoryClient();
4542
$this->assertNotNull($splitFactory->client());
4643

4744
$logger = $this->getMockedLogger();
4845

4946
$logger->expects($this->once())
50-
->method('critical')
51-
->with($this->equalTo("Factory Instantiation: creating multiple factories is not possible. "
52-
. "You have already created a factory."));
47+
->method('warning')
48+
->with($this->equalTo("Factory Instantiation: You already have an instance of the Split factory. "
49+
. "Make sure you definitely want this additional instance. We recommend keeping only one instance "
50+
. "of the factory at all times (Singleton pattern) and reusing it throughout your application."));
5351

5452
$splitFactory2 = $this->getFactoryClient();
55-
$this->assertEquals(null, $splitFactory2);
56-
*/
57-
$this->assertEquals(true, true);
53+
$this->assertNotNull($splitFactory2->client());
5854
}
5955
}

tests/Suite/InputValidation/GetTreatmentValidationTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace SplitIO\Test\Suite\InputValidation;
33

4-
use SplitIO\Component\Common\Di;
4+
use SplitIO\Test\Suite\Redis\ReflectiveTools;
55
use SplitIO\Sdk\Key;
66

77
use SplitIO\Test\Utils;
@@ -10,7 +10,6 @@ class GetTreatmentValidationTest extends \PHPUnit\Framework\TestCase
1010
{
1111
private function getFactoryClient()
1212
{
13-
Di::set(Di::KEY_FACTORY_TRACKER, false);
1413
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
1514
$options = array('prefix' => TEST_PREFIX);
1615

@@ -36,7 +35,7 @@ private function getMockedLogger()
3635
'alert', 'notice', 'write', 'log'))
3736
->getMock();
3837

39-
Di::set(Di::KEY_LOG, $logger);
38+
ReflectiveTools::overrideLogger($logger);
4039

4140
return $logger;
4241
}

tests/Suite/InputValidation/GetTreatmentsValidationTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?php
22
namespace SplitIO\Test\Suite\InputValidation;
33

4-
use SplitIO\Component\Common\Di;
54
use SplitIO\Sdk\Key;
5+
use SplitIO\Test\Suite\Redis\ReflectiveTools;
66

77
use SplitIO\Test\Utils;
88

99
class GetTreatmentsValidationTest extends \PHPUnit\Framework\TestCase
1010
{
1111
private function getFactoryClient()
1212
{
13-
Di::set(Di::KEY_FACTORY_TRACKER, false);
1413
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
1514
$options = array('prefix' => TEST_PREFIX);
1615

@@ -36,7 +35,7 @@ private function getMockedLogger()
3635
'alert', 'notice', 'write', 'log'))
3736
->getMock();
3837

39-
Di::set(Di::KEY_LOG, $logger);
38+
ReflectiveTools::overrideLogger($logger);
4039

4140
return $logger;
4241
}

tests/Suite/InputValidation/ManagerValidationTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?php
22
namespace SplitIO\Test\Suite\InputValidation;
33

4-
use SplitIO\Component\Common\Di;
4+
use SplitIO\Test\Suite\Redis\ReflectiveTools;
55

66
class ManagerValidationTest extends \PHPUnit\Framework\TestCase
77
{
88
private function getFactoryClient()
99
{
10-
Di::set(Di::KEY_FACTORY_TRACKER, false);
1110
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
1211
$options = array('prefix' => TEST_PREFIX);
1312

@@ -33,7 +32,7 @@ private function getMockedLogger()
3332
'alert', 'notice', 'write', 'log'))
3433
->getMock();
3534

36-
Di::set(Di::KEY_LOG, $logger);
35+
ReflectiveTools::overrideLogger($logger);
3736

3837
return $logger;
3938
}

tests/Suite/InputValidation/TrackValidationTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace SplitIO\Test\Suite\InputValidation;
33

4-
use SplitIO\Component\Common\Di;
4+
use SplitIO\Test\Suite\Redis\ReflectiveTools;
55
use SplitIO\Sdk\Validator\InputValidator;
66

77
use SplitIO\Test\Utils;
@@ -10,7 +10,6 @@ class TrackValidationTest extends \PHPUnit\Framework\TestCase
1010
{
1111
private function getFactoryClient()
1212
{
13-
Di::set(Di::KEY_FACTORY_TRACKER, false);
1413
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
1514
$options = array('prefix' => TEST_PREFIX);
1615

@@ -36,7 +35,7 @@ private function getMockedLogger()
3635
'alert', 'notice', 'write', 'log'))
3736
->getMock();
3837

39-
Di::set(Di::KEY_LOG, $logger);
38+
ReflectiveTools::overrideLogger($logger);
4039

4140
return $logger;
4241
}

tests/Suite/Matchers/MatchersTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class MatcherTest extends \PHPUnit\Framework\TestCase
1919

2020
private function setupSplitApp()
2121
{
22-
Di::set(Di::KEY_FACTORY_TRACKER, false);
2322
$parameters = array(
2423
'scheme' => 'redis',
2524
'host' => "localhost",

tests/Suite/Redis/ReflectiveTools.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SplitIO\Test\Suite\Redis;
44

55
use ReflectionClass;
6+
use SplitIO\Component\Common\Di;
67

78
class ReflectiveTools
89
{
@@ -54,4 +55,22 @@ public static function clientFromCachePool(\SplitIO\Component\Cache\Pool $cacheP
5455
$reflectionClient->setAccessible(true);
5556
return $reflectionClient->getValue($adapter);
5657
}
58+
59+
public static function overrideLogger($logger)
60+
{
61+
$di = Di::getInstance();
62+
$reflection = new \ReflectionClass('SplitIO\Component\Common\Di');
63+
$property = $reflection->getProperty('logger');
64+
$property->setAccessible(true);
65+
$property->setValue($di, $logger);
66+
}
67+
68+
public static function resetIPAddress()
69+
{
70+
$di = Di::getInstance();
71+
$reflection = new \ReflectionClass('SplitIO\Component\Common\Di');
72+
$property = $reflection->getProperty('ipAddress');
73+
$property->setAccessible(true);
74+
$property->setValue($di, "");
75+
}
5776
}

tests/Suite/Sdk/ImpressionWrapperTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use SplitIO\Test\Suite\Sdk\Helpers\ListenerClient;
99
use SplitIO\Test\Suite\Sdk\Helpers\ListenerClientWithException;
1010
use SplitIO\Test\Suite\Sdk\Helpers\ListenerClientWrong;
11-
use SplitIO\Component\Common\Di;
11+
use SplitIO\Test\Suite\Redis\ReflectiveTools;
1212

1313
use SplitIO\Test\Utils;
1414

@@ -42,7 +42,6 @@ public function testSendDataToClient()
4242

4343
private function getFactoryClient($sdkConfig)
4444
{
45-
Di::set(Di::KEY_FACTORY_TRACKER, false);
4645
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
4746
$options = array();
4847

@@ -121,6 +120,7 @@ public function testClient()
121120

122121
$impressionClient = new ListenerClient();
123122

123+
ReflectiveTools::resetIPAddress();
124124
$sdkConfig = array(
125125
'log' => array('adapter' => 'stdout'),
126126
'impressionListener' => $impressionClient,
@@ -162,6 +162,7 @@ public function testClientWithEmptyIpAddress()
162162

163163
$impressionClient3 = new ListenerClient();
164164

165+
ReflectiveTools::resetIPAddress();
165166
$sdkConfig = array(
166167
'log' => array('adapter' => 'stdout'),
167168
'impressionListener' => $impressionClient3,
@@ -194,6 +195,7 @@ public function testClientWithEmptyStringIpAddress()
194195

195196
$impressionClient4 = new ListenerClient();
196197

198+
ReflectiveTools::resetIPAddress();
197199
$sdkConfig = array(
198200
'log' => array('adapter' => 'stdout'),
199201
'impressionListener' => $impressionClient4,
@@ -226,6 +228,7 @@ public function testClientErasingServer()
226228

227229
$impressionClient4 = new ListenerClient();
228230

231+
ReflectiveTools::resetIPAddress();
229232
$sdkConfig = array(
230233
'log' => array('adapter' => 'stdout'),
231234
'impressionListener' => $impressionClient4,

tests/Suite/Sdk/ImpressionsTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class ImpressionsTest extends \PHPUnit\Framework\TestCase
1313
{
1414
public function testImpressionsAreAdded()
1515
{
16-
Di::set(Di::KEY_FACTORY_TRACKER, false);
1716
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
1817
$options = array('prefix' => TEST_PREFIX);
1918

@@ -63,7 +62,6 @@ public function testImpressionsAreAdded()
6362

6463
public function testExpirationOnlyOccursOnce()
6564
{
66-
Di::set(Di::KEY_FACTORY_TRACKER, false);
6765
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
6866
$options = array('prefix' => TEST_PREFIX);
6967

0 commit comments

Comments
 (0)