Skip to content

[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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[FrameworkBundle] PropertyInfo support
  • Loading branch information
dunglas committed Oct 10, 2015
commit 4fed49abf15627b106f1ac955ec44c44acbcf201
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
Copy link
Contributor

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

$container->addCompilerPass(new PropertyInfoPass());

{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('property_info_extractor')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the service with name property_info_extractor doesn't exist, you means property_info ?

return;
}

$listExtractors = $this->findAndSortTaggedServices('property_info', $container);
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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).

Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we generally name it foreach ($tags as $attributes)

$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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is broken in case there is no tagged service, because array_merge requires at least 1 argument

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public function process(ContainerBuilder $container)
$container->getDefinition('serializer')->replaceArgument(1, $encoders);
}

/**
* Finds all services with the given tag name and order them by their priority.
*
* @param string $tagName
* @param ContainerBuilder $container
*
* @return array
*
* @throws \RuntimeException
*/
private function findAndSortTaggedServices($tagName, ContainerBuilder $container)
{
$services = $container->findTaggedServiceIds($tagName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public function getConfigTreeBuilder()
$this->addAnnotationsSection($rootNode);
$this->addSerializerSection($rootNode);
$this->addPropertyAccessSection($rootNode);
$this->addPropertyInfoSection($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -723,4 +724,17 @@ private function addPropertyAccessSection(ArrayNodeDefinition $rootNode)
->end()
;
}

private function addPropertyInfoSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('property_info')
->addDefaultsIfNotSet()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this call is not needed. canBeEnabled already does it

->info('Property info configuration')
->canBeEnabled()
->end()
->end()
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerSerializerConfiguration($config['serializer'], $container, $loader);
}

if (isset($config['property_info'])) {
$this->registerPropertyInfoConfiguration($config['property_info'], $container, $loader);
}

$loader->load('debug_prod.xml');
$definition = $container->findDefinition('debug.debug_handlers_listener');

Expand Down Expand Up @@ -986,6 +990,28 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
}
}

/**
* Loads property info configuration.
*
* @param array $config
* @param ContainerBuilder $container
* @param XmlFileLoader $loader
*/
private function registerPropertyInfoConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
if (!$config['enabled']) {
return;
}

$loader->load('property_info.xml');

if (class_exists('phpDocumentor\Reflection\ClassReflector')) {
$definition = $container->register('property_info.php_doc_extractor', 'Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor');
$definition->addTag('property_info.description_extractor', array('priority' => -1000));
$definition->addTag('property_info.type_extractor', array('priority' => -1001));
}
}

/**
* Returns the base path for the XSD files.
*
Expand Down
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">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing property class

<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
Expand Up @@ -33,6 +33,7 @@
<xsd:element name="annotations" type="annotations" minOccurs="0" maxOccurs="1" />
<xsd:element name="property-access" type="property_access" minOccurs="0" maxOccurs="1" />
<xsd:element name="serializer" type="serializer" minOccurs="0" maxOccurs="1" />
<xsd:element name="property-info" type="property_info" minOccurs="0" maxOccurs="1" />
</xsd:all>

<xsd:attribute name="http-method-override" type="xsd:boolean" />
Expand Down Expand Up @@ -220,4 +221,8 @@
<xsd:attribute name="enable-annotations" type="xsd:boolean" />
<xsd:attribute name="name-converter" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="property_info">
<xsd:attribute name="enabled" type="xsd:boolean" />
</xsd:complexType>
</xsd:schema>
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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we avoid checking number of calls with stubs and use $this->any() instead. It's not important how many times a stubbed method is called. It's important that if it's called, it returns the stubbed value. Relying on number of calls to stubbed methods makes refactorings harder. It ties tests to the implementation.

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
Expand Up @@ -179,6 +179,9 @@ protected static function getBundleDefaultConfig()
'magic_call' => false,
'throw_exception_on_invalid_index' => false,
),
'property_info' => array(
'enabled' => false,
),
'assets' => array(
'version' => null,
'version_format' => '%%s?%%s',
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,18 @@ public function testSerializerServiceIsNotRegisteredWhenDisabled()
$this->assertFalse($container->hasDefinition('serializer'));
}

public function testPropertyInfoDisabled()
{
$container = $this->createContainerFromFile('default_config');
$this->assertFalse($container->has('property_info'));
}

public function testPropertyInfoEnabled()
{
$container = $this->createContainerFromFile('property_info');
$this->assertTrue($container->has('property_info'));
}

protected function createContainer(array $data = array())
{
return new ContainerBuilder(new ParameterBag(array_merge(array(
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@
"symfony/expression-language": "~2.6|~3.0.0",
"symfony/process": "~2.0,>=2.0.5|~3.0.0",
"symfony/validator": "~2.5|~3.0.0",
"symfony/yaml": "~2.0,>=2.0.5|~3.0.0"
"symfony/yaml": "~2.0,>=2.0.5|~3.0.0",
"symfony/property-info": "~2.8|~3.0.0",
"phpdocumentor/reflection": "^1.0.7"
},
"suggest": {
"symfony/console": "For using the console commands",
"symfony/finder": "For using the translation loader and cache warmer",
"symfony/form": "For using forms",
"symfony/serializer": "For using the serializer service",
"symfony/validator": "For using validation",
"symfony/yaml": "For using the debug:config and lint:yaml commands"
"symfony/yaml": "For using the debug:config and lint:yaml commands",
"symfony/property-info": "For using the property_info_extractor service",
},
"autoload": {
"psr-4": { "Symfony\\Bundle\\FrameworkBundle\\": "" }
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy