Skip to content

Commit b80b1e3

Browse files
committed
minor #17223 Replace remaining annotations with attributes, remove remaining annotations configuration block (ker0x)
This PR was squashed before being merged into the 6.0 branch. Discussion ---------- Replace remaining annotations with attributes, remove remaining annotations configuration block This PR replaces the last remaining annotations in the documentation with the attributes. I think this is the last PR to fix #16877, unless I missed some other annotations. Commits ------- 6c15670 Replace remaining annotations with attributes, remove remaining annotations configuration block
2 parents 0725a38 + 6c15670 commit b80b1e3

File tree

11 files changed

+25
-223
lines changed

11 files changed

+25
-223
lines changed

best_practices.rst

+5-6
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,11 @@ configuration. You don't need to browse several files created with different
236236
formats (YAML, XML, PHP): all the configuration is just where you need it and
237237
it only uses one format.
238238

239-
Don't Use Annotations to Configure the Controller Template
240-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
239+
Don't Use Attributes to Configure the Controller Template
240+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
241241

242-
The ``@Template`` annotation is useful, but also involves some *magic*.
243-
Moreover, most of the time ``@Template`` is used without any parameters, which
242+
The ``#[Template]`` attribute is useful, but also involves some *magic*.
243+
Moreover, most of the time ``#[Template]`` is used without any parameters, which
244244
makes it more difficult to know which template is being rendered. It also hides
245245
the fact that a controller should always return a ``Response`` object.
246246

@@ -380,8 +380,7 @@ Use Voters to Implement Fine-grained Security Restrictions
380380

381381
If your security logic is complex, you should create custom
382382
:doc:`security voters </security/voters>` instead of defining long expressions
383-
inside the ``#[Security]`` attribute (or in the ``@Security`` annotation if your
384-
PHP version doesn't support attributes yet).
383+
inside the ``#[Security]`` attribute.
385384

386385
Web Assets
387386
----------

components/http_kernel.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ on the event object that's passed to listeners on this event.
302302
the profiler is enabled.
303303

304304
One interesting listener comes from the `SensioFrameworkExtraBundle`_. This
305-
listener's `@ParamConverter`_ functionality allows you to pass a full object
305+
listener's `#[ParamConverter]`_ functionality allows you to pass a full object
306306
(e.g. a ``Post`` object) to your controller instead of a scalar value (e.g.
307307
an ``id`` parameter that was on your route). The listener -
308308
``ParamConverterListener`` - uses reflection to look at each of the
@@ -411,8 +411,8 @@ return a ``Response``.
411411

412412
There is no default listener inside the Symfony Framework for the ``kernel.view``
413413
event. However, `SensioFrameworkExtraBundle`_ *does* add a listener to this
414-
event. If your controller returns an array, and you place the `@Template`_
415-
annotation above the controller, then this listener renders a template,
414+
event. If your controller returns an array, and you place the `#[Template]`_
415+
attribute above the controller, then this listener renders a template,
416416
passes the array you returned from your controller to that template, and
417417
creates a ``Response`` containing the returned content from that template.
418418

@@ -750,6 +750,6 @@ Learn more
750750
.. _FOSRestBundle: https://github.com/friendsofsymfony/FOSRestBundle
751751
.. _`PHP FPM`: https://www.php.net/manual/en/install.fpm.php
752752
.. _`SensioFrameworkExtraBundle`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html
753-
.. _`@ParamConverter`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
754-
.. _`@Template`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/view.html
753+
.. _`#[ParamConverter]`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
754+
.. _`#[Template]`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/view.html
755755
.. _variadic: https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list

components/serializer.rst

+1-102
Original file line numberDiff line numberDiff line change
@@ -293,35 +293,6 @@ Then, create your groups definition:
293293

294294
.. configuration-block::
295295

296-
.. code-block:: php-annotations
297-
298-
namespace Acme;
299-
300-
use Symfony\Component\Serializer\Annotation\Groups;
301-
302-
class MyObj
303-
{
304-
/**
305-
* @Groups({"group1", "group2"})
306-
*/
307-
public $foo;
308-
309-
/**
310-
* @Groups({"group4"})
311-
*/
312-
public $anotherProperty;
313-
314-
/**
315-
* @Groups("group3")
316-
*/
317-
public function getBar() // is* methods are also supported
318-
{
319-
return $this->bar;
320-
}
321-
322-
// ...
323-
}
324-
325296
.. code-block:: php-attributes
326297
327298
namespace Acme;
@@ -467,22 +438,6 @@ Option 1: Using ``@Ignore`` Annotation
467438

468439
.. configuration-block::
469440

470-
.. code-block:: php-annotations
471-
472-
namespace App\Model;
473-
474-
use Symfony\Component\Serializer\Annotation\Ignore;
475-
476-
class MyClass
477-
{
478-
public $foo;
479-
480-
/**
481-
* @Ignore()
482-
*/
483-
public $bar;
484-
}
485-
486441
.. code-block:: php-attributes
487442
488443
namespace App\Model;
@@ -697,27 +652,6 @@ defines a ``Person`` entity with a ``firstName`` property:
697652

698653
.. configuration-block::
699654

700-
.. code-block:: php-annotations
701-
702-
namespace App\Entity;
703-
704-
use Symfony\Component\Serializer\Annotation\SerializedName;
705-
706-
class Person
707-
{
708-
/**
709-
* @SerializedName("customer_name")
710-
*/
711-
private $firstName;
712-
713-
public function __construct($firstName)
714-
{
715-
$this->firstName = $firstName;
716-
}
717-
718-
// ...
719-
}
720-
721655
.. code-block:: php-attributes
722656
723657
namespace App\Entity;
@@ -1412,22 +1346,6 @@ Here, we set it to 2 for the ``$child`` property:
14121346

14131347
.. configuration-block::
14141348

1415-
.. code-block:: php-annotations
1416-
1417-
namespace Acme;
1418-
1419-
use Symfony\Component\Serializer\Annotation\MaxDepth;
1420-
1421-
class MyObj
1422-
{
1423-
/**
1424-
* @MaxDepth(2)
1425-
*/
1426-
public $child;
1427-
1428-
// ...
1429-
}
1430-
14311349
.. code-block:: php-attributes
14321350
14331351
namespace Acme;
@@ -1501,9 +1419,7 @@ having unique identifiers::
15011419
{
15021420
public $id;
15031421

1504-
/**
1505-
* @MaxDepth(1)
1506-
*/
1422+
#[MaxDepth(1)]
15071423
public $child;
15081424
}
15091425

@@ -1735,23 +1651,6 @@ and ``BitBucketCodeRepository`` classes:
17351651

17361652
.. configuration-block::
17371653

1738-
.. code-block:: php-annotations
1739-
1740-
namespace App;
1741-
1742-
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
1743-
1744-
/**
1745-
* @DiscriminatorMap(typeProperty="type", mapping={
1746-
* "github"="App\GitHubCodeRepository",
1747-
* "bitbucket"="App\BitBucketCodeRepository"
1748-
* })
1749-
*/
1750-
abstract class CodeRepository
1751-
{
1752-
// ...
1753-
}
1754-
17551654
.. code-block:: php-attributes
17561655
17571656
namespace App;

contributing/documentation/standards.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ Configuration examples should show all supported formats using
8888
(and their orders) are:
8989

9090
* **Configuration** (including services): YAML, XML, PHP
91-
* **Routing**: Annotations, YAML, XML, PHP
92-
* **Validation**: Annotations, YAML, XML, PHP
93-
* **Doctrine Mapping**: Annotations, YAML, XML, PHP
91+
* **Routing**: Attributes, YAML, XML, PHP
92+
* **Validation**: Attributes, YAML, XML, PHP
93+
* **Doctrine Mapping**: Attributes, YAML, XML, PHP
9494
* **Translation**: XML, YAML, PHP
9595

9696
Example

controller/argument_value_resolver.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ with the ``User`` class::
8181
}
8282
}
8383

84-
Beware that this feature is already provided by the `@ParamConverter`_
85-
annotation from the SensioFrameworkExtraBundle. If you have that bundle
84+
Beware that this feature is already provided by the `#[ParamConverter]`_
85+
attribute from the SensioFrameworkExtraBundle. If you have that bundle
8686
installed in your project, add this config to disable the auto-conversion of
8787
type-hinted method arguments:
8888

@@ -253,7 +253,7 @@ To ensure your resolvers are added in the right position you can run the followi
253253
command to see which argument resolvers are present and in which order they run.
254254

255255
.. code-block:: terminal
256-
256+
257257
$ php bin/console debug:container debug.argument_resolver.inner --show-arguments
258258
259259
.. tip::
@@ -267,5 +267,5 @@ command to see which argument resolvers are present and in which order they run.
267267
$user = null``). The ``DefaultValueResolver`` is executed as the last
268268
resolver and will use the default value if no value was already resolved.
269269

270-
.. _`@ParamConverter`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
270+
.. _`#[ParamConverter]`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
271271
.. _`yield`: https://www.php.net/manual/en/language.generators.syntax.php

reference/configuration/doctrine.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ to organize the application code.
310310
Custom Mapping Entities in a Bundle
311311
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
312312

