-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[FrameworkBundle] PropertyInfo support #15966
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
Changes from 1 commit
4fed49a
b60f509
3359451
790dd5a
85b5396
4705b21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?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\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Adds extractors to the property_info_extractor service. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class PropertyInfoPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition('property_info_extractor')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the service with name |
||
return; | ||
} | ||
|
||
$listExtractors = $this->findAndSortTaggedServices('property_info', $container); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $listExtractors = $this->findAndSortTaggedServices('property_info.list_extractor', $container); |
||
$container->getDefinition('property_info_extractor')->replaceArgument(0, $listExtractors); | ||
|
||
$typeExtractors = $this->findAndSortTaggedServices('property_info', $container); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $typeExtractors = $this->findAndSortTaggedServices('property_info.type_extractor', $container); |
||
$container->getDefinition('property_info.type_extractor')->replaceArgument(1, $typeExtractors); | ||
|
||
$descriptionExtractors = $this->findAndSortTaggedServices('property_info', $container); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $descriptionExtractors = $this->findAndSortTaggedServices('property_info.description_extractor', $container); |
||
$container->getDefinition('property_info.description_extractor')->replaceArgument(2, $descriptionExtractors); | ||
|
||
$accessExtractors = $this->findAndSortTaggedServices('property_info', $container); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $accessExtractors = $this->findAndSortTaggedServices('property_info.access_extractor', $container); |
||
$container->getDefinition('property_info.access_extractor')->replaceArgument(3, $accessExtractors); | ||
} | ||
|
||
/** | ||
* Finds all services with the given tag name and order them by their priority. | ||
* | ||
* @param string $tagName | ||
* @param ContainerBuilder $container | ||
* | ||
* @return array | ||
*/ | ||
private function findAndSortTaggedServices($tagName, ContainerBuilder $container) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any way to factorize this method as you use it in all compiler pass. Might be useful for others. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to have it in a separate class but in the Serializer class the behavior is not exactly the same (an error is thrown if normalizer is added, this is not desirable here). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
{ | ||
$services = $container->findTaggedServiceIds($tagName); | ||
|
||
$sortedServices = array(); | ||
foreach ($services as $serviceId => $tags) { | ||
foreach ($tags as $tag) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we generally name it |
||
$priority = isset($tag['priority']) ? $tag['priority'] : 0; | ||
$sortedServices[$priority][] = new Reference($serviceId); | ||
} | ||
} | ||
|
||
krsort($sortedServices); | ||
|
||
// Flatten the array | ||
return call_user_func_array('array_merge', $sortedServices); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is broken in case there is no tagged service, because |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,6 +178,7 @@ public function getConfigTreeBuilder() | |
$this->addAnnotationsSection($rootNode); | ||
$this->addSerializerSection($rootNode); | ||
$this->addPropertyAccessSection($rootNode); | ||
$this->addPropertyInfoSection($rootNode); | ||
|
||
return $treeBuilder; | ||
} | ||
|
@@ -723,4 +724,17 @@ private function addPropertyAccessSection(ArrayNodeDefinition $rootNode) | |
->end() | ||
; | ||
} | ||
|
||
private function addPropertyInfoSection(ArrayNodeDefinition $rootNode) | ||
{ | ||
$rootNode | ||
->children() | ||
->arrayNode('property_info') | ||
->addDefaultsIfNotSet() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this call is not needed. |
||
->info('Property info configuration') | ||
->canBeEnabled() | ||
->end() | ||
->end() | ||
; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="property_info" class="Symfony\Component\PropertyInfo\PropertyInfoExtractor" > | ||
<argument type="collection" /> | ||
<argument type="collection" /> | ||
<argument type="collection" /> | ||
<argument type="collection" /> | ||
</service> | ||
|
||
<!-- Extractor --> | ||
<service id="property_info.reflection_extractor" public="false"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing property <service id="property_info.reflection_extractor" class="Symfony\Component\PropertyInfo\ReflectionExtractor" public="false" > |
||
<tag name="property_info.list_extractor" priority="-1000" /> | ||
<tag name="property_info.type_extractor" priority="-1000" /> | ||
<tag name="property_info.access_extractor" priority="-1000" /> | ||
</service> | ||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?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\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class PropertyInfoPassTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testServicesAreOrderedAccordingToPriority() | ||
{ | ||
$services = array( | ||
'n3' => array('tag' => array()), | ||
'n1' => array('tag' => array('priority' => 200)), | ||
'n2' => array('tag' => array('priority' => 100)), | ||
); | ||
|
||
$expected = array( | ||
new Reference('n1'), | ||
new Reference('n2'), | ||
new Reference('n3'), | ||
); | ||
|
||
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder', array('findTaggedServiceIds')); | ||
|
||
$container->expects($this->atLeastOnce()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest we avoid checking number of calls with stubs and use There's a similar case in the next test. |
||
->method('findTaggedServiceIds') | ||
->will($this->returnValue($services)); | ||
|
||
$propertyInfoPass = new PropertyInfoPass(); | ||
|
||
$method = new \ReflectionMethod( | ||
'Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass', | ||
'findAndSortTaggedServices' | ||
); | ||
$method->setAccessible(true); | ||
|
||
$actual = $method->invoke($propertyInfoPass, 'tag', $container); | ||
|
||
$this->assertEquals($expected, $actual); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
$container->loadFromExtension('framework', array( | ||
'property_info' => array( | ||
'enabled' => true, | ||
), | ||
)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?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 http://symfony.com/schema/dic/services/services-1.0.xsd | ||
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> | ||
|
||
<framework:config> | ||
<framework:property-info enabled="true" /> | ||
</framework:config> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
framework: | ||
property_info: | ||
enabled: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be registered in FrameworkBundle class