Skip to content

Commit

Permalink
Splitted some code to increase quality and readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
Geolim4 committed Apr 25, 2022
1 parent 56991e0 commit e0c8c61
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 107 deletions.
40 changes: 25 additions & 15 deletions lib/Phpfastcache/Cluster/Drivers/FullReplication/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ public function getItem(string $key): ExtendedCacheItemInterface
$itemData = $item->get();
$poolItemData = $poolItem->get();

if (
\is_object($itemData)
) {
if (\is_object($itemData)) {
if ($item->get() != $poolItemData) {
$poolsToResync[] = $driverPool;
}
Expand All @@ -61,18 +59,7 @@ public function getItem(string $key): ExtendedCacheItemInterface
}
}

if ($item && $item->isHit() && \count($poolsToResync) < \count($this->clusterPools)) {
foreach ($poolsToResync as $poolToResync) {
$poolItem = $poolToResync->getItem($key);
$poolItem->setEventManager($this->getEventManager())
->set($item->get())
->setHit($item->isHit())
->setTags($item->getTags())
->expiresAt($item->getExpirationDate())
->setDriver($poolToResync);
$poolToResync->save($poolItem);
}
}
$this->resynchronizePool($poolsToResync, $key, $item);

if ($item === null) {
$item = new Item($this, $key, $this->getEventManager());
Expand Down Expand Up @@ -175,4 +162,27 @@ public function commit(): bool
// Return true only if at least one backend confirmed the "commit" operation
return $hasCommitOnce;
}

/**
* @param ExtendedCacheItemPoolInterface[] $poolsToResynchronize
* @param string $key
* @param ?ExtendedCacheItemInterface $item
* @return void
* @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException
*/
protected function resynchronizePool(array $poolsToResynchronize, string $key, ?ExtendedCacheItemInterface $item): void
{
if ($item && $item->isHit() && \count($poolsToResynchronize) < \count($this->clusterPools)) {
foreach ($poolsToResynchronize as $poolToResynchronize) {
$poolItem = $poolToResynchronize->getItem($key);
$poolItem->setEventManager($this->getEventManager())
->set($item->get())
->setHit($item->isHit())
->setTags($item->getTags())
->expiresAt($item->getExpirationDate())
->setDriver($poolToResynchronize);
$poolToResynchronize->save($poolItem);
}
}
}
}
91 changes: 51 additions & 40 deletions lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,15 @@ public function getItem(string $key): ExtendedCacheItemInterface
* loop dispatching operations
*/
if (!isset($this->itemInstances[$key]) || !$this->getConfig()->isUseStaticItemCaching()) {
if (\preg_match('~([' . \preg_quote(self::$unsupportedKeyChars, '~') . ']+)~', $key, $matches)) {
throw new PhpfastcacheInvalidArgumentException(
'Unsupported key character detected: "' . $matches[1] . '".
Please check: https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Unsupported-characters-in-key-identifiers'
);
}
$this->validateCacheKey($key);

$cacheSlamsSpendSeconds = 0;

$itemClass = self::getItemClass();
/** @var $item ExtendedCacheItemInterface */
$item = new $itemClass($this, $key, $this->eventManager);
// $item = new (self::getItemClass())($this, $key, $this->eventManager);
// Uncomment above when this one will be fixed: https://github.com/phpmd/phpmd/issues/952

getItemDriverRead:
{
Expand Down Expand Up @@ -190,35 +188,7 @@ public function getItem(string $key): ExtendedCacheItemInterface
$item->setTags($this->driverUnwrapTags($driverArray));

getItemDriverExpired:
if ($item->isExpired()) {
/**
* Using driverDelete() instead of delete()
* to avoid infinite loop caused by
* getItem() call in delete() method
* As we MUST return an item in any
* way, we do not de-register here
*/
$this->driverDelete($item);

/**
* Reset the Item
*/
$item->set(null)
->expiresAfter((int) abs($this->getConfig()->getDefaultTtl()))
->setHit(false)
->setTags([]);
if ($this->getConfig()->isItemDetailedDate()) {
/**
* If the itemDetailedDate has been
* set after caching, we MUST inject
* a new DateTime object on the fly
*/
$item->setCreationDate(new DateTime());
$item->setModificationDate(new DateTime());
}
} else {
$item->setHit(true);
}
$this->handleExpiredCacheItem($item);
} else {
$item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl()));
}
Expand All @@ -227,12 +197,9 @@ public function getItem(string $key): ExtendedCacheItemInterface
$item = $this->itemInstances[$key];
}

$this->eventManager->dispatch(Event::CACHE_GET_ITEM, $this, $item);

if ($item !== null) {
$this->eventManager->dispatch(Event::CACHE_GET_ITEM, $this, $item);

$item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss();
}
$item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss();

return $item;
}
Expand Down Expand Up @@ -482,4 +449,48 @@ public function isAttached(CacheItemInterface $item): bool
}
return false;
}

protected function validateCacheKey(string $key): void
{
if (\preg_match('~([' . \preg_quote(self::$unsupportedKeyChars, '~') . ']+)~', $key, $matches)) {
throw new PhpfastcacheInvalidArgumentException(
'Unsupported key character detected: "' . $matches[1] . '".
Please check: https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Unsupported-characters-in-key-identifiers'
);
}
}