313-
Doctrine's ``auto_mapping`` feature loads annotation configuration from
313+
Doctrine's ``auto_mapping`` feature loads attribute configuration from
314314
the ``Entity/`` directory of each bundle *and* looks for other formats (e.g.
315315
YAML, XML) in the ``Resources/config/doctrine`` directory.
316316

routing/routing_from_database.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ For these cases, the ``DynamicRouter`` offers an alternative approach:
2323
When all routes are known during deploy time and the number is not too
2424
high, using a :doc:`custom route loader <custom_route_loader>` is the
2525
preferred way to add more routes. When working with only one type of
26-
objects, a slug parameter on the object and the ``@ParamConverter``
27-
annotation works fine (see `FrameworkExtraBundle`_) .
26+
objects, a slug parameter on the object and the ``#[ParamConverter]``
27+
attribute works fine (see `FrameworkExtraBundle`_) .
2828

2929
The ``DynamicRouter`` is useful when you need ``Route`` objects with
3030
the full feature set of Symfony. Each route can define a specific

security.rst

-25
Original file line numberDiff line numberDiff line change
@@ -2115,31 +2115,6 @@ using annotations:
21152115

21162116
.. configuration-block::
21172117

2118-
.. code-block:: php-annotations
2119-
2120-
// src/Controller/AdminController.php
2121-
// ...
2122-
2123-
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
2124-
2125-
/**
2126-
* Require ROLE_ADMIN for all the actions of this controller
2127-
*
2128-
* @IsGranted("ROLE_ADMIN")
2129-
*/
2130-
class AdminController extends AbstractController
2131-
{
2132-
/**
2133-
* Require ROLE_SUPER_ADMIN only for this action
2134-
*
2135-
* @IsGranted("ROLE_SUPER_ADMIN")
2136-
*/
2137-
public function adminDashboard(): Response
2138-
{
2139-
// ...
2140-
}
2141-
}
2142-
21432118
.. code-block:: php-attributes
21442119
21452120
// src/Controller/AdminController.php

service_container/autowiring.rst

-40
Original file line numberDiff line numberDiff line change
@@ -551,30 +551,6 @@ to inject the ``logger`` service, and decide to use setter-injection:
551551

552552
.. configuration-block::
553553

554-
.. code-block:: php-annotations
555-
556-
// src/Util/Rot13Transformer.php
557-
namespace App\Util;
558-
559-
class Rot13Transformer
560-
{
561-
private $logger;
562-
563-
/**
564-
* @required
565-
*/
566-
public function setLogger(LoggerInterface $logger): void
567-
{
568-
$this->logger = $logger;
569-
}
570-
571-
public function transform($value): string
572-
{
573-
$this->logger->info('Transforming '.$value);
574-
// ...
575-
}
576-
}
577-
578554
.. code-block:: php-attributes
579555
580556
// src/Util/Rot13Transformer.php
@@ -612,22 +588,6 @@ typed properties:
612588

613589
.. configuration-block::
614590

615-
.. code-block:: php-annotations
616-
617-
namespace App\Util;
618-
619-
class Rot13Transformer
620-
{
621-
/** @required */
622-
public LoggerInterface $logger;
623-
624-
public function transform($value)
625-
{
626-
$this->logger->info('Transforming '.$value);
627-
// ...
628-
}
629-
}
630-
631591
.. code-block:: php-attributes
632592
633593
namespace App\Util;

service_container/calls.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Service Method Calls and Setter Injection
66

77
.. tip::
88

9-
If you're using autowiring, you can use ``#[Required]`` or ``@required`` to
9+
If you're using autowiring, you can use ``#[Required]`` to
1010
:ref:`automatically configure method calls <autowiring-calls>`.
1111

1212
Usually, you'll want to inject your dependencies via the constructor. But sometimes,
@@ -145,13 +145,13 @@ The configuration to tell the container it should do so would be like:
145145
146146
.. tip::
147147

148-
If autowire is enabled, you can also use annotations; with the previous
148+
If autowire is enabled, you can also use attributes; with the previous
149149
example it would be::
150150

151151
/**
152-
* @required
153152
* @return static
154153
*/
154+
#[Required]
155155
public function withLogger(LoggerInterface $logger)
156156
{
157157
$new = clone $this;
@@ -162,6 +162,6 @@ The configuration to tell the container it should do so would be like:
162162

163163
You can also leverage the PHP 8 ``static`` return type instead of the
164164
``@return static`` annotation. If you don't want a method with a
165-
PHP 8 ``static`` return type and a ``@required`` annotation to behave as
165+
PHP 8 ``static`` return type and a ``#[Required]`` attribute to behave as
166166
a wither, you can add a ``@return $this`` annotation to disable the
167167
*returns clone* feature.

0 commit comments

Comments
 (0)