-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[PropertyInfo] ConstructorExtractor which has higher priority than PhpDocExtractor and ReflectionExtractor #30335
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/Symfony/Component/PropertyInfo/DependencyInjection/PropertyInfoConstructorPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyInfo\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Adds extractors to the property_info.constructor_extractor service. | ||
* | ||
* @author Dmitrii Poddubnyi <dpoddubny@gmail.com> | ||
*/ | ||
final class PropertyInfoConstructorPass implements CompilerPassInterface | ||
{ | ||
use PriorityTaggedServiceTrait; | ||
|
||
private $service; | ||
private $tag; | ||
|
||
public function __construct(string $service = 'property_info.constructor_extractor', string $tag = 'property_info.constructor_extractor') | ||
{ | ||
$this->service = $service; | ||
$this->tag = $tag; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition($this->service)) { | ||
return; | ||
} | ||
$definition = $container->getDefinition($this->service); | ||
|
||
$listExtractors = $this->findAndSortTaggedServices($this->tag, $container); | ||
$definition->replaceArgument(0, new IteratorArgument($listExtractors)); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Symfony/Component/PropertyInfo/Extractor/ConstructorArgumentTypeExtractorInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyInfo\Extractor; | ||
|
||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
/** | ||
* Infers the constructor argument type. | ||
* | ||
* @author Dmitrii Poddubnyi <dpoddubny@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
interface ConstructorArgumentTypeExtractorInterface | ||
{ | ||
/** | ||
* Gets types of an argument from constructor. | ||
* | ||
* @return Type[]|null | ||
* | ||
* @internal | ||
*/ | ||
public function getTypesFromConstructor(string $class, string $property): ?array; | ||
karser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
48 changes: 48 additions & 0 deletions
48
src/Symfony/Component/PropertyInfo/Extractor/ConstructorExtractor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyInfo\Extractor; | ||
|
||
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; | ||
|
||
/** | ||
* Extracts the constructor argument type using ConstructorArgumentTypeExtractorInterface implementations. | ||
* | ||
* @author Dmitrii Poddubnyi <dpoddubny@gmail.com> | ||
*/ | ||
final class ConstructorExtractor implements PropertyTypeExtractorInterface | ||
{ | ||
/** @var iterable|ConstructorArgumentTypeExtractorInterface[] */ | ||
private $extractors; | ||
|
||
/** | ||
* @param iterable|ConstructorArgumentTypeExtractorInterface[] $extractors | ||
*/ | ||
public function __construct(iterable $extractors = []) | ||
{ | ||
$this->extractors = $extractors; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTypes($class, $property, array $context = []) | ||
{ | ||
foreach ($this->extractors as $extractor) { | ||
$value = $extractor->getTypesFromConstructor($class, $property); | ||
if (null !== $value) { | ||
return $value; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...fony/Component/PropertyInfo/Tests/DependencyInjection/PropertyInfoConstructorPassTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyInfo\Tests\DependencyInjection; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\PropertyInfo\DependencyInjection\PropertyInfoConstructorPass; | ||
|
||
class PropertyInfoConstructorPassTest extends TestCase | ||
{ | ||
public function testServicesAreOrderedAccordingToPriority() | ||
{ | ||
$container = new ContainerBuilder(); | ||
|
||
$tag = 'property_info.constructor_extractor'; | ||
$definition = $container->register('property_info.constructor_extractor')->setArguments([null, null]); | ||
$container->register('n2')->addTag($tag, ['priority' => 100]); | ||
$container->register('n1')->addTag($tag, ['priority' => 200]); | ||
$container->register('n3')->addTag($tag); | ||
|
||
$pass = new PropertyInfoConstructorPass(); | ||
$pass->process($container); | ||
|
||
$expected = new IteratorArgument([ | ||
new Reference('n1'), | ||
new Reference('n2'), | ||
new Reference('n3'), | ||
]); | ||
$this->assertEquals($expected, $definition->getArgument(0)); | ||
} | ||
|
||
public function testReturningEmptyArrayWhenNoService() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$propertyInfoExtractorDefinition = $container->register('property_info.constructor_extractor') | ||
->setArguments([[]]); | ||
|
||
$pass = new PropertyInfoConstructorPass(); | ||
$pass->process($container); | ||
|
||
$this->assertEquals(new IteratorArgument([]), $propertyInfoExtractorDefinition->getArgument(0)); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Symfony/Component/PropertyInfo/Tests/Extractor/ConstructorExtractorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
karser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyInfo\Tests\Extractor; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\PropertyInfo\Extractor\ConstructorExtractor; | ||
use Symfony\Component\PropertyInfo\Tests\Fixtures\DummyExtractor; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
/** | ||
* @author Dmitrii Poddubnyi <dpoddubny@gmail.com> | ||
*/ | ||
class ConstructorExtractorTest extends TestCase | ||
{ | ||
/** | ||
* @var ConstructorExtractor | ||
*/ | ||
private $extractor; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->extractor = new ConstructorExtractor([new DummyExtractor()]); | ||
} | ||
|
||
public function testInstanceOf() | ||
{ | ||
$this->assertInstanceOf('Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface', $this->extractor); | ||
} | ||
|
||
public function testGetTypes() | ||
{ | ||
$this->assertEquals([new Type(Type::BUILTIN_TYPE_STRING)], $this->extractor->getTypes('Foo', 'bar', [])); | ||
} | ||
|
||
public function testGetTypes_ifNoExtractors() | ||
{ | ||
$extractor = new ConstructorExtractor([]); | ||
$this->assertNull($extractor->getTypes('Foo', 'bar', [])); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.