1<?php
2
3/*
4 * This file is part of the Silex framework.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Silex\Provider;
13
14use Pimple\Container;
15use Pimple\ServiceProviderInterface;
16use Silex\ControllerCollection;
17use Silex\Api\EventListenerProviderInterface;
18use Silex\Provider\Routing\RedirectableUrlMatcher;
19use Silex\Provider\Routing\LazyRequestMatcher;
20use Symfony\Component\Routing\RouteCollection;
21use Symfony\Component\Routing\Generator\UrlGenerator;
22use Symfony\Component\Routing\RequestContext;
23use Symfony\Component\HttpKernel\EventListener\RouterListener;
24use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25
26/**
27 * Symfony Routing component Provider.
28 *
29 * @author Fabien Potencier <fabien@symfony.com>
30 */
31class RoutingServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
32{
33    public function register(Container $app)
34    {
35        $app['route_class'] = 'Silex\\Route';
36
37        $app['route_factory'] = $app->factory(function ($app) {
38            return new $app['route_class']();
39        });
40
41        $app['routes_factory'] = $app->factory(function () {
42            return new RouteCollection();
43        });
44
45        $app['routes'] = function ($app) {
46            return $app['routes_factory'];
47        };
48        $app['url_generator'] = function ($app) {
49            return new UrlGenerator($app['routes'], $app['request_context']);
50        };
51
52        $app['request_matcher'] = function ($app) {
53            return new RedirectableUrlMatcher($app['routes'], $app['request_context']);
54        };
55
56        $app['request_context'] = function ($app) {
57            $context = new RequestContext();
58
59            $context->setHttpPort(isset($app['request.http_port']) ? $app['request.http_port'] : 80);
60            $context->setHttpsPort(isset($app['request.https_port']) ? $app['request.https_port'] : 443);
61
62            return $context;
63        };
64
65        $app['controllers'] = function ($app) {
66            return $app['controllers_factory'];
67        };
68
69        $controllers_factory = function () use ($app, &$controllers_factory) {
70            return new ControllerCollection($app['route_factory'], $app['routes_factory'], $controllers_factory);
71        };
72        $app['controllers_factory'] = $app->factory($controllers_factory);
73
74        $app['routing.listener'] = function ($app) {
75            $urlMatcher = new LazyRequestMatcher(function () use ($app) {
76                return $app['request_matcher'];
77            });
78
79            return new RouterListener($urlMatcher, $app['request_stack'], $app['request_context'], $app['logger']);
80        };
81    }
82
83    public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
84    {
85        $dispatcher->addSubscriber($app['routing.listener']);
86    }
87}
88