Skip to content

Commit 2be7e06

Browse files
committed
[TypeInfo] Add extra type alias support
1 parent 0f1cbf3 commit 2be7e06

File tree

17 files changed

+114
-11
lines changed

17 files changed

+114
-11
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Allow using their name without added suffix when using `#[Target]` for custom services
99
* Deprecate `Symfony\Bundle\FrameworkBundle\Console\Application::add()` in favor of `Symfony\Bundle\FrameworkBundle\Console\Application::addCommand()`
1010
* Add `assertEmailAddressNotContains()` to the `MailerAssertionsTrait`
11+
* Add `framework.type_info.aliases` option
1112

1213
7.3
1314
---

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,17 @@ private function addTypeInfoSection(ArrayNodeDefinition $rootNode, callable $ena
13041304
->arrayNode('type_info')
13051305
->info('Type info configuration')
13061306
->{$enableIfStandalone('symfony/type-info', Type::class)}()
1307+
->addDefaultsIfNotSet()
1308+
->fixXmlConfig('alias', 'aliases')
1309+
->children()
1310+
->arrayNode('aliases')
1311+
->info('Additional type aliases to be used during type context creation.')
1312+
->defaultValue([])
1313+
->normalizeKeys(false)
1314+
->useAttributeAsKey('name')
1315+
->scalarPrototype()->end()
1316+
->end()
1317+
->end()
13071318
->end()
13081319
->end()
13091320
;

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ public function load(array $configs, ContainerBuilder $container): void
469469
}
470470

471471
if ($typeInfoEnabled = $this->readConfigEnabled('type_info', $container, $config['type_info'])) {
472-
$this->registerTypeInfoConfiguration($container, $loader);
472+
$this->registerTypeInfoConfiguration($config['type_info'], $container, $loader);
473473
}
474474

475475
if ($propertyInfoEnabled) {
@@ -2170,7 +2170,7 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild
21702170
}
21712171
}
21722172

2173-
private function registerTypeInfoConfiguration(ContainerBuilder $container, PhpFileLoader $loader): void
2173+
private function registerTypeInfoConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader): void
21742174
{
21752175
if (!class_exists(Type::class)) {
21762176
throw new LogicException('TypeInfo support cannot be enabled as the TypeInfo component is not installed. Try running "composer require symfony/type-info".');
@@ -2179,7 +2179,8 @@ private function registerTypeInfoConfiguration(ContainerBuilder $container, PhpF
21792179
$loader->load('type_info.php');
21802180

21812181
if (ContainerBuilder::willBeAvailable('phpstan/phpdoc-parser', PhpDocParser::class, ['symfony/framework-bundle', 'symfony/type-info'])) {
2182-
$container->register('type_info.resolver.string', StringTypeResolver::class);
2182+
$container->register('type_info.resolver.string', StringTypeResolver::class)
2183+
->setArguments([null, null, $config['aliases']]);
21832184

21842185
$container->register('type_info.resolver.reflection_parameter.phpdoc_aware', PhpDocAwareReflectionTypeResolver::class)
21852186
->setArguments([new Reference('type_info.resolver.reflection_parameter'), new Reference('type_info.resolver.string'), new Reference('type_info.type_context_factory')]);
@@ -2196,6 +2197,8 @@ private function registerTypeInfoConfiguration(ContainerBuilder $container, PhpF
21962197
\ReflectionProperty::class => new Reference('type_info.resolver.reflection_property.phpdoc_aware'),
21972198
\ReflectionFunctionAbstract::class => new Reference('type_info.resolver.reflection_return.phpdoc_aware'),
21982199
] + $resolversLocator->getValues());
2200+
2201+
$container->getDefinition('type_info.type_context_factory')->replaceArgument(1, $config['aliases']);
21992202
}
22002203
}
22012204

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,18 @@
382382
</xsd:complexType>
383383

384384
<xsd:complexType name="type_info">
385-
<xsd:attribute name="enabled" type="xsd:boolean" />
385+
<xsd:sequence>
386+
<xsd:element name="alias" minOccurs="0" maxOccurs="unbounded">
387+
<xsd:complexType>
388+
<xsd:simpleContent>
389+
<xsd:extension base="xsd:string">
390+
<xsd:attribute name="name" type="xsd:string" use="required"/>
391+
</xsd:extension>
392+
</xsd:simpleContent>
393+
</xsd:complexType>
394+
</xsd:element>
395+
</xsd:sequence>
396+
<xsd:attribute name="enabled" type="xsd:boolean"/>
386397
</xsd:complexType>
387398

388399
<xsd:complexType name="named_serializer_options">

src/Symfony/Bundle/FrameworkBundle/Resources/config/type_info.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
$container->services()
2424
// type context
2525
->set('type_info.type_context_factory', TypeContextFactory::class)
26-
->args([service('type_info.resolver.string')->nullOnInvalid()])
26+
->args([
27+
service('type_info.resolver.string')->nullOnInvalid(),
28+
[],
29+
])
2730

2831
// type resolvers
2932
->set('type_info.resolver', TypeResolver::class)

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ protected static function getBundleDefaultConfig()
812812
],
813813
'type_info' => [
814814
'enabled' => !class_exists(FullStack::class) && class_exists(Type::class),
815+
'aliases' => [],
815816
],
816817
'property_info' => [
817818
'enabled' => !class_exists(FullStack::class),

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/type_info.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
'php_errors' => ['log' => true],
88
'type_info' => [
99
'enabled' => true,
10+
'aliases' => [
11+
'CustomAlias' => 'int',
12+
],
1013
],
1114
]);

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/type_info.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<framework:config http-method-override="false" handle-all-throwables="true">
99
<framework:annotations enabled="false" />
1010
<framework:php-errors log="true" />
11-
<framework:type-info enabled="true" />
11+
<framework:type-info enabled="true">
12+
<framework:alias name="CustomAlias">int</framework:alias>
13+
</framework:type-info>
1214
</framework:config>
1315
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/type_info.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ framework:
66
log: true
77
type_info:
88
enabled: true
9+
aliases:
10+
CustomAlias: int

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/TypeInfoTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPStan\PhpDocParser\Parser\PhpDocParser;
1515
use Symfony\Bundle\FrameworkBundle\Tests\Functional\app\TypeInfo\Dummy;
16+
use Symfony\Component\HttpKernel\Kernel;
1617
use Symfony\Component\TypeInfo\Type;
1718

1819
class TypeInfoTest extends AbstractWebTestCase
@@ -28,5 +29,10 @@ public function testComponent()
2829
}
2930

3031
$this->assertEquals(Type::int(), static::getContainer()->get('type_info.resolver')->resolve('int'));
32+
33+
if (Kernel::VERSION_ID >= 70400) {
34+
$this->assertEquals(Type::int(), static::getContainer()->get('type_info.resolver')->resolve(new \ReflectionProperty(Dummy::class, 'customAlias')));
35+
$this->assertEquals(Type::int(), static::getContainer()->get('type_info.resolver')->resolve('CustomAlias'));
36+
}
3137
}
3238
}

0 commit comments

Comments
 (0)