EclipseGc

DrupalRoutes.php

Feb 3rd, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.71 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\Core\Routes;
  4.  
  5. use Symfony\Component\Routing;
  6. use Symfony\Component\Routing\RouteCollection;
  7. use Symfony\Component\Routing\Route;
  8.  
  9. class DrupalRoutes {
  10.   protected $routes;
  11.   static protected $shared;
  12.  
  13.   public function __construct() {
  14.     $this->routes = array();
  15.     foreach (module_implements('menu') as $module) {
  16.       $this->routes[$module] = module_invoke($module, 'menu');
  17.     }
  18.   }
  19.  
  20.   public function getRoutes() {
  21.     if (isset(self::$shared['routes'])) {
  22.       return self::$shared['routes'];
  23.     }
  24.     $routes = new RouteCollection();
  25.     foreach ($this->routes as $module => $items) {
  26.       foreach ($items as $path => $item) {
  27.         $name = str_replace(array('/', '%', '-'), array('.', '_', ''), $path);
  28.         $path_parts = explode('/', $path);
  29.         $arguments = array(
  30.           'arguments' => array(),
  31.         );
  32.         foreach ($path_parts as $key => $part) {
  33.           if (substr($part, 0, 1) == '%') {
  34.             $arguments['arguments'] += array($key => substr($part, 1));
  35.             $path_parts[$key] = '{' . substr($part, 1) . '}';
  36.           }
  37.         }
  38.         $path = implode('/', $path_parts);
  39.         if (isset($item['page callback'])) {
  40.           $item += array(
  41.             'page arguments' => array(),
  42.             'access callback' => 'user_access',
  43.             'access arguments' => array(),
  44.           );
  45.           if (isset($item['file']) && !isset($item['file path'])) {
  46.             $item['file path'] = drupal_get_path('module', $module);
  47.           }
  48.           $arguments['menu item'] = $item;
  49.           $routes->add($name, new Route($path, $arguments));
  50.         }
  51.       }
  52.     }
  53.     return self::$shared['routes'] = $routes;
  54.   }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment