8000 [FrameworkBundle][PropertyInfo] Wire the `ConstructorExtractor` class by HypeMC · Pull Request #50334 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle][PropertyInfo] Wire the ConstructorExtractor class #50334

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

Merged
merged 1 commit into from
Jan 6, 2025
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
6 changes: 6 additions & 0 deletions UPGRADE-7.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ Read more about this in the [Symfony documentation](https://symfony.com/doc/7.3/

If you're upgrading from a version below 7.1, follow the [7.2 upgrade guide](UPGRADE-7.2.md) first.

FrameworkBundle
---------------

* Not setting the `framework.property_info.with_constructor_extractor` option explicitly is deprecated
because its default value will change in version 8.0

Serializer
----------

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add support for assets pre-compression
* Rename `TranslationUpdateCommand` to `TranslationExtractCommand`
* Add JsonEncoder services and configuration
* Add new `framework.property_info.with_constructor_extractor` option to allow enabling or disabling the constructor extractor integration

7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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 @@ -1226,8 +1226,23 @@ private function addPropertyInfoSection(ArrayNodeDefinition $rootNode, callable
->arrayNode('property_info')
->info('Property info configuration')
->{$enableIfStandalone('symfony/property-info', PropertyInfoExtractorInterface::class)}()
->children()
->booleanNode('with_constructor_extractor')
->info('Registers the constructor extractor.')
->end()
->end()
->end()
->end()
->validate()
->ifTrue(fn ($v) => $v['property_info']['enabled'] && !isset($v['property_info']['with_constructor_extractor']))
->then(function ($v) {
$v['property_info']['with_constructor_extractor'] = false;

trigger_deprecation('symfony/property-info', '7.3', 'Not setting the "with_constructor_extractor" option explicitly is deprecated because its default value will change in version 8.0.');

return $v;
})
->end()
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,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 @@ -427,7 +428,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('json_encoder', $container, $config['json_encoder'])) {
Expand Down Expand Up @@ -657,6 +658,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 @@ -2040,26 +2043,32 @@ private function registerJsonEncoderConfiguration(array $config, ContainerBuilde
}
}

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 @@ -56,6 +56,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 @@ -164,6 +165,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 @@ -370,6 +370,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 @@ -808,7 +808,7 @@ protected static function getBundleDefaultConfig()
],
'property_info' => [
'enabled' => !class_exists(FullStack::class),
],
] + (!class_exists(FullStack::class) ? ['with_constructor_extractor' => false] : []),
'router' => [
'enabled' => false,
'default_uri' => null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@
],
],
],
'property_info' => true,
'property_info' => [
'enabled' => true,
'with_constructor_extractor' => true,
],
'type_info' => true,
'ide' => 'file%%link%%format',
'request' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
'php_errors' => ['log' => true],
'property_info' => [
'enabled' => true,
'with_constructor_extractor' => false,
],
]);
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
Expand Up @@ -5,7 +5,10 @@
'http_method_override' => false,
'handle_all_throwables' => true,
'php_errors' => ['log' => true],
'property_info' => ['enabled' => true],
'property_info' => [
'enabled' => true,
'with_constructor_extractor' => true,
],
'validation' => [
'email_validation_mode' => 'html5',
'auto_mapping' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</framework:default-context>
</framework:named-serializer>
</framework:serializer>
<framework:property-info />
<framework:property-info with-constructor-extractor="true" />
<framework:type-info />
<framework:json-encoder />
</framework:config>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<framework:config http-method-override="false" handle-all-throwables="true">
<framework:annotations enabled="false" />
<framework:php-errors log="true" />
<framework:property-info enabled="true" />
<framework:property-info enabled="true" with-constructor-extractor="false" />
</framework:config>
</container>
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
Expand Up @@ -6,7 +6,7 @@
<framework:config http-method-override="false" handle-all-throwables="true">
<framework:annotations enabled="false" />
<framework:php-errors log="true" />
<framework:property-info enabled="true" />
<framework:property-info enabled="true" with-constructor-extractor="true" />
<framework:validation email-validation-mode="html5">
<framework:auto-mapping namespace="App\">
<framework:service>foo</framework:service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ framework:
default_context:
enable_max_depth: false
type_info: ~
property_info: ~
property_info:
with_constructor_extractor: true
ide: file%%link%%format
request:
formats:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ framework:
log: true
property_info:
enabled: true
with_constructor_extractor: false
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 @@ -4,7 +4,9 @@ framework:
handle_all_throwables: true
php_errors:
log: true
property_info: { enabled: true }
property_info:
enabled: true
with_constructor_extractor: true
validation:
email_validation_mode: html5
auto_mapping:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,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 @@ -5,4 +5,6 @@ framework:
serializer:
enabled: true
validation: true
property_info: { enabled: true }
property_info:
enabled: true
with_constructor_extractor: true
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ framework:
translator: true
validation: true
serializer: true
property_info: true
property_info:
enabled: true
with_constructor_extractor: true
csrf_protection: true
form: true
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ framework:
max_depth_handler: Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Serializer\MaxDepthHandler
default_context:
enable_max_depth: true
property_info: { enabled: true }
property_info:
enabled: true
with_constructor_extractor: true

services:
serializer.alias:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,20 @@
* Infers the constructor argument type.
*
* @author Dmitrii Poddubnyi <dpoddubny@gmail.com>
*
* @internal
*/
interface ConstructorArgumentTypeExtractorInterface
{
/**
* Gets types of an argument from constructor.
*
* @return LegacyType[]|null
*
* @internal
*/
public function getTypesFromConstructor(string $class, string $property): ?array;

/**
* Gets type of an argument from constructor.
*
* @param class-string $class
*
* @internal
*/
public function getTypeFromConstructor(string $class, string $property): ?Type;
}
0