Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] Allow attribute autoconfiguration on static methods #47066

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

Merged
merged 1 commit into from
Jan 26, 2023
Merged
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 @@ -120,7 +120,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed

if ($this->methodAttributeConfigurators || $this->parameterAttributeConfigurators) {
foreach ($classReflector->getMethods(\ReflectionMethod::IS_PUBLIC) as $methodReflector) {
if ($methodReflector->isStatic() || $methodReflector->isConstructor() || $methodReflector->isDestructor()) {
if ($methodReflector->isConstructor() || $methodReflector->isDestructor()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithoutIndex;
use Symfony\Component\DependencyInjection\Tests\Fixtures\StaticMethodTag;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedConsumerWithExclude;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService1;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService2;
Expand Down Expand Up @@ -1025,6 +1026,28 @@ static function (ChildDefinition $definition) {
self::assertTrue($service->hasBeenConfigured);
}

public function testAttributeAutoconfigurationOnStaticMethod()
{
$container = new ContainerBuilder();
$container->registerAttributeForAutoconfiguration(
CustomMethodAttribute::class,
static function (ChildDefinition $d, CustomMethodAttribute $a, \ReflectionMethod $_r) {
$d->addTag('custom_tag', ['attribute' => $a->someAttribute]);
}
);

$container->register('service', StaticMethodTag::class)
->setPublic(true)
->setAutoconfigured(true);

$container->compile();

$definition = $container->getDefinition('service');
self::assertEquals([['attribute' => 'static']], $definition->getTag('custom_tag'));

$container->get('service');
}

public function testTaggedIteratorAndLocatorWithExclude()
{
$container = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\Tests\Fixtures\Attribute\CustomMethodAttribute;

final class StaticMethodTag
{
#[CustomMethodAttribute('static')]
public static function method(): void
{
}
}