From 7d3a0934e3795d3cbdc337b1fb0788664c0b08ec Mon Sep 17 00:00:00 2001 From: Matias Melograno Date: Tue, 17 Jan 2023 15:29:15 -0300 Subject: [PATCH 1/2] replaced Di in favor of Context --- src/SplitIO/Component/Cache/EventsCache.php | 5 +-- .../Component/Cache/ImpressionCache.php | 5 +-- src/SplitIO/Component/Cache/Pool.php | 4 +- src/SplitIO/Component/Cache/SegmentCache.php | 2 +- src/SplitIO/Component/Cache/SplitCache.php | 2 +- .../Cache/Storage/Adapter/PRedis.php | 4 +- .../Storage/Adapter/SafeRedisWrapper.php | 38 +++++++++---------- .../Component/Common/{Di.php => Context.php} | 4 +- src/SplitIO/Engine.php | 2 +- src/SplitIO/Sdk.php | 8 ++-- src/SplitIO/Sdk/Manager/SplitManager.php | 2 +- src/SplitIO/Split.php | 4 +- src/SplitIO/functions.php | 2 +- tests/Suite/Attributes/SdkAttributesTest.php | 2 +- tests/Suite/Component/TrafficTypeTests.php | 6 +-- .../Suite/DynamicConfigurations/SplitTest.php | 2 +- tests/Suite/Engine/HashTest.php | 2 +- tests/Suite/Engine/SplitterTest.php | 4 +- tests/Suite/Matchers/MatchersTest.php | 2 +- tests/Suite/Redis/CacheInterfacesTest.php | 4 +- tests/Suite/Redis/ReflectiveTools.php | 14 +++---- tests/Suite/Sdk/ImpressionsTest.php | 2 +- tests/Suite/Sdk/SdkReadOnlyTest.php | 4 +- .../TrafficAllocationTest.php | 4 +- 24 files changed, 63 insertions(+), 65 deletions(-) rename src/SplitIO/Component/Common/{Di.php => Context.php} (99%) diff --git a/src/SplitIO/Component/Cache/EventsCache.php b/src/SplitIO/Component/Cache/EventsCache.php index 71b3351b..b8baf739 100644 --- a/src/SplitIO/Component/Cache/EventsCache.php +++ b/src/SplitIO/Component/Cache/EventsCache.php @@ -1,7 +1,7 @@ toArray()); - // @TODO REMOVE LOGGER DI - Di::getLogger()->debug("Adding event item into queue: ". $queueJSONmessage); + Context::getLogger()->debug("Adding event item into queue: ". $queueJSONmessage); return ($this->cache->rightPushInList(self::KEY_EVENTS_LIST, $queueJSONmessage) > 0); } } diff --git a/src/SplitIO/Component/Cache/ImpressionCache.php b/src/SplitIO/Component/Cache/ImpressionCache.php index b9eb2c69..c15caf38 100644 --- a/src/SplitIO/Component/Cache/ImpressionCache.php +++ b/src/SplitIO/Component/Cache/ImpressionCache.php @@ -1,7 +1,7 @@ debug("Adding impressions into queue: ". implode(",", $toStore)); + Context::getLogger()->debug("Adding impressions into queue: ". implode(",", $toStore)); $count = $this->cache->rightPushInList(self::IMPRESSIONS_QUEUE_KEY, $toStore); if ($count == count($impressions)) { $this->cache->expireKey(self::IMPRESSIONS_QUEUE_KEY, self::IMPRESSION_KEY_DEFAULT_TTL); diff --git a/src/SplitIO/Component/Cache/Pool.php b/src/SplitIO/Component/Cache/Pool.php index ae850078..88e22744 100644 --- a/src/SplitIO/Component/Cache/Pool.php +++ b/src/SplitIO/Component/Cache/Pool.php @@ -3,7 +3,7 @@ use SplitIO\Component\Cache\Storage\Adapter\PRedis as PRedisAdapter; use SplitIO\Component\Cache\Storage\Adapter\SafeRedisWrapper; -use SplitIO\Component\Common\Di; +use SplitIO\Component\Common\Context; class Pool extends CacheKeyTrait { @@ -39,7 +39,7 @@ public function __construct(array $options = array()) public function get($key) { $this->assertValidKey($key); - Di::getLogger()->debug("Fetching item ** $key ** from cache"); + Context::getLogger()->debug("Fetching item ** $key ** from cache"); return $this->adapter->get($key); } diff --git a/src/SplitIO/Component/Cache/SegmentCache.php b/src/SplitIO/Component/Cache/SegmentCache.php index 1c613779..437eb006 100644 --- a/src/SplitIO/Component/Cache/SegmentCache.php +++ b/src/SplitIO/Component/Cache/SegmentCache.php @@ -1,7 +1,7 @@ warning("'replication' option was deprecated please use 'distributedStrategy'"); + Context::getLogger()->warning("'replication' option was deprecated please use 'distributedStrategy'"); if (!isset($_options['distributedStrategy'])) { $_options['distributedStrategy'] = $_options['replication']; } diff --git a/src/SplitIO/Component/Cache/Storage/Adapter/SafeRedisWrapper.php b/src/SplitIO/Component/Cache/Storage/Adapter/SafeRedisWrapper.php index 9d7a9f4d..43484898 100644 --- a/src/SplitIO/Component/Cache/Storage/Adapter/SafeRedisWrapper.php +++ b/src/SplitIO/Component/Cache/Storage/Adapter/SafeRedisWrapper.php @@ -4,7 +4,7 @@ use SplitIO\Component\Cache\Storage\Exception\AdapterException; use SplitIO\Component\Cache\Item; use SplitIO\Component\Utils as SplitIOUtils; -use SplitIO\Component\Common\Di; +use SplitIO\Component\Common\Context; /** * Class SafeRedisWrapper @@ -32,9 +32,9 @@ public function get($key) try { return $this->cacheAdapter->get($key); } catch (\Exception $e) { - Di::getLogger()->critical("An error occurred getting " . $key . " from redis."); - Di::getLogger()->critical($e->getMessage()); - Di::getLogger()->critical($e->getTraceAsString()); + Context::getLogger()->critical("An error occurred getting " . $key . " from redis."); + Context::getLogger()->critical($e->getMessage()); + Context::getLogger()->critical($e->getTraceAsString()); return null; } } @@ -53,9 +53,9 @@ public function fetchMany(array $keys = array()) try { return $this->cacheAdapter->fetchMany($keys); } catch (\Exception $e) { - Di::getLogger()->critical("An error occurred getting " . json_encode($keys) . " from redis."); - Di::getLogger()->critical($e->getMessage()); - Di::getLogger()->critical($e->getTraceAsString()); + Context::getLogger()->critical("An error occurred getting " . json_encode($keys) . " from redis."); + Context::getLogger()->critical($e->getMessage()); + Context::getLogger()->critical($e->getTraceAsString()); return array(); } } @@ -70,9 +70,9 @@ public function isOnList($key, $value) try { return $this->cacheAdapter->isOnList($key, $value); } catch (\Exception $e) { - Di::getLogger()->critical("An error occurred for " . $key); - Di::getLogger()->critical($e->getMessage()); - Di::getLogger()->critical($e->getTraceAsString()); + Context::getLogger()->critical("An error occurred for " . $key); + Context::getLogger()->critical($e->getMessage()); + Context::getLogger()->critical($e->getTraceAsString()); return false; } } @@ -86,9 +86,9 @@ public function getKeys($pattern = '*') try { return $this->cacheAdapter->getKeys($pattern); } catch (\Exception $e) { - Di::getLogger()->critical("An error occurred getting " . $pattern); - Di::getLogger()->critical($e->getMessage()); - Di::getLogger()->critical($e->getTraceAsString()); + Context::getLogger()->critical("An error occurred getting " . $pattern); + Context::getLogger()->critical($e->getMessage()); + Context::getLogger()->critical($e->getTraceAsString()); return array(); } } @@ -103,9 +103,9 @@ public function rightPushQueue($queueName, $item) try { return $this->cacheAdapter->rightPushQueue($queueName, $item); } catch (\Exception $e) { - Di::getLogger()->critical("An error occurred performing RPUSH into " . $queueName); - Di::getLogger()->critical($e->getMessage()); - Di::getLogger()->critical($e->getTraceAsString()); + Context::getLogger()->critical("An error occurred performing RPUSH into " . $queueName); + Context::getLogger()->critical($e->getMessage()); + Context::getLogger()->critical($e->getTraceAsString()); return 0; } } @@ -120,9 +120,9 @@ public function expireKey($key, $ttl) try { return $this->cacheAdapter->expireKey($key, $ttl); } catch (\Exception $e) { - Di::getLogger()->critical("An error occurred setting expiration for " . $key); - Di::getLogger()->critical($e->getMessage()); - Di::getLogger()->critical($e->getTraceAsString()); + Context::getLogger()->critical("An error occurred setting expiration for " . $key); + Context::getLogger()->critical($e->getMessage()); + Context::getLogger()->critical($e->getTraceAsString()); return false; } } diff --git a/src/SplitIO/Component/Common/Di.php b/src/SplitIO/Component/Common/Context.php similarity index 99% rename from src/SplitIO/Component/Common/Di.php rename to src/SplitIO/Component/Common/Context.php index 3111a247..97a28b82 100644 --- a/src/SplitIO/Component/Common/Di.php +++ b/src/SplitIO/Component/Common/Context.php @@ -4,10 +4,10 @@ use SplitIO\Component\Log\Logger; /** - * Class Di + * Class Context * @package SplitIO\Common */ -class Di +class Context { const SAME_APIKEY = "Factory Instantiation: You already have %s factory/factories with this API Key. " . "We recommend keeping only one instance of the factory at all times (Singleton pattern) and " diff --git a/src/SplitIO/Engine.php b/src/SplitIO/Engine.php index d449c35f..db956b31 100644 --- a/src/SplitIO/Engine.php +++ b/src/SplitIO/Engine.php @@ -6,7 +6,7 @@ use SplitIO\Engine\Splitter; use SplitIO\Grammar\Condition\ConditionTypeEnum; use SplitIO\Sdk\Impressions\ImpressionLabel; -use SplitIO\Component\Common\Di; +use SplitIO\Component\Common\Context; class Engine { diff --git a/src/SplitIO/Sdk.php b/src/SplitIO/Sdk.php index ecb39081..6b388a35 100644 --- a/src/SplitIO/Sdk.php +++ b/src/SplitIO/Sdk.php @@ -7,7 +7,7 @@ use SplitIO\Exception\Exception; use SplitIO\Sdk\Factory\LocalhostSplitFactory; use SplitIO\Sdk\Factory\SplitFactory; -use SplitIO\Component\Common\Di; +use SplitIO\Component\Common\Context; use SplitIO\Engine\Splitter; use SplitIO\Component\Cache\Pool; @@ -32,7 +32,7 @@ public static function factory($apiKey = 'localhost', array $options = array()) $options['apiKey'] = $apiKey; //Tracking Factory Instantiation - Di::trackFactory($apiKey); + Context::trackFactory($apiKey); //Register Logger self::registerLogger((isset($options['log'])) ? $options['log'] : array()); @@ -57,7 +57,7 @@ public static function factory($apiKey = 'localhost', array $options = array()) private static function registerLogger(array $options) { $logger = LoggerFactory::setupLogger($options); - Di::setLogger($logger); + Context::setLogger($logger); } private static function configureCache(array $options) @@ -88,6 +88,6 @@ private static function configureCache(array $options) private static function setIP($ip) { - \SplitIO\Component\Common\Di::setIPAddress($ip); + \SplitIO\Component\Common\Context::setIPAddress($ip); } } diff --git a/src/SplitIO/Sdk/Manager/SplitManager.php b/src/SplitIO/Sdk/Manager/SplitManager.php index 30b6cb93..72e477d2 100644 --- a/src/SplitIO/Sdk/Manager/SplitManager.php +++ b/src/SplitIO/Sdk/Manager/SplitManager.php @@ -2,7 +2,7 @@ namespace SplitIO\Sdk\Manager; use \stdClass; -use SplitIO\Component\Common\Di; +use SplitIO\Component\Common\Context; use SplitIO\Grammar\Condition; use SplitIO\Grammar\Split; use SplitIO\Split as SplitApp; diff --git a/src/SplitIO/Split.php b/src/SplitIO/Split.php index 7cc0e4a3..797203a1 100644 --- a/src/SplitIO/Split.php +++ b/src/SplitIO/Split.php @@ -1,7 +1,7 @@ getMock(); - Di::setLogger($logger); + Context::setLogger($logger); return $logger; } @@ -42,7 +42,7 @@ public function testTrafficTypeWarning() $this->assertEquals($keyTrafficType, 'SPLITIO.trafficType.abc'); - $redisClient = ReflectiveTools::clientFromCachePool(Di::getCache()); + $redisClient = ReflectiveTools::clientFromCachePool(Context::getCache()); $redisClient->del($keyTrafficType); $splitCache = new SplitCache(); diff --git a/tests/Suite/DynamicConfigurations/SplitTest.php b/tests/Suite/DynamicConfigurations/SplitTest.php index c946725d..2caaedd4 100644 --- a/tests/Suite/DynamicConfigurations/SplitTest.php +++ b/tests/Suite/DynamicConfigurations/SplitTest.php @@ -1,7 +1,7 @@ 'stdout', 'level' => 'error')); - Di::setLogger($logger); + Context::setLogger($logger); $this->assertTrue(true); } diff --git a/tests/Suite/Matchers/MatchersTest.php b/tests/Suite/Matchers/MatchersTest.php index b48990d1..2b9c37dc 100644 --- a/tests/Suite/Matchers/MatchersTest.php +++ b/tests/Suite/Matchers/MatchersTest.php @@ -7,7 +7,7 @@ use SplitIO\Component\Cache\SplitCache; use SplitIO\Grammar\Condition\Matcher; use SplitIO\Grammar\Condition\Matcher\DataType\DateTime; -use SplitIO\Component\Common\Di; +use SplitIO\Component\Common\Context; use SplitIO\Test\Suite\Redis\ReflectiveTools; use \ReflectionMethod; diff --git a/tests/Suite/Redis/CacheInterfacesTest.php b/tests/Suite/Redis/CacheInterfacesTest.php index 23426a6a..16971c01 100644 --- a/tests/Suite/Redis/CacheInterfacesTest.php +++ b/tests/Suite/Redis/CacheInterfacesTest.php @@ -5,7 +5,7 @@ use SplitIO\Component\Cache\Pool; use SplitIO\Component\Cache\SegmentCache; use SplitIO\Component\Cache\SplitCache; -use SplitIO\Component\Common\Di; +use SplitIO\Component\Common\Context; use SplitIO\Component\Cache\BlockUntilReadyCache; use SplitIO\Component\Log\Handler\Stdout; use SplitIO\Component\Log\Logger; @@ -42,7 +42,7 @@ public function testDiLog() $logger = new Logger($logAdapter, LogLevelEnum::INFO); - Di::getInstance()->setLogger($logger); + Context::getInstance()->setLogger($logger); $this->assertTrue(true); } diff --git a/tests/Suite/Redis/ReflectiveTools.php b/tests/Suite/Redis/ReflectiveTools.php index fe5c1e0b..064265e2 100644 --- a/tests/Suite/Redis/ReflectiveTools.php +++ b/tests/Suite/Redis/ReflectiveTools.php @@ -3,7 +3,7 @@ namespace SplitIO\Test\Suite\Redis; use ReflectionClass; -use SplitIO\Component\Common\Di; +use SplitIO\Component\Common\Context; class ReflectiveTools { @@ -58,8 +58,8 @@ public static function clientFromCachePool(\SplitIO\Component\Cache\Pool $cacheP public static function overrideLogger($logger) { - $di = Di::getInstance(); - $reflection = new \ReflectionClass('SplitIO\Component\Common\Di'); + $di = Context::getInstance(); + $reflection = new \ReflectionClass('SplitIO\Component\Common\Context'); $property = $reflection->getProperty('logger'); $property->setAccessible(true); $property->setValue($di, $logger); @@ -67,8 +67,8 @@ public static function overrideLogger($logger) public static function resetIPAddress() { - $di = Di::getInstance(); - $reflection = new \ReflectionClass('SplitIO\Component\Common\Di'); + $di = Context::getInstance(); + $reflection = new \ReflectionClass('SplitIO\Component\Common\Context'); $property = $reflection->getProperty('ipAddress'); $property->setAccessible(true); $property->setValue($di, ""); @@ -76,8 +76,8 @@ public static function resetIPAddress() public static function overrideTracker() { - $di = Di::getInstance(); - $reflection = new \ReflectionClass('SplitIO\Component\Common\Di'); + $di = Context::getInstance(); + $reflection = new \ReflectionClass('SplitIO\Component\Common\Context'); $property = $reflection->getProperty('factoryTracker'); $property->setAccessible(true); $property->setValue($di, array()); diff --git a/tests/Suite/Sdk/ImpressionsTest.php b/tests/Suite/Sdk/ImpressionsTest.php index 9fb1c182..c9befe1f 100644 --- a/tests/Suite/Sdk/ImpressionsTest.php +++ b/tests/Suite/Sdk/ImpressionsTest.php @@ -1,7 +1,7 @@ equalTo('The SPLIT definition for \'mockedPRedisInvalid\' has not been found') )); - Di::setLogger($logger); + Context::setLogger($logger); $this->assertEquals('on', $splitSdk->getTreatment('valid', 'mockedPRedis')); $this->assertEquals('off', $splitSdk->getTreatment('invalid', 'mockedPRedis')); diff --git a/tests/Suite/TrafficAllocation/TrafficAllocationTest.php b/tests/Suite/TrafficAllocation/TrafficAllocationTest.php index 78e0ce3a..ec2ccd95 100644 --- a/tests/Suite/TrafficAllocation/TrafficAllocationTest.php +++ b/tests/Suite/TrafficAllocation/TrafficAllocationTest.php @@ -4,7 +4,7 @@ use SplitIO\Component\Initialization\LoggerFactory; use SplitIO\Component\Common\ServiceProvider; use SplitIO\Grammar\Split; -use SplitIO\Component\Common\Di; +use SplitIO\Component\Common\Context; class TrafficAllocationTest extends \PHPUnit\Framework\TestCase { @@ -21,7 +21,7 @@ public function testTrafficAllocation() ]); $logger = LoggerFactory::setupLogger(array('adapter' => 'stdout', 'level' => 'error')); - Di::setLogger($logger); + Context::setLogger($logger); $rawSplit = array( 'name' => 'test1', From 092d94cf8173de9ed40a4a76c9316807d142d21f Mon Sep 17 00:00:00 2001 From: Matias Melograno Date: Tue, 17 Jan 2023 15:33:03 -0300 Subject: [PATCH 2/2] removed unused imports --- src/SplitIO/Component/Cache/SegmentCache.php | 2 -- src/SplitIO/Component/Cache/SplitCache.php | 2 -- src/SplitIO/Engine.php | 1 - src/SplitIO/Sdk/Manager/SplitManager.php | 1 - tests/Suite/Attributes/SdkAttributesTest.php | 1 - tests/Suite/DynamicConfigurations/SplitTest.php | 1 - tests/Suite/Engine/HashTest.php | 1 - tests/Suite/Matchers/MatchersTest.php | 1 - tests/Suite/Sdk/ImpressionsTest.php | 1 - 9 files changed, 11 deletions(-) diff --git a/src/SplitIO/Component/Cache/SegmentCache.php b/src/SplitIO/Component/Cache/SegmentCache.php index 437eb006..1c1a838f 100644 --- a/src/SplitIO/Component/Cache/SegmentCache.php +++ b/src/SplitIO/Component/Cache/SegmentCache.php @@ -1,8 +1,6 @@