8000 Merge branch '4.4' into 5.4 · symfony/symfony-docs@450a19f · GitHub
[go: up one dir, main page]

Skip to content

Commit 450a19f

Browse files
committed
Merge branch '4.4' into 5.4
* 4.4: refactor(reference): document kernel.locale_aware Adding caution box on dependecy injection
2 parents 8a4b3dc + de881b3 commit 450a19f

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,77 @@ To add a new rendering strategy - in addition to the core strategies like
639639
:class:`Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface`,
640640
register it as a service, then tag it with ``kernel.fragment_renderer``.
641641

642+
kernel.locale_aware
643+
-------------------
644+
645+
.. versionadded:: 4.3
646+
647+
The ``kernel.locale_aware`` tag was introduced in Symfony 4.3.
648+
649+
**Purpose**: To access and use the current :doc:`locale </translation/locale>`
650+
651+
Setting and retrieving the locale can be done via configuration or using
652+
container parameters, listeners, route parameters or the current request.
653+
654+
Thanks to the ``Translation`` contract, the locale can be set via services.
655+
656+
To register your own locale aware service, first create a service that implements
657+
the :class:`Symfony\\Contracts\\Translation\\LocaleAwareInterface` interface::
658+
659+
// src/Locale/MyCustomLocaleHandler.php
660+
namespace App\Locale;
661+
662+
use Symfony\Contracts\Translation\LocaleAwareInterface;
663+
664+
class MyCustomLocaleHandler implements LocaleAwareInterface
665+
{
666+
public function setLocale($locale)
667+
{
668+
$this->locale = $locale;
669+
}
670+
671+
public function getLocale()
672+
{
673+
return $this->locale;
674+
}
675+
}
676+
677+
If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
678+
your service will be automatically tagged with ``kernel.locale_aware``. But, you
679+
can also register it manually:
680+
681+
.. configuration-block::
682+
683+
.. code-block:: yaml
684+
685+
services:
686+
App\Locale\MyCustomLocaleHandler:
687+
tags: [kernel.locale_aware]
688+
689+
.. code-block:: xml
690+
691+
<?xml version="1.0" encoding="UTF-8" ?>
692+
<container xmlns="http://symfony.com/schema/dic/services"
693+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
694+
xsi:schemaLocation="http://symfony.com/schema/dic/services
695+
https://symfony.com/schema/dic/services/services-1.0.xsd">
696+
697+
<services>
698+
<service id="App\Locale\MyCustomLocaleHandler">
699+
<tag name="kernel.locale_aware"/>
700+
</service>
701+
</services>
702+
</container>
703+
704+
.. code-block:: php
705+
706+
use App\Locale\MyCustomLocaleHandler;
707+
708+
$container
709+
->register(LocaleHandler::class)
710+
->addTag('kernel.locale_aware')
711+
;
712+
642713
kernel.reset
643714
------------
644715

0 commit comments

Comments
 (0)
0