Skip to content

Commit 45b53fe

Browse files
feature #40556 Add #[As-prefix] to service attributes (nicolas-grekas)
This PR was merged into the 5.3-dev branch. Discussion ---------- Add `#[As-prefix]` to service attributes | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This PR renames all existing attributes with the `As` prefix, as I proposed several times already. This should help autocompletion, and it's required to not collide with existing class names (eg the `Command` class, but also the old `Controller` class, etc.) I think this `As` prefix is a convention for the better. Commits ------- 4f13189 Add `#[As-prefix]` to service attributes
2 parents ea58a15 + 4f13189 commit 45b53fe

File tree

14 files changed

+31
-28
lines changed

14 files changed

+31
-28
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
use Symfony\Component\DependencyInjection\Parameter;
6060
use Symfony\Component\DependencyInjection\Reference;
6161
use Symfony\Component\DependencyInjection\ServiceLocator;
62-
use Symfony\Component\EventDispatcher\Attribute\EventListener;
62+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
6363
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6464
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
6565
use Symfony\Component\Finder\Finder;
@@ -556,7 +556,7 @@ public function load(array $configs, ContainerBuilder $container)
556556
$container->registerForAutoconfiguration(LoggerAwareInterface::class)
557557
->addMethodCall('setLogger', [new Reference('logger')]);
558558

