Skip to content

[FrameworkBundle][PropertyInfo] Allow defining accessors and mutators via an attribute #59529

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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
use Symfony\Component\PropertyInfo\Extractor\ConstructorArgumentTypeExtractorInterface;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;
use Symfony\Component\PropertyInfo\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
Expand Down Expand Up @@ -2076,6 +2077,16 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild

if ($container->getParameter('kernel.debug')) {
$container->removeDefinition('property_info.cache');
$container->removeDefinition('property_info.mapping.cached_class_metadata_factory');
}

if (!interface_exists(ClassMetadataFactoryInterface::class)) {
$container->removeDefinition('property_info.mapping.attribute_loader');
$container->removeDefinition('property_info.mapping.class_metadata_factory');
$container->removeDefinition('property_info.mapping.cached_class_metadata_factory');
} else {
$container->getDefinition('property_info.reflection_extractor')
->setArgument('$classMetadataFactory', new Reference('property_info.mapping.class_metadata_factory'));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use Symfony\Component\PropertyInfo\Extractor\ConstructorExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\Mapping\Factory\CachedClassMetadataFactory;
use Symfony\Component\PropertyInfo\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\PropertyInfo\Mapping\Loader\AttributeLoader;
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyInfoCacheExtractor;
Expand Down Expand Up @@ -40,6 +43,18 @@
->decorate('property_info')
->args([service('property_info.cache.inner'), service('cache.property_info')])

->set('property_info.mapping.attribute_loader', AttributeLoader::class)

->set('property_info.mapping.class_metadata_factory', ClassMetadataFactory::class)
->args([service('property_info.mapping.attribute_loader')])

->set('property_info.mapping.cached_class_metadata_factory', CachedClassMetadataFactory::class)
->decorate('property_info.mapping.class_metadata_factory')
->args([
service('property_info.mapping.cached_class_metadata_factory.inner'),
service('cache.property_info'),
])

// Extractor
->set('property_info.reflection_extractor', ReflectionExtractor::class)
->tag('property_info.list_extractor', ['priority' => -1000])
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/PropertyInfo/Attribute/WithAccessors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\PropertyInfo\Attribute;

use Symfony\Component\PropertyInfo\Exception\LogicException;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
final readonly class WithAccessors
{
public function __construct(
public ?string $getter = null,
public ?string $setter = null,
public ?string $adder = null,
public ?string $remover = null,
) {
if (!($this->getter || $this->setter || $this->adder || $this->remover)) {
throw new LogicException('You need to have at least one method name set.');
}
if ($this->adder xor $this->remover) {
throw new LogicException('You need to have both an adder and remover set.');
}
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/PropertyInfo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add support for `non-positive-int`, `non-negative-int` and `non-zero-int` PHPStan types to `PhpStanExtractor`
* Add `PropertyDescriptionExtractorInterface` to `PhpStanExtractor`
* Allow defining accessors and mutators via the `#[WithAccessors]` attribute

7.1
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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\PropertyInfo\Exception;

interface ExceptionInterface extends \Throwable
{
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/PropertyInfo/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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\PropertyInfo\Exception;

class LogicException extends \LogicException implements ExceptionInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\PropertyInfo\Exception;

class MappingException extends RuntimeException
{
/**
* @param list<string> $invalidMethods
*/
public function __construct(
string $message,
public readonly string $forClass,
public readonly array $invalidMethods,
?\Throwable $previous = null,
) {
parent::__construct($message, 0, $previous);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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\PropertyInfo\Exception;

class RuntimeException extends \RuntimeException implements ExceptionInterface
{
}
Loading
Loading