Commit 349dcf97 authored by Fabien Potencier's avatar Fabien Potencier

moved everything related to the routing to a reusable service provider

parent e96f9be8
...@@ -19,7 +19,6 @@ use Symfony\Component\HttpKernel\Event\FilterResponseEvent; ...@@ -19,7 +19,6 @@ use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\PostResponseEvent; use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\HttpKernel\EventListener\ResponseListener; use Symfony\Component\HttpKernel\EventListener\ResponseListener;
use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
...@@ -29,7 +28,6 @@ use Symfony\Component\HttpFoundation\RedirectResponse; ...@@ -29,7 +28,6 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
use Silex\Api\BootableProviderInterface; use Silex\Api\BootableProviderInterface;
use Silex\Api\EventListenerProviderInterface; use Silex\Api\EventListenerProviderInterface;
use Silex\Api\ControllerProviderInterface; use Silex\Api\ControllerProviderInterface;
...@@ -37,7 +35,7 @@ use Silex\Api\ServiceProviderInterface; ...@@ -37,7 +35,7 @@ use Silex\Api\ServiceProviderInterface;
use Silex\EventListener\MiddlewareListener; use Silex\EventListener\MiddlewareListener;
use Silex\EventListener\ConverterListener; use Silex\EventListener\ConverterListener;
use Silex\EventListener\StringToResponseListener; use Silex\EventListener\StringToResponseListener;
use Silex\Provider\Router\LazyUrlMatcher; use Silex\Provider\RoutingServiceProvider;
/** /**
* The Silex framework class. * The Silex framework class.
...@@ -94,10 +92,6 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte ...@@ -94,10 +92,6 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
$this['dispatcher'] = $this->share(function () use ($app) { $this['dispatcher'] = $this->share(function () use ($app) {
$dispatcher = new $app['dispatcher_class'](); $dispatcher = new $app['dispatcher_class']();
$urlMatcher = new LazyUrlMatcher(function () use ($app) {
return $app['url_matcher'];
});
$dispatcher->addSubscriber(new RouterListener($urlMatcher, $app['request_context'], $app['logger'], $app['request_stack']));
if (isset($app['exception_handler'])) { if (isset($app['exception_handler'])) {
$dispatcher->addSubscriber($app['exception_handler']); $dispatcher->addSubscriber($app['exception_handler']);
} }
...@@ -125,24 +119,13 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte ...@@ -125,24 +119,13 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
return new RequestStack(); return new RequestStack();
}); });
$this['request_context'] = $this->share(function () use ($app) {
$context = new RequestContext();
$context->setHttpPort($app['request.http_port']);
$context->setHttpsPort($app['request.https_port']);
return $context;
});
$this['url_matcher'] = $this->share(function () use ($app) {
return new RedirectableUrlMatcher($app['routes'], $app['request_context']);
});
$this['request.http_port'] = 80; $this['request.http_port'] = 80;
$this['request.https_port'] = 443; $this['request.https_port'] = 443;
$this['debug'] = false; $this['debug'] = false;
$this['charset'] = 'UTF-8'; $this['charset'] = 'UTF-8';
$this->register(new RoutingServiceProvider());
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
$this[$key] = $value; $this[$key] = $value;
} }
......
...@@ -14,7 +14,7 @@ namespace Silex\Provider; ...@@ -14,7 +14,7 @@ namespace Silex\Provider;
use Silex\Api\ServiceProviderInterface; use Silex\Api\ServiceProviderInterface;
use Silex\Api\EventListenerProviderInterface; use Silex\Api\EventListenerProviderInterface;
use Silex\Provider\Locale\LocaleListener; use Silex\Provider\Locale\LocaleListener;
use Silex\Provider\Router\LazyUrlMatcher; use Silex\Provider\Routing\LazyUrlMatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/** /**
...@@ -44,8 +44,4 @@ class LocaleServiceProvider implements ServiceProviderInterface, EventListenerPr ...@@ -44,8 +44,4 @@ class LocaleServiceProvider implements ServiceProviderInterface, EventListenerPr
{ {
$dispatcher->addSubscriber($app['locale.listener']); $dispatcher->addSubscriber($app['locale.listener']);
} }
public function boot(Application $app)
{
}
} }
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Silex\Provider\Router; namespace Silex\Provider\Routing;
use Symfony\Component\Routing\RequestContext as SymfonyRequestContext; use Symfony\Component\Routing\RequestContext as SymfonyRequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface; use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Silex; namespace Silex\Provider\Routing;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcher as BaseRedirectableUrlMatcher; use Symfony\Component\Routing\Matcher\RedirectableUrlMatcher as BaseRedirectableUrlMatcher;
......
...@@ -12,14 +12,20 @@ ...@@ -12,14 +12,20 @@
namespace Silex\Provider; namespace Silex\Provider;
use Silex\Api\ServiceProviderInterface; use Silex\Api\ServiceProviderInterface;
use Silex\Api\EventListenerProviderInterface;
use Silex\Provider\Routing\RedirectableUrlMatcher;
use Silex\Provider\Routing\LazyUrlMatcher;
use Symfony\Component\Routing\Generator\UrlGenerator; use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/** /**
* Symfony Routing component Provider for URL generation. * Symfony Routing component Provider.
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
*/ */
class UrlGeneratorServiceProvider implements ServiceProviderInterface class RoutingServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
{ {
public function register(\Pimple $app) public function register(\Pimple $app)
{ {
...@@ -28,5 +34,31 @@ class UrlGeneratorServiceProvider implements ServiceProviderInterface ...@@ -28,5 +34,31 @@ class UrlGeneratorServiceProvider implements ServiceProviderInterface
return new UrlGenerator($app['routes'], $app['request_context']); return new UrlGenerator($app['routes'], $app['request_context']);
}); });
$app['url_matcher'] = $app->share(function () use ($app) {
return new RedirectableUrlMatcher($app['routes'], $app['request_context']);
});
$app['request_context'] = $app->share(function () use ($app) {
$context = new RequestContext();
$context->setHttpPort(isset($app['request.http_port']) ? $app['request.http_port'] : 80);
$context->setHttpsPort(isset($app['request.https_port']) ? $app['request.https_port'] : 443);
return $context;
});
$app['routing.listener'] = $app->share(function () use ($app) {
$urlMatcher = new LazyUrlMatcher(function () use ($app) {
return $app['url_matcher'];
});
return new RouterListener($urlMatcher, $app['request_context'], $app['logger'], $app['request_stack']);
});
}
public function subscribe(\Pimple $app, EventDispatcherInterface $dispatcher)
{
$dispatcher->addSubscriber($app['routing.listener']);
} }
} }
...@@ -120,7 +120,10 @@ class SecurityServiceProvider implements ServiceProviderInterface, EventListener ...@@ -120,7 +120,10 @@ class SecurityServiceProvider implements ServiceProviderInterface, EventListener
$app['security.channel_listener'] = $app->share(function ($app) { $app['security.channel_listener'] = $app->share(function ($app) {
return new ChannelListener( return new ChannelListener(
$app['security.access_map'], $app['security.access_map'],
new RetryAuthenticationEntryPoint($app['request.http_port'], $app['request.https_port']), new RetryAuthenticationEntryPoint(
isset($app['request.http_port']) ? $app['request.http_port'] : 80,
isset($app['request.https_port']) ? $app['request.https_port'] : 443
),
$app['logger'] $app['logger']
); );
}); });
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
namespace Silex\Tests\Application; namespace Silex\Tests\Application;
use Silex\Application; use Silex\Application;
use Silex\Provider\UrlGeneratorServiceProvider; use Silex\Provider\RoutingServiceProvider;
/** /**
* UrlGeneratorTrait test cases. * UrlGeneratorTrait test cases.
...@@ -25,7 +25,7 @@ class UrlGeneratorTraitTest extends \PHPUnit_Framework_TestCase ...@@ -25,7 +25,7 @@ class UrlGeneratorTraitTest extends \PHPUnit_Framework_TestCase
{ {
public function testUrl() public function testUrl()
{ {
$app = $this->createApplication(); $app = new UrlGeneratorApplication();
$app['url_generator'] = $translator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->disableOriginalConstructor()->getMock(); $app['url_generator'] = $translator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->disableOriginalConstructor()->getMock();
$translator->expects($this->once())->method('generate')->with('foo', array(), true); $translator->expects($this->once())->method('generate')->with('foo', array(), true);
$app->url('foo'); $app->url('foo');
...@@ -33,17 +33,9 @@ class UrlGeneratorTraitTest extends \PHPUnit_Framework_TestCase ...@@ -33,17 +33,9 @@ class UrlGeneratorTraitTest extends \PHPUnit_Framework_TestCase
public function testPath() public function testPath()
{ {
$app = $this->createApplication(); $app = new UrlGeneratorApplication();
$app['url_generator'] = $translator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->disableOriginalConstructor()->getMock(); $app['url_generator'] = $translator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->disableOriginalConstructor()->getMock();
$translator->expects($this->once())->method('generate')->with('foo', array(), false); $translator->expects($this->once())->method('generate')->with('foo', array(), false);
$app->path('foo'); $app->path('foo');
} }
public function createApplication()
{
$app = new UrlGeneratorApplication();
$app->register(new UrlGeneratorServiceProvider());
return $app;
}
} }
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
namespace Silex\Tests; namespace Silex\Tests;
use Silex\Provider\Router\LazyUrlMatcher; use Silex\Provider\Routing\LazyUrlMatcher;
/** /**
* LazyUrlMatcher test case. * LazyUrlMatcher test case.
......
...@@ -12,22 +12,20 @@ ...@@ -12,22 +12,20 @@
namespace Silex\Tests\Provider; namespace Silex\Tests\Provider;
use Silex\Application; use Silex\Application;
use Silex\Provider\UrlGeneratorServiceProvider; use Silex\Provider\RoutingServiceProvider;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
/** /**
* UrlGeneratorProvider test cases. * RoutingProvider test cases.
* *
* @author Igor Wiedler <igor@wiedler.ch> * @author Igor Wiedler <igor@wiedler.ch>
*/ */
class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase class RoutingServiceProviderTest extends \PHPUnit_Framework_TestCase
{ {
public function testRegister() public function testRegister()
{ {
$app = new Application(); $app = new Application();
$app->register(new UrlGeneratorServiceProvider());
$app->get('/hello/{name}', function ($name) {}) $app->get('/hello/{name}', function ($name) {})
->bind('hello'); ->bind('hello');
...@@ -43,8 +41,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -43,8 +41,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase
{ {
$app = new Application(); $app = new Application();
$app->register(new UrlGeneratorServiceProvider());
$app->get('/hello/{name}', function ($name) {}) $app->get('/hello/{name}', function ($name) {})
->bind('hello'); ->bind('hello');
...@@ -62,8 +58,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -62,8 +58,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase
{ {
$app = new Application(); $app = new Application();
$app->register(new UrlGeneratorServiceProvider());
$app->get('/hello/{name}', function ($name) {}) $app->get('/hello/{name}', function ($name) {})
->bind('hello'); ->bind('hello');
...@@ -81,8 +75,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -81,8 +75,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase
{ {
$app = new Application(); $app = new Application();
$app->register(new UrlGeneratorServiceProvider());
$app->get('/insecure', function () {}) $app->get('/insecure', function () {})
->bind('insecure_page') ->bind('insecure_page')
->requireHttp(); ->requireHttp();
...@@ -101,8 +93,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -101,8 +93,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase
{ {
$app = new Application(); $app = new Application();
$app->register(new UrlGeneratorServiceProvider());
$app->get('/secure', function () {}) $app->get('/secure', function () {})
->bind('secure_page') ->bind('secure_page')
->requireHttps(); ->requireHttps();
......
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