8000 [DX] [PropertyInfo] Filter PropertyInfo Extractors with Interfaces by zanbaldwin · Pull Request #16890 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DX] [PropertyInfo] Filter PropertyInfo Extractors with Interfaces #16890

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
[DX] Filter PropertyInfo Extractors with Interfaces
Use a single service tag to filter a central source of information extractor instances.
Since PropertyInfo requires PHP 5.5+, use ::class constant to negate the use of class constants or strings.
  • Loading branch information
Zander Baldwin committed Dec 7, 2015
commit 159fd83a15bfc9ff0e4fcdfb650cb49de43011a9
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,8 @@ public function process(ContainerBuilder $container)
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);
$extractors = $this->findAndSortTaggedServices('property_info.extractor', $container);
$container->getDefinition('property_info')->replaceArgument(0, $extractors);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,7 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild

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));
$definition->addTag('property_info.extractor', array('priority' => -1001));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@
<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\Extractor\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" />
<tag name="property_info.extractor" priority="-1000" />
</service>
</services>
</container>
12 changes: 12 additions & 0 deletions src/Symfony/Component/PropertyInfo/ExtractorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Symfony\Component\PropertyInfo;

/**
* Interface ExtractorInterface.
*
* @author Zander Baldwin <hello@zanderbaldwin.com>
*/
interface ExtractorInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface PropertyAccessExtractorInterface
interface PropertyAccessExtractorInterface extends ExtractorInterface
{
/**
* Is the property readable?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface PropertyDescriptionExtractorInterface
interface PropertyDescriptionExtractorInterface extends ExtractorInterface
{
/**
* Gets the short description of the property.
Expand Down
86 changes: 51 additions & 35 deletions src/Symfony/Component/PropertyInfo/PropertyInfoExtractor.php
Original file line number Diff line n 628C umber Diff line change
Expand Up @@ -19,102 +19,118 @@
class PropertyInfoExtractor implements PropertyInfoExtractorInterface
{
/**
* @var PropertyListExtractorInterface[]
* @var \SplObjectStorage
*/
private $listExtractors;
private $extractors;

/**
* @var PropertyTypeExtractorInterface[]
* @param ExtractorInterface[] $extractors
*/
private $typeExtractors;

/**
* @var PropertyDescriptionExtractorInterface[]
*/
private $descriptionExtractors;

/**
* @var PropertyAccessExtractorInterface[]
*/
private $accessExtractors;
public function __construct(array $extractors = array())
{
$this->extractors = new \SplObjectStorage();
foreach ($extractors as $extractor) {
$this->addExtractor($extractor);
}
}

/**
* @param PropertyListExtractorInterface[] $listExtractors
* @param PropertyTypeExtractorInterface[] $typeExtractors
* @param PropertyDescriptionExtractorInterface[] $descriptionExtractors
* @param PropertyAccessExtractorInterface[] $accessExtractors
* {@inheritdoc}
*/
public function __construct(array $listExtractors = array(), array $typeExtractors = array(), array $descriptionExtractors = array(), array $accessExtractors = array())
public function addExtractor(ExtractorInterface $extractor)
{
$this->listExtractors = $listExtractors;
$this->typeExtractors = $typeExtractors;
$this->descriptionExtractors = $descriptionExtractors;
$this->accessExtractors = $accessExtractors;
$this->extractors->attach($extractor);
}

/**
* {@inheritdoc}
*/
public function getProperties($class, array $context = array())
{
return $this->extract($this->listExtractors, 'getProperties', array($class, $context));
return $this->extract(
PropertyListExtractorInterface::class,
'getProperties',
array($class, $context)
);
}

/**
* {@inheritdoc}
*/
public function getShortDescription($class, $property, array $context = array())
{
return $this->extract($this->descriptionExtractors, 'getShortDescription', array($class, $property, $context));
return $this->extract(
PropertyDescriptionExtractorInterface::class,
'getShortDescription',
array($class, $property, $context)
);
}

/**
* {@inheritdoc}
*/
public function getLongDescription($class, $property, array $context = array())
{
return $this->extract($this->descriptionExtractors, 'getLongDescription', array($class, $property, $context));
return $this->extract(
PropertyDescriptionExtractorInterface::class,
'getLongDescription',
array($class, $property, $context)
);
}

/**
* {@inheritdoc}
*/
public function getTypes($class, $property, array $context = array())
{
return $this->extract($this->typeExtractors, 'getTypes', array($class, $property, $context));
return $this->extract(
PropertyTypeExtractorInterface::class,
'getTypes',
array($class, $property, $context)
);
}

/**
* {@inheritdoc}
*/
public function isReadable($class, $property, array $context = array())
{
return $this->extract($this->accessExtractors, 'isReadable', array($class, $property, $context));
return $this->extract(
PropertyAccessExtractorInterface::class,
'isReadable',
array($class, $property, $context)
);
}

/**
* {@inheritdoc}
*/
public function isWritable($class, $property, array $context = array())
{
return $this->extract($this->accessExtractors, 'isWritable', array($class, $property, $context));
return $this->extract(
PropertyAccessExtractorInterface::class,
'isWritable',
array($class, $property, $context)
);
}

/**
* Iterates over registered extractors and return the first value found.
*
* @param array $extractors
* @param string $interfaceFilter
* @param string $method
* @param array $arguments
*
* @return mixed
*/
private function extract(array $extractors, $method, array $arguments)
private function extract($interfaceFilter, $method, array $arguments)
{
foreach ($extractors as $extractor) {
$value = call_user_func_array(array($extractor, $method), $arguments);
if (null !== $value) {
return $value;
foreach ($this->extractors as $extractor) {
if ($extractor instanceof $interfaceFilter) {
$value = call_user_func_array(array($extractor, $method), $arguments);
if (null !== $value) {
return $value;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@
*/
interface PropertyInfoExtractorInterface extends PropertyTypeExtractorInterface, PropertyDescriptionExtractorInterface, PropertyAccessExtractorInterface, PropertyListExtractorInterface
{
/**
* @param ExtractorInterface $extractor
*/
public function addExtractor(ExtractorInterface $extractor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface PropertyListExtractorInterface
interface PropertyListExtractorInterface extends ExtractorInterface
{
/**
* Gets the list of properties available for the given class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface PropertyTypeExtractorInterface
interface PropertyTypeExtractorInterface extends ExtractorInterface
{
/**
* Gets types of a property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class PropertyInfoExtractorTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$extractors = array(new NullExtractor(), new DummyExtractor());
$this->propertyInfo = new PropertyInfoExtractor($extractors, $extractors, $extractors, $extractors);
$this->propertyInfo = new PropertyInfoExtractor(array(new NullExtractor(), new DummyExtractor()));
}

public function testInstanceOf()
Expand All @@ -38,6 +37,7 @@ public function testInstanceOf()
$this->assertInstanceOf('Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface', $this->propertyInfo);
$this->assertInstanceOf('Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface', $this->propertyInfo);
$this->assertInstanceOf('Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface', $this->propertyInfo);
$this->assertInstanceOf('Symfony\Component\PropertyInfo\ExtractorInterface', $this->propertyInfo);
}

public function testGetShortDescription()
Expand Down
0