Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Synchronized Service alternative, backwards compatible #8904

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
merged 7 commits into from
Sep 8, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[HttpKernel] modified listeners to be BC with Symfony <2.4
  • Loading branch information
fabpot committed Sep 7, 2013
commit 93e60eaeabe74f11371cbf7597ab4d7026f260f3
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testUnknownFragmentRenderer()
->disableOriginalConstructor()
->getMock()
;
$renderer = new FragmentHandler($context, array());
$renderer = new FragmentHandler(array(), false, $context);

$this->setExpectedException('InvalidArgumentException', 'The "inline" renderer does not exist.');
$renderer->render('/foo');
Expand All @@ -64,7 +64,7 @@ protected function getFragmentHandler($return)

$context->expects($this->any())->method('getCurrentRequest')->will($this->returnValue(Request::create('/')));

$renderer = new FragmentHandler($context, array($strategy));
$renderer = new FragmentHandler(array($strategy), false, $context);

return $renderer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

<services>
<service id="fragment.handler" class="%fragment.handler.class%">
<argument type="service" id="request_context" />
<argument type="collection" />
<argument>%kernel.debug%</argument>
<argument type="service" id="request_context" />
</service>

<service id="fragment.renderer.inline" class="%fragment.renderer.inline.class%">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@
<tag name="kernel.event_subscriber" />
<tag name="monolog.logger" channel="request" />
<argument type="service" id="router" />
<argument type="service" id="request_context" />
<argument type="service" id="router.request_context" on-invalid="ignore" />
<argument type="service" id="logger" on-invalid="ignore" />
<argument type="service" id="request_context" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
<service id="locale_listener" class="%locale_listener.class%">
<tag name="kernel.event_subscriber" />
<argument>%kernel.default_locale%</argument>
<argument type="service" id="request_context" />
<argument type="service" id="router" on-invalid="ignore" />
<argument type="service" id="request_context" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
/**
* Initializes the locale based on the current request.
*
* This listener works in 2 modes:
*
* * 2.3 compatibility mode where you must call setRequest whenever the Request changes.
* * 2.4+ mode where you must pass a RequestContext instance in the constructor.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class LocaleListener implements EventSubscriberInterface
Expand All @@ -30,7 +35,10 @@ class LocaleListener implements EventSubscriberInterface
private $defaultLocale;
private $requestContext;

public function __construct($defaultLocale = 'en', RequestContext $requestContext, RequestContextAwareInterface $router = null)
/**
* RequestContext will become required in 3.0.
*/
public function __construct($defaultLocale = 'en', RequestContextAwareInterface $router = null, RequestContext $requestContext = null)
{
$this->defaultLocale = $defaultLocale;
$this->requestContext = $requestContext;
Expand Down Expand Up @@ -69,6 +77,10 @@ public function onKernelRequest(GetResponseEvent $event)

public function onKernelFinishRequest(FinishRequestEvent $event)
{
if (null === $this->requestContext) {
throw new \LogicException('You must pass a RequestContext.');
}

if (null !== $parentRequest = $this->requestContext->getParentRequest()) {
$this->setRouterContext($parentRequest);
}
Expand Down
18 changes: 16 additions & 2 deletions 18 src/Symfony/Component/HttpKernel/EventListener/RouterListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
/**
* Initializes the context from the request and sets request attributes based on a matching route.
*
* This listener works in 2 modes:
*
* * 2.3 compatibility mode where you must call setRequest whenever the Request changes.
* * 2.4+ mode where you must pass a RequestContext instance in the constructor.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class RouterListener implements EventSubscriberInterface
Expand All @@ -43,13 +48,15 @@ class RouterListener implements EventSubscriberInterface
/**
* Constructor.
*
* RequestContext will become required in 3.0.
*
* @param UrlMatcherInterface|RequestMatcherInterface $matcher The Url or Request matcher
* @param RequestContext|null $context The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
* @param LoggerInterface|null $logger The logger
*
* @throws \InvalidArgumentException
*/
public function __construct($matcher, KernelRequestContext $kernelContext, RequestContext $context = null, LoggerInterface $logger = null)
public function __construct($matcher, RequestContext $context = null, LoggerInterface $logger = null, KernelRequestContext $kernelContext = null)
{
if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
Expand Down Expand Up @@ -86,6 +93,10 @@ public function setRequest(Request $request = null)

public function onKernelFinishRequest(FinishRequestEvent $event)
{
if (null === $this->kernelContext) {
throw new \LogicException('You must pass a RequestContext.');
}

$this->setRequest($this->kernelContext->getParentRequest());
}

Expand All @@ -96,7 +107,10 @@ public function onKernelRequest(GetResponseEvent $event)
// initialize the context that is also used by the generator (assuming matcher and generator share the same context instance)
// we call setRequest even if most of the time, it has already been done to keep compatibility
// with frameworks which do not use the Symfony service container
$this->setRequest($request);
// when we have a RequestContext, no need to do it
if (null !== $this->kernelContext) {
$this->setRequest($request);
}

if ($request->attributes->has('_controller')) {
// routing is already done
Expand Down
37 changes: 33 additions & 4 deletions 37 src/Symfony/Component/HttpKernel/Fragment/FragmentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
* This class handles the rendering of resource fragments that are included into
* a main resource. The handling of the rendering is managed by specialized renderers.
*
* This listener works in 2 modes:
*
* * 2.3 compatibility mode where you must call setRequest whenever the Request changes.
* * 2.4+ mode where you must pass a RequestContext instance in the constructor.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @see FragmentRendererInterface
Expand All @@ -31,15 +36,18 @@ class FragmentHandler
{
private $debug;
private $renderers;
private $request;
private $context;

/**
* Constructor.
*
* RequestContext will become required in 3.0.
*
* @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances
* @param Boolean $debug Whether the debug mode is enabled or not
*/
public function __construct(RequestContext $context, array $renderers = array(), $debug = false)
public function __construct(array $renderers = array(), $debug = false, RequestContext $context = null)
{
$this->context = $context;
$this->renderers = array();
Expand All @@ -59,6 +67,22 @@ public function addRenderer(FragmentRendererInterface $renderer)
$this->renderers[$renderer->getName()] = $renderer;
}

/**
* Sets the current Request.
*
* This method was used to synchronize the Request, but as the HttpKernel
* is doing that automatically now, you should never be called it directly.
* It is kept public for BC with the 2.3 version.
*
* @param Request|null $request A Request instance
*
* @deprecated Deprecated since version 2.4, to be removed in 3.0.
*/
public function setRequest(Request $request = null)
{
$this->request = $request;
}

/**
* Renders a URI and returns the Response content.
*
Expand All @@ -85,11 +109,11 @@ public function render($uri, $renderer = 'inline', array $options = array())
throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
}

if (null === $this->context->getCurrentRequest()) {
if (!$request = $this->getRequest()) {
throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
}

return $this->deliver($this->renderers[$renderer]->render($uri, $this->context->getCurrentRequest(), $options));
return $this->deliver($this->renderers[$renderer]->render($uri, $request, $options));
}

/**
Expand All @@ -107,7 +131,7 @@ public function render($uri, $renderer = 'inline', array $options = array())
protected function deliver(Response $response)
{
if (!$response->isSuccessful()) {
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->context->getCurrentRequest()->getUri(), $response->getStatusCode()));
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->getRequest()->getUri(), $response->getStatusCode()));
}

if (!$response instanceof StreamedResponse) {
Expand All @@ -116,4 +140,9 @@ protected function deliver(Response $response)

$response->sendContent();
}

private function getRequest()
{
return $this->context ? $this->context->getCurrentRequest() : $this->request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function setUp()

public function testDefaultLocaleWithoutSession()
{
$listener = new LocaleListener('fr', $this->context);
$listener = new LocaleListener('fr', null, $this->context);
$event = $this->getEvent($request = Request::create('/'));

$listener->onKernelRequest($event);
Expand All @@ -42,7 +42,7 @@ public function testLocaleFromRequestAttribute()
$request->cookies->set('foo', 'value');

$request->attributes->set('_locale', 'es');
$listener = new LocaleListener('fr', $this->context);
$listener = new LocaleListener('fr', null, $this->context);
$event = $this->getEvent($request);

$listener->onKernelRequest($event);
Expand All @@ -61,7 +61,7 @@ public function testLocaleSetForRoutingContext()
$request = Request::create('/');

$request->attributes->set('_locale', 'es');
$listener = new LocaleListener('fr', $this->context, $router);
$listener = new LocaleListener('fr', $router, $this->context);
$listener->onKernelRequest($this->getEvent($request));
}

Expand All @@ -85,15 +85,15 @@ public function testRouterResetWithParentRequestOnKernelFinishRequest()

$event = $this->getMock('Symfony\Component\HttpKernel\Event\FinishRequestEvent', array(), array(), '', false);

$listener = new LocaleListener('fr', $this->context, $router);
$listener = new LocaleListener('fr', $router, $this->context);
$listener->onKernelFinishRequest($event);
}

public function testRequestLocaleIsNotOverridden()
{
$request = Request::create('/');
$request->setLocale('de');
$listener = new LocaleListener('fr', $this->context);
$listener = new LocaleListener('fr', null, $this->context);
$event = $this->getEvent($request);

$listener->onKernelRequest($event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testPort($defaultHttpPort, $defaultHttpsPort, $uri, $expectedHtt
->method('getContext')
->will($this->returnValue($context));

$listener = new RouterListener($urlMatcher, $this->kernelContext);
$listener = new RouterListener($urlMatcher, null, null, $this->kernelContext);
$event = $this->createGetResponseEventForUri($uri);
$listener->onKernelRequest($event);

Expand Down Expand Up @@ -80,7 +80,7 @@ private function createGetResponseEventForUri($uri)
*/
public function testInvalidMatcher()
{
new RouterListener(new \stdClass(), $this->kernelContext);
new RouterListener(new \stdClass(), null, null, $this->kernelContext);
}

public function testRequestMatcher()
Expand All @@ -95,7 +95,7 @@ public function testRequestMatcher()
->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
->will($this->returnValue(array()));

$listener = new RouterListener($requestMatcher, $this->kernelContext, new RequestContext());
$listener = new RouterListener($requestMatcher, new RequestContext(), null, $this->kernelContext);
$listener->onKernelRequest($event);
}

Expand All @@ -116,7 +116,7 @@ public function testSubRequestWithDifferentMethod()
->method('getContext')
->will($this->returnValue($context));

$listener = new RouterListener($requestMatcher, $this->kernelContext, new RequestContext());
$listener = new RouterListener($requestMatcher, new RequestContext(), null, $this->kernelContext);
$listener->onKernelRequest($event);

// sub-request with another HTTP method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function setUp()
*/
public function testRenderWhenRendererDoesNotExist()
{
$handler = new FragmentHandler($this->context);
$handler = new FragmentHandler(array(), null, $this->context);
$handler->render('/', 'foo');
}

Expand Down Expand Up @@ -87,7 +87,7 @@ protected function getHandler($returnValue, $arguments = array())
call_user_func_array(array($e, 'with'), $arguments);
}

$handler = new FragmentHandler($this->context);
$handler = new FragmentHandler(array(), null, $this->context);
$handler->addRenderer($renderer);

return $handler;
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.