-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[PropertyAccess] Allow custom methods on property accesses #18016
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
af3a824
5b82237
00a26eb
e4aaac9
e46df98
5f206e6
94e6157
09707ad
d02d93a
748b894
688cbda
003efdb
dc5cacb
1a35d45
1e29523
7c6a79c
d0bd777
6b7eeff
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 |
---|---|---|
|
@@ -138,7 +138,7 @@ public function load(array $configs, ContainerBuilder $container) | |
} | ||
|
||
$this->registerAnnotationsConfiguration($config['annotations'], $container, $loader); | ||
$this->registerPropertyAccessConfiguration($config['property_access'], $container); | ||
$this->registerPropertyAccessConfiguration($config['property_access'], $container, $loader); | ||
|
||
if ($this->isConfigEnabled($container, $config['serializer'])) { | ||
$this->r 8000 egisterSerializerConfiguration($config['serializer'], $container, $loader); | ||
|
@@ -915,13 +915,90 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde | |
} | ||
} | ||
|
||
private function registerPropertyAccessConfiguration(array $config, ContainerBuilder $container) | ||
/** | ||
* Loads the PropertyAccess configuration. | ||
* | ||
* @param array $config A serializer configuration array | ||
* @param ContainerBuilder $container A ContainerBuilder instance | ||
* @param XmlFileLoader $loader An XmlFileLoader instance | ||
*/ | ||
private function registerPropertyAccessConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader) | ||
{ | ||
$container | ||
->getDefinition('property_accessor') | ||
->replaceArgument(0, $config['magic_call']) | ||
->replaceArgument(1, $config['throw_exception_on_invalid_index']) | ||
; | ||
|
||
if (!$this->isConfigEnabled($container, $config)) { | ||
return; | ||
} | ||
|
||
$loader->load('property_access.xml'); | ||
$chainLoader = $container->getDefinition('property_access.mapping.chain_loader'); | ||
|
||
$serializerLoaders = array(); | ||
if (isset($config['enable_annotations']) && $config['enable_annotations']) { | ||
$annotationLoader = new Definition( | ||
'Symfony\Component\PropertyAccess\Mapping\Loader\AnnotationLoader', | ||
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. Prefer 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. Ah, ok. That change would make the PR incompatible with PHP 5.4, but if its headed to Symfony 3.1 that shouldn't be a problem |
||
array(new Reference('annotation_reader')) | ||
); | ||
$annotationLoader->setPublic(false); | ||
|
||
$serializerLoaders[] = $annotationLoader; | ||
} | ||
|
||
$bundles = $container->getParameter('kernel.bundles'); | ||
foreach ($bundles as $bundle) { | ||
$reflection = new \ReflectionClass($bundle); | ||
$dirname = dirname($reflection->getFileName()); | ||
|
||
if (is_file($file = $dirname.'/Resources/config/property_access.xml')) { | ||
$definition = new Definition('Symfony\Component\PropertyAccess\Mapping\Loader\XmlFileLoader', array(realpath($file))); | ||
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.
|
||
$definition->setPublic(false); | ||
|
||
$serializerLoaders[] = $definition; | ||
$container->addResource(new FileResource($file)); | ||
} | ||
|
||
if (is_file($file = $dirname.'/Resources/config/property_access.yml')) { | ||
$definition = new Definition('Symfony\Component\PropertyAccess\Mapping\Loader\YamlFileLoader', array(realpath($file))); | ||
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.
|
||
$definition->setPublic(false); | ||
|
||
$serializerLoaders[] = $definition; | ||
$container->addResource(new FileResource($file)); | ||
} | ||
|
||
if (is_dir($dir = $dirname.'/Resources/config/property_access')) { | ||
foreach (Finder::create()->files()->in($dir)->name('*.xml') as $file) { | ||
$definition = new Definition('Symfony\Component\PropertyAccess\Mapping\Loader\XmlFileLoader', array($file->getRealpath())); | ||
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. same here |
||
$definition->setPublic(false); | ||
|
||
$serializerLoaders[] = $definition; | ||
} | ||
foreach (Finder::create()->files()->in($dir)->name('*.yml') as $file) { | ||
$definition = new Definition('Symfony\Component\PropertyAccess\Mapping\Loader\YamlFileLoader', array($file->getRealpath())); | ||
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. same here |
||
$definition->setPublic(false); | ||
|
||
$serializerLoaders[] = $definition; | ||
} | ||
|
||
$container->addResource(new DirectoryResource($dir)); | ||
} | ||
} | ||
|
||
$chainLoader->replaceArgument(0, $serializerLoaders); | ||
|
||
if (isset($config['cache']) && $config['cache']) { | ||
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 should be delayed until #17868 is merged. 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. Sure, it makes sense 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'm not sure #17868 will be merged before the 3.1 feature freeze. Could we skip this and work on it after the new Cache component is ready? 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 should use the new |
||
$container->setParameter( | ||
'property_access.mapping.cache.prefix', | ||
'property_access_'.$this->getKernelRootHash($container) | ||
); | ||
|
||
$container->getDefinition('property_access.mapping.class_metadata_factory')->replaceArgument( | ||
1, new Reference($config['cache']) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,30 @@ | |
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
|
||
<!-- Loader --> | ||
<service id="property_access.mapping.chain_loader" class="Symfony\Component\PropertyAccess\Mapping\Loader\LoaderChain" public="false"> | ||
<argument type="collection" /> | ||
</service> | ||
|
||
<!-- Class Metadata Factory --> | ||
<service id="property_access.mapping.class_metadata_factory" class="Symfony\Component\PropertyAccess\Mapping\Factory\ClassMetadataFactory" public="false"> | ||
<argument type="service" id="property_access.mapping.chain_loader" /> | ||
<argument>null</argument> | ||
</service> | ||
|
||
<!-- Cache --> | ||
<service id="property_access.mapping.cache.apc" class="Doctrine\Common\Cache\ApcCache" 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. Should be removed and use PSR-6 instead. 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. @dunglas, could you point me to some leads on how to proceed with this one? All I've found is some sparse WIP documentation about PSR-6 in Symfony |
||
<call method="setNamespace"> | ||
<argument>%property_access.mapping.cache.prefix%</argument> | ||
</call> | ||
</service> | ||
|
||
<service id="property_accessor" class="Symfony\Component\PropertyAccess\PropertyAccessor" > | ||
<argument /> <!-- magicCall, set by the extension --> | ||
<argument /> <!-- throwExceptionOnInvalidIndex, set by the extension --> | ||
<argument type="service" id="cache.property_access" on-invalid="ignore" /> | ||
<argument type="service" id="annotation_reader" on-invalid="ignore" /> | ||
<argument type="service" id="property_access.mapping.class_metadata_factory" on-invalid="ignore" /> | ||
</service> | ||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ framework: | |
property_access: | ||
magic_call: true | ||
throw_exception_on_invalid_index: true | ||
enable_annotations: true | ||
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 option is disabled in the PHP fixtures. Shouldn't it be the same here? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?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\PropertyAccess\Exception; | ||
|
||
/** | ||
* MappingException. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to o 2254 thers. Learn more. Weird too 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. Same as above, I just changed the class name and namespace... 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 agree, you shouldn't put other authors on your own code. |
||
*/ | ||
class MappingException extends RuntimeException | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
<?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\PropertyAccess\Mapping; | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class AttributeMetadata implements AttributeMetadataInterface | ||
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. If we introduce new metadata classes (value objects), I propose to make them immutable like I've done in the PropertyInfo component (and API Platform v2): https://github.com/api-platform/core/blob/master/src/Metadata/Property/ItemMetadata.php 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. Sure, I'll turn it immutable. 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 should be named PropertyMetadata to keep a consistent language with the rest of the component. Naming the same thing "property" once and "attribute" another time is confusing. 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've also renamed the nodes in the XSD and YAML so it is always "property" |
||
{ | ||
/** | ||
* @var string | ||
* | ||
* @internal This property is public in order to reduce the size of the | ||
* class' serialized representation. Do not access it. Use | ||
* {@link getName()} instead. | ||
*/ | ||
public $name; | ||
|
||
/** | ||
* @var string | ||
* | ||
* @internal This property is public in order to reduce the size of the | ||
* class' serialized representation. Do not access it. Use | ||
* {@link getGetter()} instead. | ||
*/ | ||
public $getter; | ||
|
||
/** | ||
* @var string | ||
* | ||
* @internal This property is public in order to reduce the size of the | ||
* class' serialized representation. Do not access it. Use | ||
* {@link getSetter()} instead. | ||
*/ | ||
public $setter; | ||
|
||
/** | ||
* @var string | ||
* | ||
* @internal This property is public in order to reduce the size of the | ||
* class' serialized representation. Do not access it. Use | ||
* {@link getAdder()} instead. | ||
*/ | ||
public $adder; | ||
|
||
/** | ||
* @var string | ||
* | ||
* @internal This property is public in order to reduce the size of the | ||
* class' serialized representation. Do not access it. Use | ||
* {@link getRemover()} instead. | ||
*/ | ||
public $remover; | ||
|
||
/** | ||
* Constructs a metadata for the given attribute. | ||
* | ||
* @param string $name | ||
*/ | ||
public function __construct($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSetter() | ||
{ | ||
return $this->setter; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setSetter($setter) | ||
{ | ||
$this->setter = $setter; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getGetter() | ||
{ | ||
return $this->getter; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setGetter($getter) | ||
{ | ||
$this->getter = $getter; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getAdder() | ||
{ | ||
return $this->adder; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setAdder($adder) | ||
{ | ||
$this->adder = $adder; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRemover() | ||
{ | ||
return $this->remover; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setRemover($remover) | ||
{ | ||
$this->remover = $remover; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function merge(AttributeMetadataInterface $attributeMetadata) | ||
{ | ||
// Overwrite only if not defined | ||
if (null === $this->getter) { | ||
$this->getter = $attributeMetadata->getGetter(); | ||
} | ||
if (null === $this->setter) { | ||
$this->setter = $attributeMetadata->getSetter(); | ||
} | ||
if (null === $this->adder) { | ||
$this->adder = $attributeMetadata->getAdder(); | ||
} | ||
if (null === $this->remover) { | ||
$this->remover = $attributeMetadata->getRemover(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the names of the properties that should be serialized. | ||
* | ||
* @return string[] | ||
*/ | ||
public function __sleep() | ||
{ | ||
return array('name', 'getter', 'setter', 'adder', 'remover'); | ||
} | ||
} |
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.
no need for this docblock imo