Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.90 KB | None | 0 0
  1. <?php
  2.  
  3. use Symfony\Component\Routing\Matcher\UrlMatcher;
  4. use Symfony\Component\Routing\RequestContext;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpKernel\HttpKernel;
  7. use Symfony\Component\HttpKernel\Kernel;
  8. use Symfony\Component\EventDispatcher\EventDispatcher;
  9. use Symfony\Component\HttpKernel\Controller\ControllerResolver;
  10. use Symfony\Component\HttpKernel\EventListener\RouterListener;
  11. use Symfony\Component\Config\FileLocator;
  12. use Symfony\Component\Routing\Loader\YamlFileLoader;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
  15.  
  16.  
  17. class CKRouter {
  18.     function __construct() {
  19.         #FIXME clean the routing path. Ugly.
  20.        $yml = new YamlFileLoader(new FileLocator($currentPath = __DIR__ . '/../../../'));
  21.  
  22.         #importing routing from yml file. Symfony syntax.
  23.        $this->routes = $yml->load('routing.yml');
  24.  
  25.         $this->matcher = new UrlMatcher($this->routes, new RequestContext());
  26.  
  27.         #dispatcher for the route resolution
  28.        $container = new ContainerBuilder();
  29.         $this->dispatcher = new ContainerAwareEventDispatcher($container);
  30.  
  31.         $this->dispatcher->addSubscriber(new RouterListener($this->matcher));
  32.    
  33.         #default Controller resolver.
  34.        $this->resolver = new ControllerResolver();
  35.  
  36.         $this->kernel = new HttpKernel($this->dispatcher, $this->resolver);
  37.     }
  38.  
  39.     function run() {
  40.         $request = Request::createFromGlobals();
  41.        
  42.         #separate those for unit testing purposes.
  43.        $response = $this->_run($request);
  44.         $this->_output($response);
  45.  
  46.         $kernel->terminate($request, $response);
  47.     }
  48.  
  49.     function _run($request) {
  50.         $response = $this->kernel->handle($request);
  51.         return $response;
  52.     }
  53.  
  54.     function _output($response) {
  55.         $response->send();
  56.     }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement