Skip to content

Commit 57ab485

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: refactor(reference): document kernel.locale_aware Adding caution box on dependecy injection
2 parents 22661eb + a5931d4 commit 57ab485

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

doctrine/custom_dql_functions.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,10 @@ In Symfony, you can register your custom DQL functions as follows:
135135
->datetimeFunction('test_datetime', DatetimeFunction::class);
136136
};
137137
138+
.. caution::
139+
140+
DQL functions are instantiated by Doctrine outside of the Symfony
141+
:doc:`service container </service_container>` so you can't inject services
142+
or parameters into a custom DQL function.
143+
138144
.. _`DQL User Defined Functions`: https://www.doctrine-project.org/projects/doctrine-orm/en/current/cookbook/dql-user-defined-functions.html

reference/dic_tags.rst

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,73 @@ To add a new rendering strategy - in addition to the core strategies like
615615
:class:`Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface`,
616616
register it as a service, then tag it with ``kernel.fragment_renderer``.
617617

618+
kernel.locale_aware
619+
-------------------
620+
621+
**Purpose**: To access and use the current :doc:`locale </translation/locale>`
622+
623+
Setting and retrieving the locale can be done via configuration or using
624+
container parameters, listeners, route parameters or the current request.
625+
626+
Thanks to the ``Translation`` contract, the locale can be set via services.
627+
628+
To register your own locale aware service, first create a service that implements
629+
the :class:`Symfony\\Contracts\\Translation\\LocaleAwareInterface` interface::
630+
631+
// src/Locale/MyCustomLocaleHandler.php
632+
namespace App\Locale;
633+
634+
use Symfony\Contracts\Translation\LocaleAwareInterface;
635+
636+
class MyCustomLocaleHandler implements LocaleAwareInterface
637+
{
638+
public function setLocale($locale)
639+
{
640+
$this->locale = $locale;
641+
}
642+
643+
public function getLocale()
644+
{
645+
return $this->locale;
646+
}
647+
}
648+
649+
If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
650+
your service will be automatically tagged with ``kernel.locale_aware``. But, you
651+
can also register it manually:
652+
653+
.. configuration-block::
654+
655+
.. code-block:: yaml
656+
657+
services:
658+
App\Locale\MyCustomLocaleHandler:
659+
tags: [kernel.locale_aware]
660+
661+
.. code-block:: xml
662+
663+
<?xml version="1.0" encoding="UTF-8" ?>
664+
<container xmlns="http://symfony.com/schema/dic/services"
665+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
666+
xsi:schemaLocation="http://symfony.com/schema/dic/services
667+
https://symfony.com/schema/dic/services/services-1.0.xsd">
668+
669+
<services>
670+
<service id="App\Locale\MyCustomLocaleHandler">
671+
<tag name="kernel.locale_aware"/>
672+
</service>
673+
</services>
674+
</container>
675+
676+
.. code-block:: php
677+
678+
use App\Locale\MyCustomLocaleHandler;
679+
680+
$container
681+
->register(LocaleHandler::class)
682+
->addTag('kernel.locale_aware')
683+
;
684+
618685
kernel.reset
619686
------------
620687

0 commit comments

Comments
 (0)