559-
$container->registerAttributeForAutoconfiguration(EventListener::class, static function (ChildDefinition $definition, EventListener $attribute): void {
559+
$container->registerAttributeForAutoconfiguration(AsEventListener::class, static function (ChildDefinition $definition, AsEventListener $attribute): void {
560560
$definition->addTag('kernel.event_listener', get_object_vars($attribute));
561561
});
562562
$container->registerAttributeForAutoconfiguration(AsController::class, static function (ChildDefinition $definition, AsController $attribute): void {

src/Symfony/Component/Console/Attribute/ConsoleCommand.php renamed to src/Symfony/Component/Console/Attribute/AsCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
namespace Symfony\Component\Console\Attribute;
1313

14+
/**
15+
* Service tag to autoconfigure commands.
16+
*/
1417
#[\Attribute(\Attribute::TARGET_CLASS)]
15-
class ConsoleCommand
18+
class AsCommand
1619
{
1720
public function __construct(
1821
public string $name,

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ CHANGELOG
1010
on the `console.command` tag to allow the `list` command to instantiate commands lazily
1111
* Add option `--short` to the `list` command
1212
* Add support for bright colors
13-
* Add `ConsoleCommand` attribute for declaring commands on PHP 8
13+
* Add `#[AsCommand]` attribute for declaring commands on PHP 8
1414

1515
5.2.0
1616
-----

src/Symfony/Component/Console/Command/Command.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Console\Command;
1313

1414
use Symfony\Component\Console\Application;
15-
use Symfony\Component\Console\Attribute\ConsoleCommand;
15+
use Symfony\Component\Console\Attribute\AsCommand;
1616
use Symfony\Component\Console\Exception\ExceptionInterface;
1717
use Symfony\Component\Console\Exception\InvalidArgumentException;
1818
use Symfony\Component\Console\Exception\LogicException;
@@ -67,7 +67,7 @@ public static function getDefaultName()
6767
{
6868
$class = static::class;
6969

70-
if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(ConsoleCommand::class)) {
70+
if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(AsCommand::class)) {
7171
return $attribute[0]->newInstance()->name;
7272
}
7373

@@ -83,7 +83,7 @@ public static function getDefaultDescription(): ?string
8383
{
8484
$class = static::class;
8585

86-
if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(ConsoleCommand::class)) {
86+
if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(AsCommand::class)) {
8787
return $attribute[0]->newInstance()->description;
8888
}
8989

src/Symfony/Component/Console/Tests/Command/CommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Application;
16-
use Symfony\Component\Console\Attribute\ConsoleCommand;
16+
use Symfony\Component\Console\Attribute\AsCommand;
1717
use Symfony\Component\Console\Command\Command;
1818
use Symfony\Component\Console\Exception\InvalidOptionException;
1919
use Symfony\Component\Console\Helper\FormatterHelper;
@@ -409,7 +409,7 @@ public function testSetCodeWithStaticAnonymousFunction()
409409
/**
410410
* @requires PHP 8
411411
*/
412-
public function testConsoleCommandAttribute()
412+
public function testCommandAttribute()
413413
{
414414
$this->assertSame('|foo|f', Php8Command::getDefaultName());
415415
$this->assertSame('desc', Php8Command::getDefaultDescription());
@@ -425,7 +425,7 @@ function createClosure()
425425
};
426426
}
427427

428-
#[ConsoleCommand(name: 'foo', description: 'desc', hidden: true, aliases: ['f'])]
428+
#[AsCommand(name: 'foo', description: 'desc', hidden: true, aliases: ['f'])]
429429
class Php8Command extends Command
430430
{
431431
}

src/Symfony/Component/DependencyInjection/Attribute/TaggedItem.php renamed to src/Symfony/Component/DependencyInjection/Attribute/AsTaggedItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @author Nicolas Grekas <p@tchwork.com>
1818
*/
1919
#[\Attribute(\Attribute::TARGET_CLASS)]
20-
class TaggedItem
20+
class AsTaggedItem
2121
{
2222
public function __construct(
2323
public ?string $index = null,

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CHANGELOG
77
* Add `ServicesConfigurator::remove()` in the PHP-DSL
88
* Add `%env(not:...)%` processor to negate boolean values
99
* Add support for loading autoconfiguration rules via the `#[Autoconfigure]` and `#[AutoconfigureTag]` attributes on PHP 8
10-
* Add `#[TaggedItem]` attribute for defining the index and priority of classes found in tagged iterators/locators
10+
* Add `#[AsTaggedItem]` attribute for defining the index and priority of classes found in tagged iterators/locators
1111
* Add autoconfigurable attributes
1212
* Add support for per-env configuration in loaders
1313
* Add `ContainerBuilder::willBeAvailable()` to help with conditional configuration

src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

1414
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
15-
use Symfony\Component\DependencyInjection\Attribute\TaggedItem;
15+
use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1818
use Symfony\Component\DependencyInjection\Reference;
@@ -126,7 +126,7 @@ public static function getDefault(ContainerBuilder $container, string $serviceId
126126
}
127127

128128
if ($checkTaggedItem && !$r->hasMethod($defaultMethod)) {
129-
foreach ($r->getAttributes(TaggedItem::class) as $attribute) {
129+
foreach ($r->getAttributes(AsTaggedItem::class) as $attribute) {
130130
return 'priority' === $indexAttribute ? $attribute->newInstance()->priority : $attribute->newInstance()->index;
131131
}
132132

src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
16-
use Symfony\Component\DependencyInjection\Attribute\TaggedItem;
16+
use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;
1717
use Symfony\Component\DependencyInjection\ChildDefinition;
1818
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
1919
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
@@ -235,12 +235,12 @@ public function test($tagName, ContainerBuilder $container)
235235
}
236236
}
237237

238-
#[TaggedItem(index: 'hello', priority: 1)]
238+
#[AsTaggedItem(index: 'hello', priority: 1)]
239239
class HelloNamedService extends \stdClass
240240
{
241241
}
242242

243-
#[TaggedItem(priority: 2)]
243+
#[AsTaggedItem(priority: 2)]
244244
class HelloNamedService2
245245
{
246246
}

src/Symfony/Component/EventDispatcher/Attribute/EventListener.php renamed to src/Symfony/Component/EventDispatcher/Attribute/AsEventListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @author Alexander M. Turek <me@derrabus.de>
1818
*/
1919
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
20-
class EventListener
20+
class AsEventListener
2121
{
2222
public function __construct(
2323
public ?string $event = null,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy