Commit efe70d6a authored by Fabien Potencier's avatar Fabien Potencier

bug #1392 Fix PHP7 type hint on controllers (GromNaN)

This PR was merged into the 2.0.x-dev branch.

Discussion
----------

Fix PHP7 type hint on controllers

Fix for #1389

Commits
-------

0a382f4e Fix PHP7 type hint on controllers
parents 7e5910f4 0a382f4e
...@@ -34,7 +34,7 @@ class AppArgumentValueResolver implements ArgumentValueResolverInterface ...@@ -34,7 +34,7 @@ class AppArgumentValueResolver implements ArgumentValueResolverInterface
*/ */
public function supports(Request $request, ArgumentMetadata $argument) public function supports(Request $request, ArgumentMetadata $argument)
{ {
return $argument->getType() === Application::class || (null !== $argument->getType() && in_array(Application::class, class_parents($argument->getType()), true)); return null !== $argument->getType() && ($argument->getType() === Application::class || is_subclass_of($argument->getType(), Application::class));
} }
/** /**
......
...@@ -177,6 +177,7 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase ...@@ -177,6 +177,7 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
public function testControllersAsMethods() public function testControllersAsMethods()
{ {
$app = new Application(); $app = new Application();
unset($app['exception_handler']);
$app->get('/{name}', 'Silex\Tests\FooController::barAction'); $app->get('/{name}', 'Silex\Tests\FooController::barAction');
...@@ -186,12 +187,26 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase ...@@ -186,12 +187,26 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
public function testApplicationTypeHintWorks() public function testApplicationTypeHintWorks()
{ {
$app = new SpecialApplication(); $app = new SpecialApplication();
unset($app['exception_handler']);
$app->get('/{name}', 'Silex\Tests\FooController::barSpecialAction'); $app->get('/{name}', 'Silex\Tests\FooController::barSpecialAction');
$this->assertEquals('Hello Fabien in Silex\Tests\SpecialApplication', $app->handle(Request::create('/Fabien'))->getContent()); $this->assertEquals('Hello Fabien in Silex\Tests\SpecialApplication', $app->handle(Request::create('/Fabien'))->getContent());
} }
/**
* @requires PHP 7.0
*/
public function testPhp7TypeHintWorks()
{
$app = new SpecialApplication();
unset($app['exception_handler']);
$app->get('/{name}', 'Silex\Tests\Fixtures\Php7Controller::typehintedAction');
$this->assertEquals('Hello Fabien in Silex\Tests\SpecialApplication', $app->handle(Request::create('/Fabien'))->getContent());
}
public function testHttpSpec() public function testHttpSpec()
{ {
$app = new Application(); $app = new Application();
......
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Fixtures;
use Silex\Application;
class Php7Controller
{
public function typehintedAction(Application $application, string $name)
{
return 'Hello '.$application->escape($name).' in '.get_class($application);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment