-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 all commits
4fed49a
b60f509
3359451
790dd5a
85b5396
4705b21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?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 service. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class PropertyInfoPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition('property_info')) { | ||
return; | ||
} | ||
|
||
$listExtractors = $this->findAndSortTaggedServices('property_info.list_extractor', $container); | ||
$container->getDefinition('property_info')->replaceArgument(0, $listExtractors); | ||
|
||
$typeExtractors = $this->findAndSortTaggedServices('property_info.type_extractor', $container); | ||
$container->getDefinition('property_info')->replaceArgument(1, $typeExtractors); | ||
|
||
$descriptionExtractors = $this->findAndSortTaggedServices('property_info.description_extractor', $container); | ||
$container->getDefinition('property_info')->replaceArgument(2, $descriptionExtractors); | ||
|
||
$accessExtractors = $this->findAndSortTaggedServices('property_info.access_extractor', $container); | ||
$container->getDefinition('property_info')->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 $attributes) { | ||
$priority = isset($attributes['priority']) ? $attributes['priority'] : 0; | ||
$sortedServices[$priority][] = new Reference($serviceId); | ||
} | ||
} | ||
|
||
if (empty($sortedServices)) { | ||
return array(); | ||
} | ||
|
||
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 |
---|---|---|
@@ -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" 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,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\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->any()) | ||
->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); | ||
} | ||
|
||
public function testReturningEmptyArrayWhenNoService() | ||
{ | ||
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder', array('findTaggedServiceIds')); | ||
|
||
$container->expects($this->any()) | ||
->method('findTaggedServiceIds') | ||
->will($this->returnValue(array())); | ||
|
||
$propertyInfoPass = new PropertyInfoPass(); | ||
|
||
$method = new \ReflectionMethod( | ||
'Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass', | ||
'findAndSortTaggedServices' | ||
); | ||
$method->setAccessible(true); | ||
|
||
$actual = $method->invoke($propertyInfoPass, 'tag', $container); | ||
|
||
$this->assertEquals(array(), $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