Commit 25cc8b71 authored by Fabien Potencier's avatar Fabien Potencier

feature #1385 Fix deprecations under Symfony 3.1 (romainneutron)

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

Discussion
----------

Fix deprecations under Symfony 3.1

Commits
-------

5ba8ccf0 Fix deprecations under Symfony 3.1
parents 48df8d24 5ba8ccf0
......@@ -2,6 +2,10 @@ language: php
sudo: false
env:
global:
- SYMFONY_DEPRECATIONS_HELPER=weak
cache:
directories:
- $HOME/.composer/cache
......
<?php
/*
* This file is part of the Silex framework.
*
* (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 Silex;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
/**
* HttpKernel Argument Resolver for Silex.
*
* @author Romain Neutron <imprec@gmail.com>
*/
class AppArgumentValueResolver implements ArgumentValueResolverInterface
{
private $app;
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* {@inheritdoc}
*/
public function supports(Request $request, ArgumentMetadata $argument)
{
return $argument->getType() === Application::class || (null !== $argument->getType() && in_array(Application::class, class_parents($argument->getType()), true));
}
/**
* {@inheritdoc}
*/
public function resolve(Request $request, ArgumentMetadata $argument)
{
yield $this->app;
}
}
......@@ -19,6 +19,8 @@ use Symfony\Component\HttpFoundation\Request;
* Adds Application as a valid argument for controllers.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @deprecated This class can be dropped once Symfony 3.0 is not supported anymore.
*/
class ControllerResolver extends BaseControllerResolver
{
......
......@@ -5,6 +5,7 @@ namespace Silex\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Silex\Api\EventListenerProviderInterface;
use Silex\AppArgumentValueResolver;
use Silex\CallbackResolver;
use Silex\ControllerResolver;
use Silex\EventListener\ConverterListener;
......@@ -12,9 +13,17 @@ use Silex\EventListener\MiddlewareListener;
use Silex\EventListener\StringToResponseListener;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestValueResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\VariadicValueResolver;
use Symfony\Component\HttpKernel\Controller\ControllerResolver as SfControllerResolver;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
use Symfony\Component\HttpKernel\EventListener\ResponseListener;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\Kernel;
class HttpKernelServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
{
......@@ -24,11 +33,40 @@ class HttpKernelServiceProvider implements ServiceProviderInterface, EventListen
public function register(Container $app)
{
$app['resolver'] = function ($app) {
if (Kernel::VERSION_ID >= 30100) {
return new SfControllerResolver($app['logger']);
}
return new ControllerResolver($app, $app['logger']);
};
if (Kernel::VERSION_ID >= 30100) {
$app['argument_metadata_factory'] = function ($app) {
return new ArgumentMetadataFactory();
};
$app['argument_value_resolvers'] = function ($app) {
if (Kernel::VERSION_ID < 30200) {
return array(
new AppArgumentValueResolver($app),
new RequestAttributeValueResolver(),
new RequestValueResolver(),
new DefaultValueResolver(),
new VariadicValueResolver(),
);
}
return array_merge(array(new AppArgumentValueResolver($app)), ArgumentResolver::getDefaultArgumentValueResolvers());
};
}
$app['argument_resolver'] = function ($app) {
if (Kernel::VERSION_ID >= 30100) {
return new ArgumentResolver($app['argument_metadata_factory'], $app['argument_value_resolvers']);
}
};
$app['kernel'] = function ($app) {
return new HttpKernel($app['dispatcher'], $app['resolver'], $app['request_stack']);
return new HttpKernel($app['dispatcher'], $app['resolver'], $app['request_stack'], $app['argument_resolver']);
};
$app['request_stack'] = function () {
......
......@@ -183,6 +183,15 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('Hello Fabien', $app->handle(Request::create('/Fabien'))->getContent());
}
public function testApplicationTypeHintWorks()
{
$app = new SpecialApplication();
$app->get('/{name}', 'Silex\Tests\FooController::barSpecialAction');
$this->assertEquals('Hello Fabien in Silex\Tests\SpecialApplication', $app->handle(Request::create('/Fabien'))->getContent());
}
public function testHttpSpec()
{
$app = new Application();
......@@ -668,6 +677,11 @@ class FooController
{
return 'Hello '.$app->escape($name);
}
public function barSpecialAction(SpecialApplication $app, $name)
{
return 'Hello '.$app->escape($name).' in '.get_class($app);
}
}
class IncorrectControllerCollection implements ControllerProviderInterface
......@@ -681,3 +695,7 @@ class IncorrectControllerCollection implements ControllerProviderInterface
class RouteCollectionSubClass extends RouteCollection
{
}
class SpecialApplication extends Application
{
}
......@@ -22,6 +22,9 @@ use Symfony\Component\HttpFoundation\Request;
*/
class ControllerResolverTest extends \PHPUnit_Framework_TestCase
{
/**
* @group legacy
*/
public function testGetArguments()
{
$app = new 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