Skip to content

Commit

Permalink
Inject an AttributeReader when annotations are disabled
Browse files Browse the repository at this point in the history
This avoids triggering the fallback path of instantiating an (uncached)
annotation reader in the extensions in case annotations are disabled in
FrameworkBundle but the `doctrine/annotations` package is still
installed.
  • Loading branch information
stof committed Jun 10, 2024
1 parent a3ceb54 commit 6845226
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 11 deletions.
27 changes: 27 additions & 0 deletions src/DependencyInjection/Compiler/ReaderPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Stof\DoctrineExtensionsBundle\DependencyInjection\Compiler;

use Gedmo\Mapping\Driver\AttributeReader;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @internal
*/
final class ReaderPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if ($container->has('annotation_reader')) {
$container->setAlias('.stof_doctrine_extensions.reader', new Alias('annotation_reader', false));

return;
}

if (\PHP_VERSION_ID >= 80000) {
$container->register('.stof_doctrine_extensions.reader', AttributeReader::class);
}
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/blameable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<argument type="service" id="stof_doctrine_extensions.metadata_cache" />
</call>
<call method="setAnnotationReader">
<argument type="service" id="annotation_reader" on-invalid="ignore" />
<argument type="service" id=".stof_doctrine_extensions.reader" on-invalid="ignore" />
</call>
</service>

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/ip_traceable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<argument type="service" id="stof_doctrine_extensions.metadata_cache" />
</call>
<call method="setAnnotationReader">
<argument type="service" id="annotation_reader" on-invalid="ignore" />
<argument type="service" id=".stof_doctrine_extensions.reader" on-invalid="ignore" />
</call>
</service>

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/loggable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<argument type="service" id="stof_doctrine_extensions.metadata_cache" />
</call>
<call method="setAnnotationReader">
<argument type="service" id="annotation_reader" on-invalid="ignore" />
<argument type="service" id=".stof_doctrine_extensions.reader" on-invalid="ignore" />
</call>
</service>

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/reference_integrity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<argument type="service" id="stof_doctrine_extensions.metadata_cache" />
</call>
<call method="setAnnotationReader">
<argument type="service" id="annotation_reader" on-invalid="ignore" />
<argument type="service" id=".stof_doctrine_extensions.reader" on-invalid="ignore" />
</call>
</service>
</services>
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/sluggable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<argument type="service" id="stof_doctrine_extensions.metadata_cache" />
</call>
<call method="setAnnotationReader">
<argument type="service" id="annotation_reader" on-invalid="ignore" />
<argument type="service" id=".stof_doctrine_extensions.reader" on-invalid="ignore" />
</call>
</service>
</services>
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/softdeleteable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<argument type="service" id="clock" on-invalid="ignore" />
</call>
<call method="setAnnotationReader">
<argument type="service" id="annotation_reader" on-invalid="ignore" />
<argument type="service" id=".stof_doctrine_extensions.reader" on-invalid="ignore" />
</call>
</service>
</services>
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/sortable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<argument type="service" id="stof_doctrine_extensions.metadata_cache" />
</call>
<call method="setAnnotationReader">
<argument type="service" id="annotation_reader" on-invalid="ignore" />
<argument type="service" id=".stof_doctrine_extensions.reader" on-invalid="ignore" />
</call>
</service>
</services>
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/timestampable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<argument type="service" id="clock" on-invalid="ignore" />
</call>
<call method="setAnnotationReader">
<argument type="service" id="annotation_reader" on-invalid="ignore" />
<argument type="service" id=".stof_doctrine_extensions.reader" on-invalid="ignore" />
</call>
</service>
</services>
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/translatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<argument type="service" id="stof_doctrine_extensions.metadata_cache" />
</call>
<call method="setAnnotationReader">
<argument type="service" id="annotation_reader" on-invalid="ignore" />
<argument type="service" id=".stof_doctrine_extensions.reader" on-invalid="ignore" />
</call>
<call method="setDefaultLocale">
<argument>%stof_doctrine_extensions.default_locale%</argument>
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/tree.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<argument type="service" id="stof_doctrine_extensions.metadata_cache" />
</call>
<call method="setAnnotationReader">
<argument type="service" id="annotation_reader" on-invalid="ignore" />
<argument type="service" id=".stof_doctrine_extensions.reader" on-invalid="ignore" />
</call>
</service>
</services>
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/uploadable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<argument type="service" id="stof_doctrine_extensions.metadata_cache" />
</call>
<call method="setAnnotationReader">
<argument type="service" id="annotation_reader" on-invalid="ignore" />
<argument type="service" id=".stof_doctrine_extensions.reader" on-invalid="ignore" />
</call>

<call method="setDefaultFileInfoClass">
Expand Down
2 changes: 2 additions & 0 deletions src/StofDoctrineExtensionsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Stof\DoctrineExtensionsBundle;

use Stof\DoctrineExtensionsBundle\DependencyInjection\Compiler\ReaderPass;
use Stof\DoctrineExtensionsBundle\DependencyInjection\Compiler\ValidateExtensionConfigurationPass;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -16,5 +17,6 @@ class StofDoctrineExtensionsBundle extends Bundle
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new ValidateExtensionConfigurationPass());
$container->addCompilerPass(new ReaderPass());
}
}

0 comments on commit 6845226

Please sign in to comment.