Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the ability to configure the Attributes cache or disable the cache #968

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Framework/Bootloader/Attributes/AttributesBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
use Doctrine\Common\Annotations\Reader as DoctrineReaderInterface;
use Psr\Container\ContainerInterface;
use Psr\SimpleCache\CacheInterface;
use Spiral\Attributes\AnnotationReader;
use Spiral\Attributes\AttributeReader;
use Spiral\Attributes\Composite\SelectiveReader;
Expand All @@ -19,6 +18,7 @@
use Spiral\Attributes\ReaderInterface;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Boot\EnvironmentInterface;
use Spiral\Cache\CacheStorageProviderInterface;
use Spiral\Config\ConfiguratorInterface;

class AttributesBootloader extends Bootloader
Expand All @@ -41,6 +41,10 @@ public function init(EnvironmentInterface $env): void
'annotations' => [
'support' => $env->get('SUPPORT_ANNOTATIONS', true),
],
'cache' => [
'storage' => $env->get('ATTRIBUTES_CACHE_STORAGE', null),
'enabled' => $env->get('ATTRIBUTES_CACHE_ENABLED', false),
],
],
);
}
Expand All @@ -62,11 +66,11 @@ private function initReader(
): ReaderInterface {
$reader = new AttributeReader($instantiator);

if ($container->has(CacheInterface::class)) {
$cache = $container->get(CacheInterface::class);
\assert($cache instanceof CacheInterface);
if ($config->isCacheEnabled()) {
$provider = $container->get(CacheStorageProviderInterface::class);
\assert($provider instanceof CacheStorageProviderInterface);

$reader = new Psr16CachedReader($reader, $cache);
$reader = new Psr16CachedReader($reader, $provider->storage($config->getCacheStorage()));
}

$supportAnnotations = $config->isAnnotationsReaderEnabled();
Expand Down
23 changes: 23 additions & 0 deletions src/Framework/Bootloader/Attributes/AttributesConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,37 @@ final class AttributesConfig extends InjectableConfig
{
public const CONFIG = 'attributes';

/**
* @var array{
* annotations: array{support:bool},
* cache: array{storage: null|non-empty-string, enabled: bool},
* }
*/
protected array $config = [
'annotations' => [
'support' => true,
],
'cache' => [
'storage' => null,
'enabled' => false,
],
];

public function isAnnotationsReaderEnabled(): bool
{
return (bool)$this->config['annotations']['support'];
}

public function isCacheEnabled(): bool
{
return (bool)($this->config['cache']['enabled'] ?? false);
}

/**
* @return non-empty-string|null
*/
public function getCacheStorage(): ?string
{
return $this->config['cache']['storage'] ?? null;
}
}
11 changes: 11 additions & 0 deletions tests/Framework/Bootloader/Attributes/AttributesBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Spiral\Attributes\Internal\Instantiator\Facade;
use Spiral\Attributes\Internal\Instantiator\InstantiatorInterface;
use Spiral\Attributes\ReaderInterface;
use Spiral\Bootloader\Attributes\AttributesConfig;
use Spiral\Bootloader\Attributes\Factory;
use Spiral\Tests\Framework\BaseTestCase;

Expand All @@ -22,4 +23,14 @@ public function testInstantiatorBinding(): void
{
$this->assertContainerBoundAsSingleton(InstantiatorInterface::class, Facade::class);
}

public function testIsCacheEnabledShouldBeFalse(): void
{
$this->assertFalse($this->getConfig(AttributesConfig::CONFIG)['cache']['enabled']);
}

public function testGetStorageShouldBeNull(): void
{
$this->assertNull($this->getConfig(AttributesConfig::CONFIG)['cache']['storage']);
}
}
34 changes: 34 additions & 0 deletions tests/Framework/Bootloader/Attributes/AttributesConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Framework\Bootloader\Attributes;

use PHPUnit\Framework\TestCase;
use Spiral\Bootloader\Attributes\AttributesConfig;

final class AttributesConfigTest extends TestCase
{
public function testIsAnnotationsReaderEnabled(): void
{
$this->assertTrue((new AttributesConfig())->isAnnotationsReaderEnabled());
$this->assertFalse(
(new AttributesConfig(['annotations' => ['support' => false]]))->isAnnotationsReaderEnabled()
);
}

public function testIsCacheEnabled(): void
{
$this->assertFalse((new AttributesConfig())->isCacheEnabled());
$this->assertTrue((new AttributesConfig(['cache' => ['enabled' => true]]))->isCacheEnabled());
}

public function testGetCacheStorage(): void
{
$this->assertNull((new AttributesConfig())->getCacheStorage());
$this->assertSame(
'test',
(new AttributesConfig(['cache' => ['storage' => 'test']]))->getCacheStorage()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,22 @@

final class AttributesWithoutAnnotationsBootloaderTest extends BaseTestCase
{
public const MAKE_APP_ON_STARTUP = false;

#[Env('SUPPORT_ANNOTATIONS', 'false')]
public function testReaderBindingWithoutCache(): void
{
$this
->withDisabledBootloaders(\Spiral\Cache\Bootloader\CacheBootloader::class)
->initApp($this->getTestEnvVariables());

$this->assertContainerBoundAsSingleton(ReaderInterface::class, AttributeReader::class);
}

#[Env('SUPPORT_ANNOTATIONS', 'false')]
public function testReaderBindingWithtCache(): void
#[Env('ATTRIBUTES_CACHE_ENABLED', 'true')]
public function testReaderBindingWithCache(): void
{
$this->initApp($this->getTestEnvVariables());

$this->assertContainerBoundAsSingleton(ReaderInterface::class, Psr16CachedReader::class);
}

#[Env('SUPPORT_ANNOTATIONS', 'true')]
public function testSelectiveReaderBinding(): void
{
$this->initApp($this->getTestEnvVariables());

$this->assertContainerBoundAsSingleton(ReaderInterface::class, SelectiveReader::class);
}
}
Loading