Skip to content

Commit

Permalink
[FrameworkBundle][PropertyInfo] Wire the ConstructorExtractor class
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Feb 22, 2024
1 parent 73f8713 commit d2fcfbc
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ CHANGELOG
* Add `rate_limiter` tags to rate limiter services
* Add `secrets:reveal` command
* Add `rate_limiter` option to `http_client.default_options` and `http_client.scoped_clients`
* Wire the `Symfony\Component\PropertyInfo\Extractor\ConstructorExtractor` class
* Add new `framework.property_info.with_constructor_extractor` option to
allow enabling or disabling the constructor extractor integration

7.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class UnusedTagsPass implements CompilerPassInterface
'monolog.logger',
'notifier.channel',
'property_info.access_extractor',
'property_info.constructor_extractor',
'property_info.initializable_extractor',
'property_info.list_extractor',
'property_info.type_extractor',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,14 @@ private function addPropertyInfoSection(ArrayNodeDefinition $rootNode, callable
->children()
->arrayNode('property_info')
->info('Property info configuration')
->addDefaultsIfNotSet()
->{$enableIfStandalone('symfony/property-info', PropertyInfoExtractorInterface::class)}()
->children()
->booleanNode('with_constructor_extractor')
->info('Registers the constructor extractor.')
->defaultFalse()
->end()
->end()
->end()
->end()
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
use Symfony\Component\Notifier\Transport\TransportFactoryInterface as NotifierTransportFactoryInterface;
use Symfony\Component\Process\Messenger\RunProcessMessageHandler;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyInfo\Extractor\ConstructorArgumentTypeExtractorInterface;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
Expand Down Expand Up @@ -398,7 +399,7 @@ public function load(array $configs, ContainerBuilder $container): void
}

if ($propertyInfoEnabled) {
$this->registerPropertyInfoConfiguration($container, $loader);
$this->registerPropertyInfoConfiguration($config['property_info'], $container, $loader);
}

if ($this->readConfigEnabled('lock', $container, $config['lock'])) {
Expand Down Expand Up @@ -630,6 +631,8 @@ public function load(array $configs, ContainerBuilder $container): void
->addTag('property_info.list_extractor');
$container->registerForAutoconfiguration(PropertyTypeExtractorInterface::class)
->addTag('property_info.type_extractor');
$container->registerForAutoconfiguration(ConstructorArgumentTypeExtractorInterface::class)
->addTag('property_info.constructor_extractor');
$container->registerForAutoconfiguration(PropertyDescriptionExtractorInterface::class)
->addTag('property_info.description_extractor');
$container->registerForAutoconfiguration(PropertyAccessExtractorInterface::class)
Expand Down Expand Up @@ -1936,26 +1939,32 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
}
}

private function registerPropertyInfoConfiguration(ContainerBuilder $container, PhpFileLoader $loader): void
private function registerPropertyInfoConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader): void
{
if (!interface_exists(PropertyInfoExtractorInterface::class)) {
throw new LogicException('PropertyInfo support cannot be enabled as the PropertyInfo component is not installed. Try running "composer require symfony/property-info".');
}

$loader->load('property_info.php');

if (!$config['with_constructor_extractor']) {
$container->removeDefinition('property_info.constructor_extractor');
}

if (
ContainerBuilder::willBeAvailable('phpstan/phpdoc-parser', PhpDocParser::class, ['symfony/framework-bundle', 'symfony/property-info'])
&& ContainerBuilder::willBeAvailable('phpdocumentor/type-resolver', ContextFactory::class, ['symfony/framework-bundle', 'symfony/property-info'])
) {
$definition = $container->register('property_info.phpstan_extractor', PhpStanExtractor::class);
$definition->addTag('property_info.type_extractor', ['priority' => -1000]);
$definition->addTag('property_info.constructor_extractor', ['priority' => -1000]);
}

if (ContainerBuilder::willBeAvailable('phpdocumentor/reflection-docblock', DocBlockFactoryInterface::class, ['symfony/framework-bundle', 'symfony/property-info'], true)) {
$definition = $container->register('property_info.php_doc_extractor', PhpDocExtractor::class);
$definition->addTag('property_info.description_extractor', ['priority' => -1000]);
$definition->addTag('property_info.type_extractor', ['priority' => -1001]);
$definition->addTag('property_info.constructor_extractor', ['priority' => -1001]);
}

if ($container->getParameter('kernel.debug')) {
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Messenger\DependencyInjection\MessengerPass;
use Symfony\Component\Mime\DependencyInjection\AddMimeTypeGuesserPass;
use Symfony\Component\PropertyInfo\DependencyInjection\PropertyInfoConstructorPass;
use Symfony\Component\PropertyInfo\DependencyInjection\PropertyInfoPass;
use Symfony\Component\Routing\DependencyInjection\AddExpressionLanguageProvidersPass;
use Symfony\Component\Routing\DependencyInjection\RoutingResolverPass;
Expand Down Expand Up @@ -152,6 +153,7 @@ public function build(ContainerBuilder $container): void
$container->addCompilerPass(new FragmentRendererPass());
$this->addCompilerPassIfExists($container, SerializerPass::class);
$this->addCompilerPassIfExists($container, PropertyInfoPass::class);
$this->addCompilerPassIfExists($container, PropertyInfoConstructorPass::class);
$container->addCompilerPass(new ControllerArgumentValueResolverPass());
$container->addCompilerPass(new CachePoolPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 32);
$container->addCompilerPass(new CachePoolClearerPass(), PassConfig::TYPE_AFTER_REMOVING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\PropertyInfo\Extractor\ConstructorExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
Expand Down Expand Up @@ -43,9 +44,14 @@
->set('property_info.reflection_extractor', ReflectionExtractor::class)
->tag('property_info.list_extractor', ['priority' => -1000])
->tag('property_info.type_extractor', ['priority' => -1002])
->tag('property_info.constructor_extractor', ['priority' => -1002])
->tag('property_info.access_extractor', ['priority' => -1000])
->tag('property_info.initializable_extractor', ['priority' => -1000])

->set('property_info.constructor_extractor', ConstructorExtractor::class)
->args([[]])
->tag('property_info.type_extractor', ['priority' => -999])

->alias(PropertyReadInfoExtractorInterface::class, 'property_info.reflection_extractor')
->alias(PropertyWriteInfoExtractorInterface::class, 'property_info.reflection_extractor')
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@

<xsd:complexType name="property_info">
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="with-constructor-extractor" type="xsd:boolean" />
</xsd:complexType>

<xsd:complexType name="cache">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ protected static function getBundleDefaultConfig()
],
'property_info' => [
'enabled' => !class_exists(FullStack::class),
'with_constructor_extractor' => false,
],
'router' => [
'enabled' => false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$container->loadFromExtension('framework', [
'annotations' => false,
'http_method_override' => false,
'handle_all_throwables' => true,
'php_errors' => ['log' => true],
'property_info' => [
'enabled' => true,
'with_constructor_extractor' => true,
],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config http-method-override="false" handle-all-throwables="true">
<framework:annotations enabled="false" />
<framework:php-errors log="true" />
<framework:property-info enabled="true" with-constructor-extractor="true" />
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
framework:
annotations: false
http_method_override: false
handle_all_throwables: true
php_errors:
log: true
property_info:
enabled: true
with_constructor_extractor: true
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,14 @@ public function testPropertyInfoEnabled()
{
$container = $this->createContainerFromFile('property_info');
$this->assertTrue($container->has('property_info'));
$this->assertFalse($container->has('property_info.constructor_extractor'));
}

public function testPropertyInfoWithConstructorExtractorEnabled()
{
$container = $this->createContainerFromFile('property_info_with_constructor_extractor');
$this->assertTrue($container->has('property_info'));
$this->assertTrue($container->has('property_info.constructor_extractor'));
}

public function testPropertyInfoCacheActivated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* Infers the constructor argument type.
*
* @author Dmitrii Poddubnyi <dpoddubny@gmail.com>
*
* @internal
*/
interface ConstructorArgumentTypeExtractorInterface
{
Expand Down

0 comments on commit d2fcfbc

Please sign in to comment.