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