Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Symfony\Component\Routing\Matcher\UrlMatcher;
- use Symfony\Component\Routing\RequestContext;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpKernel\HttpKernel;
- use Symfony\Component\HttpKernel\Kernel;
- use Symfony\Component\EventDispatcher\EventDispatcher;
- use Symfony\Component\HttpKernel\Controller\ControllerResolver;
- use Symfony\Component\HttpKernel\EventListener\RouterListener;
- use Symfony\Component\Config\FileLocator;
- use Symfony\Component\Routing\Loader\YamlFileLoader;
- use Symfony\Component\DependencyInjection\ContainerBuilder;
- use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
- class CKRouter {
- function __construct() {
- #FIXME clean the routing path. Ugly.
- $yml = new YamlFileLoader(new FileLocator($currentPath = __DIR__ . '/../../../'));
- #importing routing from yml file. Symfony syntax.
- $this->routes = $yml->load('routing.yml');
- $this->matcher = new UrlMatcher($this->routes, new RequestContext());
- #dispatcher for the route resolution
- $container = new ContainerBuilder();
- $this->dispatcher = new ContainerAwareEventDispatcher($container);
- $this->dispatcher->addSubscriber(new RouterListener($this->matcher));
- #default Controller resolver.
- $this->resolver = new ControllerResolver();
- $this->kernel = new HttpKernel($this->dispatcher, $this->resolver);
- }
- function run() {
- $request = Request::createFromGlobals();
- #separate those for unit testing purposes.
- $response = $this->_run($request);
- $this->_output($response);
- $kernel->terminate($request, $response);
- }
- function _run($request) {
- $response = $this->kernel->handle($request);
- return $response;
- }
- function _output($response) {
- $response->send();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement