Skip to content

Commit

Permalink
Merge pull request #1087 from spiral/bugfix/enable-annotations-by-def…
Browse files Browse the repository at this point in the history
…ault

Enable reading annotations by default
  • Loading branch information
butschster committed Feb 29, 2024
2 parents 92bcfa4 + fcde1a4 commit 82ece5f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -139,7 +139,8 @@
"spiral/validator": "^1.3",
"google/protobuf": "^3.25",
"symplify/monorepo-builder": "^10.2.7",
"vimeo/psalm": "^5.9"
"vimeo/psalm": "^5.9",
"doctrine/annotations": "^2.0"
},
"autoload-dev": {
"psr-4": {
Expand Down
4 changes: 1 addition & 3 deletions src/Framework/Bootloader/Attributes/AttributesBootloader.php
Expand Up @@ -39,7 +39,7 @@ public function init(EnvironmentInterface $env): void
AttributesConfig::CONFIG,
[
'annotations' => [
'support' => $env->get('SUPPORT_ANNOTATIONS', false),
'support' => $env->get('SUPPORT_ANNOTATIONS', \interface_exists(DoctrineReaderInterface::class)),
],
'cache' => [
'storage' => $env->get('ATTRIBUTES_CACHE_STORAGE', null),
Expand Down Expand Up @@ -76,14 +76,12 @@ private function initReader(
$supportAnnotations = $config->isAnnotationsReaderEnabled();

if ($supportAnnotations) {
/** @psalm-suppress UndefinedClass */
if (!\interface_exists(DoctrineReaderInterface::class)) {
throw new InitializationException(
'Doctrine annotations reader is not available, please install "doctrine/annotations" package',
);
}

/** @psalm-suppress UndefinedClass, InvalidArgument */
$reader = new SelectiveReader([
$reader,
new AnnotationReader(new DoctrineAnnotationReader()),
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/Bootloader/Attributes/AttributesConfig.php
Expand Up @@ -18,7 +18,7 @@ final class AttributesConfig extends InjectableConfig
*/
protected array $config = [
'annotations' => [
'support' => false,
'support' => true,
],
'cache' => [
'storage' => null,
Expand Down
Expand Up @@ -4,9 +4,9 @@

namespace Framework\Bootloader\Attributes;

use Spiral\Attributes\AttributeReader;
use Spiral\Attributes\Composite\SelectiveReader;
use Spiral\Attributes\Internal\Instantiator\Facade;
use Spiral\Attributes\Internal\Instantiator\InstantiatorInterface;
use Spiral\Attributes\Internal\Instantiator\NamedArgumentsInstantiator;
use Spiral\Attributes\ReaderInterface;
use Spiral\Bootloader\Attributes\AttributesConfig;
use Spiral\Tests\Framework\BaseTestCase;
Expand All @@ -15,12 +15,12 @@ final class AttributesBootloaderTest extends BaseTestCase
{
public function testReaderBinding(): void
{
$this->assertContainerBoundAsSingleton(ReaderInterface::class, AttributeReader::class);
$this->assertContainerBoundAsSingleton(ReaderInterface::class, SelectiveReader::class);
}

public function testInstantiatorBinding(): void
{
$this->assertContainerBoundAsSingleton(InstantiatorInterface::class, NamedArgumentsInstantiator::class);
$this->assertContainerBoundAsSingleton(InstantiatorInterface::class, Facade::class);
}

public function testIsCacheEnabledShouldBeFalse(): void
Expand Down
Expand Up @@ -11,9 +11,9 @@ final class AttributesConfigTest extends TestCase
{
public function testIsAnnotationsReaderEnabled(): void
{
$this->assertFalse((new AttributesConfig())->isAnnotationsReaderEnabled());
$this->assertTrue(
(new AttributesConfig(['annotations' => ['support' => true]]))->isAnnotationsReaderEnabled()
$this->assertTrue((new AttributesConfig())->isAnnotationsReaderEnabled());
$this->assertFalse(
(new AttributesConfig(['annotations' => ['support' => false]]))->isAnnotationsReaderEnabled()
);
}

Expand Down
Expand Up @@ -5,22 +5,30 @@
namespace Framework\Bootloader\Attributes;

use Spiral\Attributes\AttributeReader;
use Spiral\Attributes\Composite\SelectiveReader;
use Spiral\Attributes\Psr16CachedReader;
use Spiral\Attributes\ReaderInterface;
use Spiral\Testing\Attribute\Env;
use Spiral\Tests\Framework\BaseTestCase;

final class AttributesWithoutAnnotationsBootloaderTest extends BaseTestCase
{
#[Env('SUPPORT_ANNOTATIONS', false)]
public function testReaderBindingWithoutCache(): void
{
$this->assertContainerBoundAsSingleton(ReaderInterface::class, AttributeReader::class);
}

#[Env('SUPPORT_ANNOTATIONS', 'false')]
#[Env('ATTRIBUTES_CACHE_ENABLED', 'true')]
#[Env('SUPPORT_ANNOTATIONS', false)]
#[Env('ATTRIBUTES_CACHE_ENABLED', true)]
public function testReaderBindingWithCache(): void
{
$this->assertContainerBoundAsSingleton(ReaderInterface::class, Psr16CachedReader::class);
}

#[Env('SUPPORT_ANNOTATIONS', true)]
public function testSelectiveReaderBinding(): void
{
$this->assertContainerBoundAsSingleton(ReaderInterface::class, SelectiveReader::class);
}
}

0 comments on commit 82ece5f

Please sign in to comment.