-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
GH-7007: Synchronized Service alternative, backwards compatible. #7707
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
Changes from all commits
db4d934
705f0d4
01ac8c9
ec539ae
0ef3664
778eaea
5d06aff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\HttpKernel; | ||
use Symfony\Component\HttpKernel\RequestStack; | ||
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
@@ -29,19 +30,22 @@ | |
class ContainerAwareHttpKernel extends HttpKernel | ||
{ | ||
protected $container; | ||
protected $requestStack; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance | ||
* @param ContainerInterface $container A ContainerInterface instance | ||
* @param ControllerResolverInterface $controllerResolver A ControllerResolverInterface instance | ||
* @param RequestStack $requestStack A stack for master/sub requests | ||
*/ | ||
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver) | ||
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver, RequestStack $requestStack) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should accept null by default for BC. |
||
{ | ||
parent::__construct($dispatcher, $controllerResolver); | ||
parent::__construct($dispatcher, $controllerResolver, $requestStack); | ||
|
||
$this->container = $container; | ||
|
||
$container->addScope(new Scope('request')); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?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\Event; | ||
|
||
/** | ||
* Triggered whenever a request is fully processed. | ||
* | ||
* @author Benjamin Eberlei <kontakt@beberlei.de> | ||
*/ | ||
class RequestFinishedEvent extends KernelEvent | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,9 @@ | |
namespace Symfony\Component\HttpKernel\EventListener; | ||
|
||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\HttpKernel\Event\RequestFinishedEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\HttpKernel\RequestContext; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Routing\RequestContextAwareInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
@@ -26,41 +28,64 @@ class LocaleListener implements EventSubscriberInterface | |
{ | ||
private $router; | ||
private $defaultLocale; | ||
private $requestContext; | ||
|
||
public function __construct($defaultLocale = 'en', RequestContextAwareInterface $router = null) | ||
public function __construct($defaultLocale = 'en', RequestContext $requestContext, RequestContextAwareInterface $router = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a BC break. Not sure how to deal with it. The only thing that comes to my mind is to not used the new request context and keep the old way. |
||
{ | ||
$this->defaultLocale = $defaultLocale; | ||
$this->requestContext = $requestContext; | ||
$this->router = $router; | ||
} | ||
|
||
public function setRequest(Request $request = null) | ||
public function onKernelRequest(GetResponseEvent $event) | ||
{ | ||
$request = $event->getRequest(); | ||
$request->setDefaultLocale($this->defaultLocale); | ||
|
||
$this->setLocale($request); | ||
$this->setRouterContext($request); | ||
} | ||
|
||
public function onKernelRequestFinished(RequestFinishedEvent $event) | ||
{ | ||
if (null === $request) { | ||
$this->resetRouterContext(); | ||
} | ||
|
||
private function resetRouterContext() | ||
{ | ||
if ($this->requestContext === null) { | ||
return; | ||
} | ||
|
||
$parentRequest = $this->requestContext->getParentRequest(); | ||
|
||
if ($parentRequest === null) { | ||
return; | ||
} | ||
|
||
$this->setRouterContext($parentRequest); | ||
} | ||
|
||
private function setLocale(Request $request) | ||
{ | ||
if ($locale = $request->attributes->get('_locale')) { | ||
$request->setLocale($locale); | ||
} | ||
} | ||
|
||
private function setRouterContext(Request $request) | ||
{ | ||
if (null !== $this->router) { | ||
$this->router->getContext()->setParameter('_locale', $request->getLocale()); | ||
} | ||
} | ||
|
||
public function onKernelRequest(GetResponseEvent $event) | ||
{ | ||
$request = $event->getRequest(); | ||
$request->setDefaultLocale($this->defaultLocale); | ||
|
||
$this->setRequest($request); | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
// must be registered after the Router to have access to the _locale | ||
KernelEvents::REQUEST => array(array('onKernelRequest', 16)), | ||
KernelEvents::REQUEST_FINISHED => array(array('onKernelRequestFinished', 0)), | ||
); | ||
} | ||
} |
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.
What about marking it private?