Skip to content

Commit

Permalink
Introduce dynamic shop pools based on shop id
Browse files Browse the repository at this point in the history
  • Loading branch information
kenariosz committed Apr 18, 2024
1 parent ae3f519 commit 08dfb5e
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 10 deletions.
21 changes: 21 additions & 0 deletions source/Internal/Framework/Cache/Pool/ShopPoolName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool;

class ShopPoolName
{
private static string $namePrefix = 'oxid_shop_pool_';

public static function get(int $shopId): string
{
return static::$namePrefix . $shopId;
}

public static function getAll(array $shopIds): array
{
return array_map(
static fn($id) => static::$namePrefix . $id,
$shopIds
);
}
}
2 changes: 2 additions & 0 deletions source/Internal/Framework/Cache/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ services:
Psr\Cache\CacheItemPoolInterface:
class: Symfony\Component\Cache\Adapter\FilesystemAdapter
arguments:
$namespace: '%oxid_current_shop_pool%'
$directory: '%oxid_cache_directory%'

Symfony\Contracts\Cache\CacheInterface:
class: Symfony\Component\Cache\Adapter\FilesystemAdapter
arguments:
$namespace: '%oxid_current_shop_pool%'
$directory: '%oxid_cache_directory%'

imports:
Expand Down
15 changes: 8 additions & 7 deletions source/Internal/Framework/DIContainer/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OxidEsales\EshopCommunity\Internal\Framework\DIContainer;

use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolName;
use OxidEsales\EshopCommunity\Internal\Framework\DIContainer\Dao\ProjectYamlDao;
use OxidEsales\EshopCommunity\Internal\Framework\DIContainer\Service\ProjectYamlImportService;
use OxidEsales\EshopCommunity\Internal\Framework\Logger\LoggerServiceFactory;
Expand Down Expand Up @@ -39,6 +40,7 @@ public function getContainer(): SymfonyContainerBuilder
$symfonyContainer->addCompilerPass(new RegisterListenersPass());
$symfonyContainer->addCompilerPass(new AddConsoleCommandPass());
$symfonyContainer->setParameter('oxid_cache_directory', $this->context->getCacheDirectory());
$symfonyContainer->setParameter('oxid_current_shop_pool', ShopPoolName::get($this->context->getCurrentShopId()));

$this->loadEditionServices($symfonyContainer);
$this->loadModuleServices($symfonyContainer);
Expand Down Expand Up @@ -116,13 +118,12 @@ private function loadModuleServices(SymfonyContainerBuilder $symfonyContainer):
//no active modules, do nothing.
} catch (LoaderLoadException $exception) {
$loggerServiceFactory = new LoggerServiceFactory(new Context());
$logger = $loggerServiceFactory->getLogger();
// phpcs:disable
$logger->error(
"Can't load module services file path $moduleServicesFilePath. Please check if file exists and all imports in the file are correct.",
[$exception]
);
// phpcs:enable
$loggerServiceFactory->getLogger()
->error(
"Can't load module services file path $moduleServicesFilePath.
Please check if file exists and all imports in the file are correct.",
[$exception]
);
}
}
}
1 change: 1 addition & 0 deletions source/Internal/Framework/Module/Cache/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ services:

OxidEsales\EshopCommunity\Internal\Framework\Module\Cache\ModuleCacheServiceInterface:
class: OxidEsales\EshopCommunity\Internal\Framework\Module\Cache\ModuleCache
public: true

OxidEsales\EshopCommunity\Internal\Framework\Module\Cache\ModuleCacheServiceBridgeInterface:
class: OxidEsales\EshopCommunity\Internal\Framework\Module\Cache\ModuleCacheBridge
Expand Down
24 changes: 21 additions & 3 deletions tests/Integration/Internal/Container/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Container;

use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolName;
use OxidEsales\EshopCommunity\Internal\Framework\DIContainer\ContainerBuilder;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
use OxidEsales\EshopCommunity\Tests\Unit\Internal\ContextStub;
Expand Down Expand Up @@ -92,11 +93,28 @@ public function testWhenShopRelatedConfigOverwritesMainServices(): void
);
}

public function testCacheDirectoryParameterIsAvailable(): void
public function testCacheDirectoryParameterIsSet(): void
{
$container = $this->makeContainer($this->makeContextStub());
$context = $this->makeContextStub();
$context->setCacheDirectory('cache');
$container = $this->makeContainer($context);

$this->assertSame(
$context->getCacheDirectory(),
$container->getParameter('oxid_cache_directory')
);
}

$this->assertTrue($container->hasParameter('oxid_cache_directory'), 'The oxid_cache_directory parameter does not exist.');
public function testCachePoolParameterIsSet(): void
{
$context = $this->makeContextStub();
$context->setCurrentShopId(1234567890);
$container = $this->makeContainer($context);

$this->assertSame(
ShopPoolName::get($context->getCurrentShopId()),
$container->getParameter('oxid_current_shop_pool')
);
}

private function makeContainer(ContextInterface $context): Container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
use OxidEsales\EshopCommunity\Internal\Framework\Module\Cache\CacheNotFoundException;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Cache\ModuleCacheServiceInterface;
use OxidEsales\EshopCommunity\Tests\ContainerTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

#[Group('cache')]
final class ModuleCacheTest extends TestCase
{
use ContainerTrait;
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/Internal/BasicContextStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ public function getCacheDirectory(): string
return $this->cacheDirectory;
}

public function setCacheDirectory(string $cacheDirectory): void
{
$this->cacheDirectory = $cacheDirectory;
}

public function getModuleCacheDirectory(): string
{
return $this->moduleCacheDirectory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\TemplateCacheService;
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[Group('cache')]
class ClearCacheCommandTest extends TestCase
{
public function testClearCacheTriggersRegularAndTemplatesCleaners(): void
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/Internal/Framework/Cache/Pool/ShopPoolNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace OxidEsales\EshopCommunity\Tests\Unit\Internal\Framework\Cache\Pool;

use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolName;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

#[Group('cache')]
class ShopPoolNameTest extends TestCase
{
public function testGetPoolName(): void
{
$shopId = 1;
$this->assertSame(
'oxid_shop_pool_' . $shopId,
ShopPoolName::get($shopId)
);
}

public function testGetAllPoolName(): void
{
$this->assertSame(
['oxid_shop_pool_1', 'oxid_shop_pool_2', 'oxid_shop_pool_3', 'oxid_shop_pool_4', 'oxid_shop_pool_5'],
ShopPoolName::getAll([1, 2, 3, 4, 5,])
);
}
}

0 comments on commit 08dfb5e

Please sign in to comment.