Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Replace remaining annotations with attributes, remove remaining annotations configuration block #17223

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
Sep 14, 2022
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
11 changes: 5 additions & 6 deletions 11 best_practices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,11 @@ configuration. You don't need to browse several files created with different
formats (YAML, XML, PHP): all the configuration is just where you need it and
it only uses one format.

Don't Use Annotations to Configure the Controller Template
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Don't Use Attributes to Configure the Controller Template
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The ``@Template`` annotation is useful, but also involves some *magic*.
Moreover, most of the time ``@Template`` is used without any parameters, which
The ``#[Template]`` attribute is useful, but also involves some *magic*.
Moreover, most of the time ``#[Template]`` is used without any parameters, which
xabbuh marked this conversation as resolved.
Show resolved Hide resolved
makes it more difficult to know which template is being rendered. It also hides
the fact that a controller should always return a ``Response`` object.

Expand Down Expand Up @@ -380,8 +380,7 @@ Use Voters to Implement Fine-grained Security Restrictions

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

Web Assets
----------
Expand Down
10 changes: 5 additions & 5 deletions 10 components/http_kernel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ on the event object that's passed to listeners on this event.
the profiler is enabled.

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

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

Expand Down Expand Up @@ -750,6 +750,6 @@ Learn more
.. _FOSRestBundle: https://github.com/friendsofsymfony/FOSRestBundle
.. _`PHP FPM`: https://www.php.net/manual/en/install.fpm.php
.. _`SensioFrameworkExtraBundle`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html
.. _`@ParamConverter`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
.. _`@Template`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/view.html
.. _`#[ParamConverter]`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
.. _`#[Template]`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/view.html
.. _variadic: https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list
103 changes: 1 addition & 102 deletions 103 components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -293,35 +293,6 @@ Then, create your groups definition:

.. configuration-block::

.. code-block:: php-annotations

namespace Acme;

use Symfony\Component\Serializer\Annotation\Groups;

class MyObj
{
/**
* @Groups({"group1", "group2"})
*/
public $foo;

/**
* @Groups({"group4"})
*/
public $anotherProperty;

/**
* @Groups("group3")
*/
public function getBar() // is* methods are also supported
{
return $this->bar;
}

// ...
}

.. code-block:: php-attributes

namespace Acme;
Expand Down Expand Up @@ -467,22 +438,6 @@ Option 1: Using ``@Ignore`` Annotation

.. configuration-block::

.. code-block:: php-annotations

namespace App\Model;

use Symfony\Component\Serializer\Annotation\Ignore;

class MyClass
{
public $foo;

/**
* @Ignore()
*/
public $bar;
}

.. code-block:: php-attributes

namespace App\Model;
Expand Down Expand Up @@ -697,27 +652,6 @@ defines a ``Person`` entity with a ``firstName`` property:

.. configuration-block::

.. code-block:: php-annotations

namespace App\Entity;

use Symfony\Component\Serializer\Annotation\SerializedName;

class Person
{
/**
* @SerializedName("customer_name")
*/
private $firstName;

public function __construct($firstName)
{
$this->firstName = $firstName;
}

// ...
}

.. code-block:: php-attributes

namespace App\Entity;
Expand Down Expand Up @@ -1412,22 +1346,6 @@ Here, we set it to 2 for the ``$child`` property:

.. configuration-block::

.. code-block:: php-annotations

namespace Acme;

use Symfony\Component\Serializer\Annotation\MaxDepth;

class MyObj
{
/**
* @MaxDepth(2)
*/
public $child;

// ...
}

.. code-block:: php-attributes

namespace Acme;
Expand Down Expand Up @@ -1501,9 +1419,7 @@ having unique identifiers::
{
public $id;

/**
* @MaxDepth(1)
*/
#[MaxDepth(1)]
public $child;
}

Expand Down Expand Up @@ -1735,23 +1651,6 @@ and ``BitBucketCodeRepository`` classes:

.. configuration-block::

.. code-block:: php-annotations

namespace App;

use Symfony\Component\Serializer\Annotation\DiscriminatorMap;

/**
* @DiscriminatorMap(typeProperty="type", mapping={
* "github"="App\GitHubCodeRepository",
* "bitbucket"="App\BitBucketCodeRepository"
* })
*/
abstract class CodeRepository
{
// ...
}

.. code-block:: php-attributes

namespace App;
Expand Down
6 changes: 3 additions & 3 deletions 6 contributing/documentation/standards.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ Configuration examples should show all supported formats using
(and their orders) are:

* **Configuration** (including services): YAML, XML, PHP
* **Routing**: Annotations, YAML, XML, PHP
* **Validation**: Annotations, YAML, XML, PHP
* **Doctrine Mapping**: Annotations, YAML, XML, PHP
* **Routing**: Attributes, YAML, XML, PHP
* **Validation**: Attributes, YAML, XML, PHP
* **Doctrine Mapping**: Attributes, YAML, XML, PHP
* **Translation**: XML, YAML, PHP