protected function handleExpiredCacheItem(ExtendedCacheItemInterface $item): void
{
if ($item->isExpired()) {
/**
* Using driverDelete() instead of delete()
* to avoid infinite loop caused by
* getItem() call in delete() method
* As we MUST return an item in any
* way, we do not de-register here
*/
$this->driverDelete($item);

/**
* Reset the Item
*/
$item->set(null)
->expiresAfter((int) abs($this->getConfig()->getDefaultTtl()))
->setHit(false)
->setTags([]);

if ($this->getConfig()->isItemDetailedDate()) {
/**
* If the itemDetailedDate has been
* set after caching, we MUST inject
* a new DateTime object on the fly
*/
$item->setCreationDate(new DateTime());
$item->setModificationDate(new DateTime());
}
} else {
$item->setHit(true);
}
}
}
116 changes: 72 additions & 44 deletions lib/Phpfastcache/Util/ClassNamespaceResolverTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

namespace Phpfastcache\Util;

use Iterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

Expand Down Expand Up @@ -87,53 +86,12 @@ protected static function findClasses(string $path): array
$class = '';
switch ($token[0]) {
case T_NAMESPACE:
$namespace = '';
// If there is a namespace, extract it (PHP 8 test)
if (\defined('T_NAME_QUALIFIED')) {
while (isset($tokens[++$i][1])) {
if ($tokens[$i][0] === T_NAME_QUALIFIED) {
$namespace = $tokens[$i][1];
break;
}
}
} else {
while (isset($tokens[++$i][1])) {
if (\in_array($tokens[$i][0], [T_STRING, T_NS_SEPARATOR], true)) {
$namespace .= $tokens[$i][1];
}
}
}
$namespace .= '\\';
$namespace = self::buildTokenNamespace($i, $tokens);
break;
case T_CLASS:
case T_INTERFACE:
case T_TRAIT:
// Skip usage of ::class constant
$isClassConstant = false;
for ($j = $i - 1; $j > 0; --$j) {
if (!isset($tokens[$j][1])) {
break;
}
if (T_DOUBLE_COLON === $tokens[$j][0]) {
$isClassConstant = true;
break;
} elseif (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT], false)) {
break;
}
}
if ($isClassConstant) {
break;
}
// Find the classname
while (isset($tokens[++$i][1])) {
$t = $tokens[$i];
if (T_STRING === $t[0]) {
$class .= $t[1];
} elseif ('' !== $class && T_WHITESPACE === $t[0]) {
break;
}
}
$classes[] = ltrim($namespace . $class, '\\');
$classes = self::buildTokenClasses($namespace, $class, $classes, $i, $tokens);
break;
default:
break;
Expand All @@ -143,6 +101,76 @@ protected static function findClasses(string $path): array
return $classes;
}

/**
* @param string $namespace
* @param string $class
* @param string[] $classes
* @param int $index
* @param array<array<mixed>|string> $tokens
* @return string[]
*/
protected static function buildTokenClasses(string $namespace, string $class, array $classes, int $index, array $tokens): array
{
// Skip usage of ::class constant
$isClassConstant = false;
for ($j = $index - 1; $j > 0; --$j) {
if (!isset($tokens[$j][1])) {
break;
}
if (T_DOUBLE_COLON === $tokens[$j][0]) {
$isClassConstant = true;
break;
}

if (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT], false)) {
break;
}
}
if ($isClassConstant) {
return $classes;
}

// Find the classname
while (isset($tokens[++$index][1])) {
$t = $tokens[$index];
if (T_STRING === $t[0]) {
$class .= $t[1];
} elseif ('' !== $class && T_WHITESPACE === $t[0]) {
break;
}
}

return \array_merge($classes, [\ltrim($namespace . $class, '\\')]);
}

/**
* @param int $index
* @param array<array<mixed>|string> $tokens
* @return string
*/
protected static function buildTokenNamespace(int $index, array $tokens): string
{
$namespace = '';

// If there is a namespace, extract it (PHP 8 test)
if (\defined('T_NAME_QUALIFIED')) {
while (isset($tokens[++$index][1])) {
if ($tokens[$index][0] === T_NAME_QUALIFIED) {
$namespace = $tokens[$index][1];
break;
}
}
} else {
while (isset($tokens[++$index][1])) {
if (\in_array($tokens[$index][0], [T_STRING, T_NS_SEPARATOR], true)) {
$namespace .= $tokens[$index][1];
}
}
}

return $namespace . '\\';
}

/**
* @return string
*/
Expand Down
21 changes: 13 additions & 8 deletions tests/DriverListResolver.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,21 @@

$driverList = CacheManager::getDriverList();


foreach ($driverList as $driver) {
foreach ($subClasses as $subClass) {
$className = "Phpfastcache\\Drivers\\{$driver}\\{$subClass}";
if (class_exists($className)) {
$testHelper->assertPass(sprintf('Found the %s %s class: "%s"', $driver, $subClass, $className));
} else {
$testHelper->assertFail(sprintf('Class "%s" not found', $className));
if(count($driverList)){
foreach ($driverList as $driver) {
foreach ($subClasses as $subClass) {
$className = "Phpfastcache\\Drivers\\{$driver}\\{$subClass}";
if (class_exists($className)) {
$testHelper->assertPass(sprintf('Found the %s %s class: "%s"', $driver, $subClass, $className));
} else {
$testHelper->assertFail(sprintf('Class "%s" not found', $className));
}
}
}
} else {
$testHelper->assertFail('Driver list is empty');
}



$testHelper->terminateTest();

0 comments on commit e0c8c61

Please sign in to comment.