8000 [PropertyAccess] Allow custom methods on property accesses by lrlopez · Pull Request #18016 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Closed
wants to merge 18 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
Prev Previous commit
Next Next commit
Added support for YAML and XML mapping
  • Loading branch information
lrlopez committed Jul 3, 2016
commit 5b82237b825195dabb5fb91dfb150d6f19122c9d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
*/
Copy link
Member

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

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',
Copy link
Member

Choose a reason for hiding this comment

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

Prefer AnnotationLoader::class

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

XmlFileLoader::class

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

Choose a reason for hiding this comment

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

YamlFileLoader::class

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

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

It should be delayed until #17868 is merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, it makes sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

It should use the new system.cache infrastructure: http://symfony.com/blog/new-in-symfony-3-1-cache-component

$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'])
);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Copy link
Member

Choose a reason for hiding this comment

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

Should be removed and use PSR-6 instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Up @@ -225,6 +225,7 @@ protected static function getBundleDefaultConfig()
'property_access' => array(
'magic_call' => false,
'throw_exception_on_invalid_index' => false,
'enable_annotations' => false,
),
'property_info' => array(
'enabled' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
'property_access' => array(
'magic_call' => true,
'throw_exception_on_invalid_index' => true,
'enable_annotations' => false,
),
));
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ framework:
property_access:
magic_call: true
throw_exception_on_invalid_index: true
enable_annotations: true
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Up @@ -15,6 +15,7 @@
* Property accessor configuration annotation.
*
* @Annotation
* @Target({"PROPERTY"})
*
* @author Luis Ramón López <lrlopez@gmail.com>
*/
Expand Down
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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to o 2254 thers. Learn more.

Weird too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above, I just changed the class name and namespace...

Copy link
Contributor

Choose a reason for hiding this comment

The 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
{
}
177 changes: 177 additions & 0 deletions src/Symfony/Component/PropertyAccess/Mapping/AttributeMetadata.php
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
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll turn it immutable.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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');
}
}
Loading
0