Skip to content

Use the short Yaml syntax for service definition #7860

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

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 1 addition & 2 deletions best_practices/security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ To enable the security voter in the application, define a new service:
class: AppBundle\Security\PostVoter
arguments: ['@security.access.decision_manager']
public: false
tags:
- { name: security.voter }
tags: [security.voter]

Now, you can use the voter with the ``@Security`` annotation:

Expand Down
3 changes: 1 addition & 2 deletions best_practices/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ name is irrelevant because you never use it in your own code):
class: AppBundle\Twig\AppExtension
arguments: ['@app.markdown']
public: false
tags:
- { name: twig.extension }
tags: [twig.extension]

.. _`Twig`: http://twig.sensiolabs.org/
.. _`Parsedown`: http://parsedown.org/
8 changes: 3 additions & 5 deletions console/commands_as_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ with ``console.command``:
services:
app.command.my_command:
class: AppBundle\Command\MyCommand
tags:
- { name: console.command }
tags: [console.command]

.. code-block:: xml

Expand Down Expand Up @@ -89,7 +88,7 @@ store the default value in some ``%command.default_name%`` parameter::
public function __construct($defaultName)
{
$this->defaultName = $defaultName;

parent::__construct();
}

Expand Down Expand Up @@ -135,8 +134,7 @@ inject the ``command.default_name`` parameter:
app.command.my_command:
class: AppBundle\Command\MyCommand
arguments: ["%command.default_name%"]
tags:
- { name: console.command }
tags: [console.command]

.. code-block:: xml

Expand Down
3 changes: 1 addition & 2 deletions form/create_custom_field_type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ the ``genders`` parameter value as the first argument to its to-be-created
class: AppBundle\Form\Type\GenderType
arguments:
- '%genders%'
tags:
- { name: form.type }
tags: [form.type]

.. code-block:: xml

Expand Down
6 changes: 2 additions & 4 deletions form/data_transformers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ Define the form type as a service in your configuration files.
app.form.type.task:
class: AppBundle\Form\Type\TaskType
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: form.type }
tags: [form.type]

.. code-block:: xml

Expand Down Expand Up @@ -434,8 +433,7 @@ it's recognized as a custom field type:
app.type.issue_selector:
class: AppBundle\Form\IssueSelectorType
arguments: ['@doctrine.orm.entity_manager']
tags:
- { name: form.type }
tags: [form.type]


