10000 [FrameworkBundle] Integrate custom property accessors into the framework by lrlopez · Pull Request #22191 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Integrate custom property accessors into the framework #22191

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 4 commits 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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ CHANGELOG
name as value, using it makes the command lazy
* Added `cache:pool:prune` command to allow manual stale cache item pruning of supported PSR-6 and PSR-16 cache pool
implementations
* Added custom property accessors support
* Deprecated `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`, use
`Symfony\Component\Translation\Reader\TranslationReader` instead
* Deprecated `translation.loader` service, use `translation.reader` instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,16 @@ private function addPropertyAccessSection(ArrayNodeDefinition $rootNode)
->children()
->booleanNode('magic_call')->defaultFalse()->end()
->booleanNode('throw_exception_on_invalid_index')->defaultFalse()->end()
->end()
->booleanNode('enable_annotations')->defaultFalse()->end()
->arrayNode('mapping')
->addDefaultsIfNotSet()
->fixXmlConfig('path')
->children()
->arrayNode('paths')
->prototype('scalar')->end()
->end()
->end()
->end()
->end()
->end()
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1271,26 +1271,31 @@ private function registerValidatorMapping(ContainerBuilder $container, array $co
$fileRecorder('xml', dirname($reflClass->getFileName()).'/Resources/config/validation.xml');
}

$this->registerComponentMapping($container, $fileRecorder, 'validation');

$this->registerMappingFilesFromConfig($container, $config, $fileRecorder);
}

