Skip to content

[FrameworkBundle] fix FC with HttpKernel v5 #31938

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 1 commit into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@

use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* Guarantees that the _controller key is parsed into its final format.
*
* @author Ryan Weaver <ryan@knpuniversity.com>
*
* @method onKernelRequest(RequestEvent $event)
*
* @deprecated since Symfony 4.1
*/
class ResolveControllerNameSubscriber implements EventSubscriberInterface
Expand All @@ -36,8 +38,22 @@ public function __construct(ControllerNameParser $parser, bool $triggerDeprecati
$this->parser = $parser;
}

public function onKernelRequest(GetResponseEvent $event)
/**
* @internal
*/
public function resolveControllerName(...$args)
{
$this->onKernelRequest(...$args);
}

public function __call(string $method, array $args)
{
if ('onKernelRequest' !== $method && 'onKernelRequest' !== strtolower($method)) {
throw new \Error(sprintf('Error: Call to undefined method %s::%s()', \get_class($this), $method));
}

$event = $args[0];

$controller = $event->getRequest()->attributes->get('_controller');
if (\is_string($controller) && false === strpos($controller, '::') && 2 === substr_count($controller, ':')) {
// controller in the a:b:c notation then
Expand All @@ -50,7 +66,7 @@ public function onKernelRequest(GetResponseEvent $event)
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => ['onKernelRequest', 24],
KernelEvents::REQUEST => ['resolveControllerName', 24],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bundle\FrameworkBundle\EventListener\ResolveControllerNameSubscriber;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

Expand All @@ -23,9 +24,6 @@
*/
class ResolveControllerNameSubscriberTest extends TestCase
{
/**
* @group legacy
*/
public function testReplacesControllerAttribute()
{
$parser = $this->getMockBuilder(ControllerNameParser::class)->disableOriginalConstructor()->getMock();
Expand All @@ -41,6 +39,10 @@ public function testReplacesControllerAttribute()
$subscriber = new ResolveControllerNameSubscriber($parser);
$subscriber->onKernelRequest(new RequestEvent($httpKernel, $request, HttpKernelInterface::MASTER_REQUEST));
$this->assertEquals('App\\Final\\Format::methodName', $request->attributes->get('_controller'));

$subscriber = new ChildResolveControllerNameSubscriber($parser);
$subscriber->onKernelRequest(new RequestEvent($httpKernel, $request, HttpKernelInterface::MASTER_REQUEST));
$this->assertEquals('App\\Final\\Format::methodName', $request->attributes->get('_controller'));
}

/**
Expand All @@ -67,3 +69,11 @@ public function provideSkippedControllers()
yield [function () {}];
}
}

class ChildResolveControllerNameSubscriber extends ResolveControllerNameSubscriber
{
public function onKernelRequest(GetResponseEvent $event)
{
parent::onKernelRequest($event);
}
}