.. code-block:: xml
Expand Down
3 changes: 1 addition & 2 deletions form/dynamic_form_modification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,7 @@ you need to register it as a service and tag it with :ref:`form.type <dic-tags-f
app.form.friend_message:
class: AppBundle\Form\Type\FriendMessageFormType
arguments: ['@security.token_storage']
tags:
- { name: form.type }
tags: [form.type]

.. code-block:: xml

Expand Down
5 changes: 2 additions & 3 deletions form/form_dependencies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Suppose you need to access the ``doctrine.orm.entity_manager`` service so that y
can make a query. First, add this as an argument to your form class::

// src/AppBundle/Form/TaskType.php

use Doctrine\ORM\EntityManager;
// ...

Expand All @@ -102,8 +102,7 @@ Next, register this as a service and tag it with ``form.type``:
app.form.type.task:
class: AppBundle\Form\TaskType
arguments: ['@doctrine.orm.entity_manager']
tags:
- { name: form.type }
tags: [form.type]

.. code-block:: xml

Expand Down
3 changes: 1 addition & 2 deletions form/type_guesser.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ creating a service and tagging it as ``form.type_guesser``:

app.phpdoc_type_guesser:
class: AppBundle\Form\TypeGuesser\PHPDocTypeGuesser
tags:
- { name: form.type_guesser }
tags: [form.type_guesser]

.. code-block:: xml

Expand Down
3 changes: 1 addition & 2 deletions profiler/data_collector.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ To enable a data collector, define it as a regular service and tag it as
app.request_collector:
class: AppBundle\DataCollector\RequestCollector
public: false
tags:
- { name: data_collector }
tags: [data_collector]

.. code-block:: xml

Expand Down
21 changes: 7 additions & 14 deletions reference/dic_tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ And then register it as a tagged service:
services:
app.custom_assetic_worker:
class: AppBundle\Assetic\CustomWorker
tags:
- { name: assetic.factory_worker }
tags: [assetic.factory_worker]

.. code-block:: xml

Expand Down Expand Up @@ -469,8 +468,7 @@ Then register this class and tag it with ``kernel.cache_clearer``:
services:
my_cache_clearer:
class: AppBundle\Cache\MyClearer
tags:
- { name: kernel.cache_clearer }
tags: [kernel.cache_clearer]

.. code-block:: xml

Expand Down Expand Up @@ -640,8 +638,7 @@ configuration and tag it with ``kernel.event_subscriber``:
services:
app.custom_subscriber:
class: AppBundle\EventListener\CustomSubscriber
tags:
- { name: kernel.event_subscriber }
tags: [kernel.event_subscriber]

.. code-block:: xml

Expand Down Expand Up @@ -771,8 +768,7 @@ You can add a processor globally:
services:
my_service:
class: Monolog\Processor\IntrospectionProcessor
tags:
- { name: monolog.processor }
tags: [monolog.processor]

.. code-block:: xml

Expand Down Expand Up @@ -895,8 +891,7 @@ of your configuration and tag it with ``routing.loader``:
services:
app.custom_routing_loader:
class: AppBundle\Routing\CustomLoader
tags:
- { name: routing.loader }
tags: [routing.loader]

.. code-block:: xml

Expand Down Expand Up @@ -1297,8 +1292,7 @@ configuration and tag it with ``twig.extension``:
services:
app.twig_extension:
class: AppBundle\Twig\AppExtension
tags:
- { name: twig.extension }
tags: [twig.extension]

.. code-block:: xml

Expand Down Expand Up @@ -1343,8 +1337,7 @@ also have to be added as regular services:
services:
twig.extension.intl:
class: Twig_Extensions_Extension_Intl
tags:
- { name: twig.extension }
tags: [twig.extension]

.. code-block:: xml

Expand Down
3 changes: 1 addition & 2 deletions routing/custom_route_loader.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ Now define a service for the ``ExtraLoader``:
services:
app.routing_loader:
class: AppBundle\Routing\ExtraLoader
tags:
- { name: routing.loader }
tags: [routing.loader]

.. code-block:: xml

Expand Down
3 changes: 1 addition & 2 deletions security/securing_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ the :ref:`sidebar <securing-services-annotations-sidebar>` below):
services:
newsletter_manager:
class: AppBundle\Newsletter\NewsletterManager
tags:
- { name: security.secure_service }
tags: [security.secure_service]

.. code-block:: xml

Expand Down
10 changes: 4 additions & 6 deletions security/voters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ decides this using whatever logic you want.
  the ``security.authorization_checker`` service. The main difference is that
when access is not granted, ``denyAccessUnlessGranted()`` throws an
   ``AccessDeniedException``, whereas ``isGranted()`` returns ``false``.

Creating the custom Voter
-------------------------

Expand Down Expand Up @@ -218,8 +218,7 @@ and tag it with ``security.voter``:
services:
app.post_voter:
class: AppBundle\Security\PostVoter
tags:
- { name: security.voter }
tags: [security.voter]
# small performance boost
public: false

Expand Down Expand Up @@ -307,8 +306,7 @@ service:
class: AppBundle\Security\PostVoter
arguments: ['@security.access.decision_manager']
public: false
tags:
- { name: security.voter }
tags: [security.voter]

.. code-block:: xml

Expand Down Expand Up @@ -345,7 +343,7 @@ service:
;

That's it! Calling ``decide()`` on the ``AccessDecisionManager`` is essentially
the same as calling ``isGranted()`` from a controller or other places
the same as calling ``isGranted()`` from a controller or other places
(it's just a little lower-level, which is necessary for a voter).

.. note::
Expand Down
5 changes: 2 additions & 3 deletions serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ Here is an example on how to load the
get_set_method_normalizer:
class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
public: false
tags:
- { name: serializer.normalizer }
tags: [serializer.normalizer]

.. code-block:: xml

Expand Down Expand Up @@ -178,7 +177,7 @@ to your class and choose which groups to use when serializing::
$someObject,
'json', array('groups' => array('group1'))
);

In addition to the ``@Groups`` annotation, the Serializer component also
supports Yaml or XML files. These files are automatically loaded when being
stored in one of the following locations:
Expand Down
3 changes: 1 addition & 2 deletions serializer/custom_encoders.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ to inject your custom encoder into the Serializer.
services:
app.yaml_encoder:
class: AppBundle\Serializer\YamlEncoder
tags:
- { name: serializer.encoder }
tags: [serializer.encoder]

.. code-block:: xml

Expand Down
21 changes: 7 additions & 14 deletions service_container/tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ to be used for a specific purpose. Take the following example:

# app/config/services.yml
services:
app.twig_extension:
class: AppBundle\Twig\AppExtension
AppBundle\Twig\AppExtension:
public: false
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I hesitated to use _defaults here to be able to remove this line and use the short syntax, wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

I'd say, let's not add the "defaults" until we decide what to do with that new option.

tags: [twig.extension]

Expand All @@ -31,11 +30,7 @@ to be used for a specific purpose. Take the following example:
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service
id="app.twig_extension"
class="AppBundle\Twig\AppExtension"
public="false">

<service id="AppBundle\Twig\AppExtension" public="false">
<tag name="twig.extension" />
</service>
</services>
Expand All @@ -45,16 +40,14 @@ to be used for a specific purpose. Take the following example:

// app/config/services.php
use AppBundle\Twig\AppExtension;
use Symfony\Component\DependencyInjection\Definition;

$definition = new Definition(AppExtension::class);
$definition->setPublic(false);
$definition->addTag('twig.extension');
$container->setDefinition('app.twig_extension', $definition);
$container->register(AppExtension::class)
->setPublic(false)
->addTag('twig.extension');

The ``twig.extension`` tag is a special tag that the TwigBundle uses
during configuration. By giving the service this ``twig.extension`` tag,
the bundle knows that the ``app.twig_extension`` service should be registered
the bundle knows that the ``AppExtension::class`` service should be registered
as a Twig extension with Twig. In other words, Twig finds all services tagged
with ``twig.extension`` and automatically registers them as extensions.

Expand Down Expand Up @@ -359,7 +352,7 @@ To answer this, change the service declaration:
tags:
- { name: app.mail_transport }

versionadded:: 3.3
.. versionadded:: 3.3
Support for the compact tag notation in the YAML format was introduced
in Symfony 3.3.

Expand Down
3 changes: 1 addition & 2 deletions session/locale_sticky_session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ Then register the listener:
app.locale_listener:
class: AppBundle\EventListener\LocaleListener
arguments: ['%kernel.default_locale%']
tags:
- { name: kernel.event_subscriber }
tags: [kernel.event_subscriber]

.. code-block:: xml

Expand Down
9 changes: 4 additions & 5 deletions templating/twig_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ As an example you'll create a price filter to format a given number into price::
}

.. note::

  Prior to Twig 1.26, your extension had to define an additional ``getName()``
method that returned a string with the extension's internal name (e.g.
``app.my_extension``). When your extension needs to be compatible with Twig
versions before 1.26, include this method which is omitted in the example
versions before 1.26, include this method which is omitted in the example
above.

.. tip::

Along with custom filters, you can also add custom `functions`_ and register
Expand All @@ -78,8 +78,7 @@ Now you must let the Service Container know about your newly created Twig Extens
app.twig_extension:
class: AppBundle\Twig\AppExtension
public: false
tags:
- { name: twig.extension }
tags: [twig.extension]

.. code-block:: xml

Expand Down
3 changes: 1 addition & 2 deletions validation/custom_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ tag so that the validation system knows about it:
services:
app.contains_alphanumeric_validator:
class: AppBundle\Validator\Constraints\ContainsAlphanumericValidator
tags:
- { name: validator.constraint_validator }
tags: [validator.constraint_validator]

.. code-block:: xml

Expand Down