private function registerComponentMapping(ContainerBuilder $container, $fileRecorder, $component)
{
foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
$dirname = $bundle['path'];

if (
$container->fileExists($file = $dirname.'/Resources/config/validation.yaml', false) ||
$container->fileExists($file = $dirname.'/Resources/config/validation.yml', false)
$container->fileExists($file = $dirname.'/Resources/config/'.$component.'.yaml', false) ||
$container->fileExists($file = $dirname.'/Resources/config/'.$component.'.yml', false)
) {
$fileRecorder('yml', $file);
}

if ($container->fileExists($file = $dirname.'/Resources/config/validation.xml', false)) {
if ($container->fileExists($file = $dirname.'/Resources/config/'.$component.'.xml', false)) {
$fileRecorder('xml', $file);
}

if ($container->fileExists($dir = $dirname.'/Resources/config/validation', '/^$/')) {
if ($container->fileExists($dir = $dirname.'/Resources/config/'.$component, '/^$/')) {
$this->registerMappingFilesFromDir($dir, $fileRecorder);
}
}

$this->registerMappingFilesFromConfig($container, $config, $fileRecorder);
}

private function registerMappingFilesFromDir($dir, callable $fileRecorder)
Expand Down Expand Up @@ -1386,10 +1391,36 @@ private function registerPropertyAccessConfiguration(array $config, ContainerBui

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

$container->getDefinition('property_accessor')->setPrivate(true);
$loaders = array();
$fileRecorder = function ($extension, $path) use (&$loaders) {
$definition = new Definition(in_array($extension, array('yaml', 'yml')) ? 'Symfony\Component\PropertyAccess\Mapping\Loader\YamlFileLoader' : 'Symfony\Component\PropertyAccess\Mapping\Loader\XmlFileLoader', array($path));
$definition->setPublic(false);
$loaders[] = $definition;
};

if (isset($config['enable_annotations']) && $config['enable_annotations']) {
if (!$this->annotationsConfigEnabled) {
throw new \LogicException('"enable_annotations" on property access cannot be set as Annotations support is disabled.');
}
$annotationLoader = new Definition(
'Symfony\Component\PropertyAccess\Mapping\Loader\AnnotationLoader',
array(new Reference('annotation_reader'))
);
$annotationLoader->setPublic(false);
$loaders[] = $annotationLoader;
}

$this->registerComponentMapping($container, $fileRecorder, 'property_accessor');

$this->registerMappingFilesFromConfig($container, $config, $fileRecorder);

$chainLoader = $container->getDefinition('property_access.mapping.chain_loader');

$chainLoader->replaceArgument(0, $loaders);

$container
->getDefinition('property_accessor')
->setPrivate(true)
->replaceArgument(0, $config['magic_call'])
->replaceArgument(1, $config['throw_exception_on_invalid_index'])
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@
<services>
<defaults public="false" />

<!-- Loader -->
<service id="property_access.mapping.chain_loader" class="Symfony\Component\PropertyAccess\Mapping\Loader\LoaderChain">
<argument type="collection" />
</service>

<!-- Class Metadata Factory -->
<service id="property_access.mapping.class_metadata_factory" class="Symfony\Component\PropertyAccess\Mapping\Factory\LazyLoadingMetadataFactory">
<argument type="service" id="property_access.mapping.chain_loader" />
</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="property_access.mapping.class_metadata_factory" on-invalid="ignore" />
</service>
<service id="Symfony\Component\PropertyAccess\PropertyAccessorInterface" alias="property_accessor" />
</services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ protected static function getBundleDefaultConfig()
'property_access' => array(
'magic_call' => false,
'throw_exception_on_invalid_index' => false,
'enable_annotations' => false,
'mapping' => array(
'paths' => array(),
),
),
'property_info' => array(
'enabled' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
'debug' => true,
'file_cache_dir' => '%kernel.cache_dir%/annotations',
),
'property_access' => array(
'magic_call' => false,
'throw_exception_on_invalid_index' => false,
'enable_annotations' => true,
),
'serializer' => array(
'enabled' => true,
'enable_annotations' => true,
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' => true,
),
));
8 changes: 8 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
"homepage": "https://symfony.com/contributors"
}
],
"repositories": [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These changes are only temporary until the PR is accepted or until PR #22190 is merged...

{
"type": "git",
"url": "https://github.com/lrlopez/property-access"
}
],
"require": {
"php": "^5.5.9|>=7.0.8",
"ext-xml": "*",
Expand All @@ -35,6 +41,7 @@
"fig/link-util": "^1.0",
"symfony/asset": "~3.3|~4.0",
"symfony/browser-kit": "~2.8|~3.0|~4.0",
"symfony/cache": "~3.4|~4.0",
"symfony/console": "~3.4|~4.0",
"symfony/css-selector": "~2.8|~3.0|~4.0",
"symfony/dom-crawler": "~2.8|~3.0|~4.0",
Expand All @@ -43,6 +50,7 @@
"symfony/form": "~3.4|~4.0",
"symfony/expression-language": "~2.8|~3.0|~4.0",
"symfony/process": "~2.8|~3.0|~4.0",
"symfony/property-access": "dev-master",
"symfony/security-core": "~3.2|~4.0",
"symfony/security-csrf": "~2.8|~3.0|~4.0",
"symfony/serializer": "~3.3|~4.0",
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/PropertyAccess/Annotation/AdderAccessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\Annotation;

/**
* Property accessor adder configuration annotation.
*
* @Annotation
* @Target({"METHOD"})
*
* @author Luis Ramón López <lrlopez@gmail.com>
*/
class AdderAccessor
{
/**
* Associates this method to the adder of this property.
*
* @var string
*/
public $property;
}
30 changes: 30 additions & 0 deletions src/Symfony/Component/PropertyAccess/Annotation/GetterAccessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\Annotation;

/**
* Property accessor getter configuration annotation.
*
* @Annotation
* @Target({"METHOD"})
*
* @author Luis Ramón López <lrlopez@gmail.com>
*/
class GetterAccessor
{
/**
* Associates this method to the getter of this property.
*
* @var string
*/
public $property;
}
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\Component\PropertyAccess\Annotation;

/**
* Property accessor configuration annotation.
*
* @Annotation
* @Target({"PROPERTY"})
*
* @author Luis Ramón López <lrlopez@gmail.com>
*/
class PropertyAccessor
{
/**
* Custom setter method for the property.
*
* @var string
*/
public $setter;

/**
* Custom getter method for the property.
*
* @var string
*/
public $getter;

/**
* Custom adder method for the property.
*
* @var string
*/
public $adder;

/**
* Custom remover method for the property.
*
* @var string
*/
public $remover;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\Annotation;

/**
* Property accessor remover configuration annotation.
*
* @Annotation
* @Target({"METHOD"})
*
* @author Luis Ramón López <lrlopez@gmail.com>
*/
class RemoverAccessor
{
/**
* Associates this method to the remover of this property.
*
* @var string
*/
public $property;
}
30 changes: 30 additions & 0 deletions src/Symfony/Component/PropertyAccess/Annotation/SetterAccessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\Annotation;

/**
* Property accessor setter configuration annotation.
*
* @Annotation
* @Target({"METHOD"})
*
* @author Luis Ramón López <lrlopez@gmail.com>
*/
class SetterAccessor
{
/**
* Associates this method to the setter of this property.
*
* @var string
*/
public $property;
}
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 Luis Ramón López <lrlopez@gmail.com>
*/
class MappingException extends RuntimeException
{
}
Loading
0