Example
Expand Down
8 changes: 4 additions & 4 deletions 8 controller/argument_value_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ with the ``User`` class::
}
}

Beware that this feature is already provided by the `@ParamConverter`_
annotation from the SensioFrameworkExtraBundle. If you have that bundle
Beware that this feature is already provided by the `#[ParamConverter]`_
Copy link
Member

Choose a reason for hiding this comment

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

Should we refer to the MapEntity (see symfony/symfony#43854) here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I would be in favor of changing the whole Custom Value Resolver example because there is already a native UserValueResolver. But I don't know what could be used instead!

Copy link
Member

Choose a reason for hiding this comment

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

Let's keep it as is then for now and rethink the whole example in a different PR.

attribute from the SensioFrameworkExtraBundle. If you have that bundle
installed in your project, add this config to disable the auto-conversion of
type-hinted method arguments:

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

.. code-block:: terminal

$ php bin/console debug:container debug.argument_resolver.inner --show-arguments

.. tip::
Expand All @@ -267,5 +267,5 @@ command to see which argument resolvers are present and in which order they run.
$user = null``). The ``DefaultValueResolver`` is executed as the last
resolver and will use the default value if no value was already resolved.

.. _`@ParamConverter`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
.. _`#[ParamConverter]`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
.. _`yield`: https://www.php.net/manual/en/language.generators.syntax.php
2 changes: 1 addition & 1 deletion 2 reference/configuration/doctrine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ to organize the application code.
Custom Mapping Entities in a Bundle
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

Expand Down
4 changes: 2 additions & 2 deletions 4 routing/routing_from_database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ For these cases, the ``DynamicRouter`` offers an alternative approach:
When all routes are known during deploy time and the number is not too
high, using a :doc:`custom route loader <custom_route_loader>` is the
preferred way to add more routes. When working with only one type of
objects, a slug parameter on the object and the ``@ParamConverter``
annotation works fine (see `FrameworkExtraBundle`_) .
objects, a slug parameter on the object and the ``#[ParamConverter]``
attribute works fine (see `FrameworkExtraBundle`_) .

The ``DynamicRouter`` is useful when you need ``Route`` objects with
the full feature set of Symfony. Each route can define a specific
Expand Down
25 changes: 0 additions & 25 deletions 25 security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2131,31 +2131,6 @@ using annotations:

.. configuration-block::

.. code-block:: php-annotations

// src/Controller/AdminController.php
// ...

use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;

/**
* Require ROLE_ADMIN for all the actions of this controller
*
* @IsGranted("ROLE_ADMIN")
*/
class AdminController extends AbstractController
{
/**
* Require ROLE_SUPER_ADMIN only for this action
*
* @IsGranted("ROLE_SUPER_ADMIN")
*/
public function adminDashboard(): Response
{
// ...
}
}

.. code-block:: php-attributes

// src/Controller/AdminController.php
Expand Down
40 changes: 0 additions & 40 deletions 40 service_container/autowiring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -551,30 +551,6 @@ to inject the ``logger`` service, and decide to use setter-injection:

.. configuration-block::

.. code-block:: php-annotations

// src/Util/Rot13Transformer.php
namespace App\Util;

class Rot13Transformer
{
private $logger;

/**
* @required
*/
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}

public function transform($value): string
{
$this->logger->info('Transforming '.$value);
// ...
}
}

.. code-block:: php-attributes

// src/Util/Rot13Transformer.php
Expand Down Expand Up @@ -612,22 +588,6 @@ typed properties:

.. configuration-block::

.. code-block:: php-annotations

namespace App\Util;

class Rot13Transformer
{
/** @required */
public LoggerInterface $logger;

public function transform($value)
{
$this->logger->info('Transforming '.$value);
// ...
}
}

.. code-block:: php-attributes

namespace App\Util;
Expand Down
8 changes: 4 additions & 4 deletions 8 service_container/calls.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Service Method Calls and Setter Injection

.. tip::

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

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

.. tip::

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

/**
* @required
* @return static
*/
#[Required]
public function withLogger(LoggerInterface $logger)
{
$new = clone $this;
Expand All @@ -162,6 +162,6 @@ The configuration to tell the container it should do so would be like:

You can also leverage the PHP 8 ``static`` return type instead of the
``@return static`` annotation. If you don't want a method with a
PHP 8 ``static`` return type and a ``@required`` annotation to behave as
PHP 8 ``static`` return type and a ``#[Required]`` attribute to behave as
a wither, you can add a ``@return $this`` annotation to disable the
*returns clone* feature.
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.