-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel][Framework] Locale aware services #28929
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
fabpot
merged 1 commit into
symfony:master
from
neghmurken:feature/locale-aware-services
Apr 3, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Symfony/Component/HttpKernel/DependencyInjection/RegisterLocaleAwareServicesPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Register all services that have the "kernel.locale_aware" tag into the listener. | ||
* | ||
* @author Pierre Bobiet <pierrebobiet@gmail.com> | ||
*/ | ||
class RegisterLocaleAwareServicesPass implements CompilerPassInterface | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private $listenerServiceId; | ||
private $localeAwareTag; | ||
|
||
public function __construct(string $listenerServiceId = 'locale_aware_listener', string $localeAwareTag = 'kernel.locale_aware') | ||
{ | ||
$this->listenerServiceId = $listenerServiceId; | ||
$this->localeAwareTag = $localeAwareTag; | ||
} | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition($this->listenerServiceId)) { | ||
return; | ||
} | ||
|
||
$services = []; | ||
|
||
foreach ($container->findTaggedServiceIds($this->localeAwareTag) as $id => $tags) { | ||
$services[] = new Reference($id); | ||
} | ||
|
||
if (!$services) { | ||
$container->removeDefinition($this->listenerServiceId); | ||
|
||
return; | ||
} | ||
|
||
$container | ||
->getDefinition($this->listenerServiceId) | ||
->setArgument(0, new IteratorArgument($services)) | ||
; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Symfony/Component/HttpKernel/EventListener/LocaleAwareListener.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\EventListener; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpKernel\Event\FinishRequestEvent; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Contracts\Translation\LocaleAwareInterface; | ||
|
||
/** | ||
* Pass the current locale to the provided services. | ||
* | ||
* @author Pierre Bobiet <pierrebobiet@gmail.com> | ||
*/ | ||
class LocaleAwareListener implements EventSubscriberInterface | ||
{ | ||
private $localeAwareServices; | ||
private $requestStack; | ||
|
||
/** | ||
* @param LocaleAwareInterface[] $localeAwareServices | ||
*/ | ||
public function __construct(iterable $localeAwareServices, RequestStack $requestStack) | ||
{ | ||
$this->localeAwareServices = $localeAwareServices; | ||
$this->requestStack = $requestStack; | ||
} | ||
|
||
public function onKernelRequest(RequestEvent $event): void | ||
{ | ||
$this->setLocale($event->getRequest()->getLocale(), $event->getRequest()->getDefaultLocale()); | ||
} | ||
|
||
public function onKernelFinishRequest(FinishRequestEvent $event): void | ||
{ | ||
if (null === $parentRequest = $this->requestStack->getParentRequest()) { | ||
$this->setLocale($event->getRequest()->getDefaultLocale()); | ||
|
||
return; | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
$this->setLocale($parentRequest->getLocale(), $parentRequest->getDefaultLocale()); | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
// must be registered after the Locale listener | ||
KernelEvents::REQUEST => [['onKernelRequest', 15]], | ||
KernelEvents::FINISH_REQUEST => [['onKernelFinishRequest', -15]], | ||
]; | ||
} | ||
|
||
private function setLocale(string $locale, ?string $defaultLocale = null): void | ||
{ | ||
foreach ($this->localeAwareServices as $service) { | ||
try { | ||
$service->setLocale($locale); | ||
} catch (\InvalidArgumentException $e) { | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$service->setLocale($defaultLocale); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...ny/Component/HttpKernel/Tests/DependencyInjection/RegisterLocaleAwareServicesPassTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\HttpKernel\Tests\DependencyInjection; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\HttpKernel\DependencyInjection\RegisterLocaleAwareServicesPass; | ||
use Symfony\Component\HttpKernel\EventListener\LocaleAwareListener; | ||
use Symfony\Contracts\Translation\LocaleAwareInterface; | ||
|
||
class RegisterLocaleAwareServicesPassTest extends TestCase | ||
{ | ||
public function testCompilerPass() | ||
{ | ||
$container = new ContainerBuilder(); | ||
|
||
$container->register('locale_aware_listener', LocaleAwareListener::class) | ||
->setPublic(true) | ||
->setArguments([null, null]); | ||
|
||
$container->register('some_locale_aware_service', LocaleAwareInterface::class) | ||
->setPublic(true) | ||
->addTag('kernel.locale_aware'); | ||
|
||
$container->register('another_locale_aware_service', LocaleAwareInterface::class) | ||
->setPublic(true) | ||
->addTag('kernel.locale_aware'); | ||
|
||
$container->addCompilerPass(new RegisterLocaleAwareServicesPass()); | ||
$container->compile(); | ||
|
||
$this->assertEquals( | ||
[ | ||
new IteratorArgument([ | ||
0 => new Reference('some_locale_aware_service'), | ||
1 => new Reference('another_locale_aware_service'), | ||
]), | ||
null, | ||
], | ||
$container->getDefinition('locale_aware_listener')->getArguments() | ||
); | ||
} | ||
|
||
public function testListenerUnregisteredWhenNoLocaleAwareServices() | ||
{ | ||
$container = new ContainerBuilder(); | ||
|
||
$container->register('locale_aware_listener', LocaleAwareListener::class) | ||
->setPublic(true) | ||
->setArguments([null, null]); | ||
|
||
$container->addCompilerPass(new RegisterLocaleAwareServicesPass()); | ||
$container->compile(); | ||
|
||
$this->assertFalse($container->hasDefinition('locale_aware_listener')); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we deprecate the service instead in case someone is relying on its presence?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can we deprecate without it conflicting with the new
LocaleAwareListener
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it do any harm if both listeners are executed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah you're right. There is no undesirable side effect beside setting the locale twice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the
translator_listener
service completely as keeping it would trigger deprecation notices. I don't think that's an issue.