예제 #1
0
 /**
  * @param Route $route
  * @return $this
  */
 public function assignRoute(Route $route)
 {
     if (isset($this->_routes[$route->getRouteType()])) {
         if (isset($this->_routes[$route->getRouteType()][$route->getPath()])) {
             throw new \Exception("assigned route path was assigned before");
         }
     }
     // $this->_concatPath($this->_path, $route->getPath()) . PHP_EOL;
     $this->_routes[$route->getRouteType()][$route->getPath()] = $route;
     return $this;
 }
예제 #2
0
 /**
  * {@inheritDoc}
  *
  * @throws RuntimeException
  */
 protected function compileRoute(Route $route)
 {
     $parameters = $this->processPathParameters($route->getPath());
     $regexp = $route->getPath();
     foreach ($parameters as $parameter) {
         if (substr_count($route->getPath(), '{' . $parameter . '}') > 1) {
             throw new \RuntimeException(sprintf('Route name "%s" cannot reference variable "%s" more than once.', $route->getName(), $parameter));
         }
         $regexp = str_replace('{' . $parameter . '}', '(?<' . $parameter . '>[A-z0-9_-]+)', $regexp);
     }
     $regexp = "#^{$regexp}\$#";
     return ['parameters' => $parameters, 'regexp' => $regexp];
 }
예제 #3
0
 public function testGetRoutPath()
 {
     $route = new Route();
     $test_path = '/a/path';
     $route->setPath($test_path);
     $this->assertSame($test_path, $route->getPath(), 'Path not set correctly');
 }
 /**
  * Returns the route path containing the aliases definitions if any given.
  * @param \Brickoo\Component\Routing\Route\Route $route
  * @return string the modified route path containing the aliases
  */
 private function getRoutePath(Route $route)
 {
     $routePath = $route->getPath();
     foreach ($this->aliases as $routeKey => $routeAlias) {
         if (strpos($routePath, $routeKey) !== false) {
             $replacement = sprintf("(%s|%s)", $routeKey, preg_quote($routeAlias, "~"));
             $routePath = str_replace($routeKey, $replacement, $routePath);
             break;
         }
     }
     return $routePath;
 }
예제 #5
0
 /**
  * @param Route $route
  * @return string
  */
 public function makeRegExp(Route $route)
 {
     $regexp = $route->getPath();
     $restrictions = $route->getRestrictions();
     if (preg_match_all('~{(.*)}~Uu', $regexp, $placeholders)) {
         foreach ($placeholders[0] as $index => $match) {
             $name = $placeholders[1][$index];
             $replace = array_key_exists($name, $restrictions) ? $restrictions[$name] : '.*';
             $replace = '(?<' . $name . '>' . $replace . ')';
             $regexp = str_replace($match, $replace, $regexp);
         }
     }
     return $regexp;
 }
예제 #6
0
 public static function fetchPlaceholder(Route $route, $path)
 {
     $origin = $route->getOrigin();
     $params = self::extractPlaceholder($origin);
     $segments = explode('/', $path);
     $numeric = $params ? array_slice($segments, (int) min($params)) : [];
     foreach ($params as $name => $index) {
         if (isset($segments[$index])) {
             $params[$name] = $segments[$index];
         } else {
             $params[$name] = null;
         }
     }
     $wildcard = $route->getWildcard();
     if (null !== $wildcard) {
         $slice = substr($path, strlen($route->getPath()));
         $slice = array_values(array_filter(explode('/', $slice)));
         $params[$wildcard] = $slice;
     }
     $route->setParams($params + $numeric);
 }
예제 #7
0
파일: Router.php 프로젝트: mheydt/scalr
 /**
  * Maps the route into routing table
  *
  * @param    Route   $route  A route instance
  * @return   Router  Returns current route instance
  */
 public function map(Route $route)
 {
     list($path, $groupMiddleware) = $this->processed;
     if ($path != '') {
         $route->setPath($path . $route->getPath());
     }
     if (!empty($groupMiddleware)) {
         $route->addMiddleware($groupMiddleware);
     }
     //Appends route to routing table.
     //We are using Trie search data structure.
     $this->routingTable->appendRoute($route);
     if ($route->getName() !== null) {
         //This is the named route
         if (isset($this->namedRoutes[$route->getName()])) {
             throw new \RuntimeException(sprintf("The Route with the name '%s' alredy exists", $route->getName()));
         } else {
             $this->namedRoutes[$route->getName()] = $route;
         }
     }
     return $this;
 }
예제 #8
0
 static function load($app_name)
 {
     $apps = self::getAppList();
     if (!isset($apps[$app_name])) {
         return;
     }
     $app_dir = $apps[$app_name];
     self::$activeAppName = strtolower($app_name);
     self::$activeAppDir = $app_dir;
     $route_file_name = String::glue($app_dir, DS, APP_CONFIG_DIR, DS, 'route.php');
     if (php_sapi_name() != 'cli') {
         try {
             Route::setActive(Route::getPath(), file_exists($route_file_name) ? include $route_file_name : array(), true);
         } catch (Exception $e) {
             self::parseError($e, $app_name);
         }
     }
     self::setDefaultParams();
     // Abort this code, but later
     if (!Auth::loged() && Core::isSecure(Request::get('module')) && Request::get('module') != 'login' && false) {
         if (php_sapi_name() == 'cli') {
             echo 'Auth Error';
             exit;
         }
         Request::redirect('/login/' . (Request::get(Route::$path) ? '&return=' . Request::get(Route::$path) : ''));
     }
     //
     Settings::load('core', CORE_CONFIG_DIR . DS);
     self::loadDependences($app_dir);
     Settings::load($app_name);
     App::startApp($app_name);
     try {
         Load::Action($app_dir . DS, Request::get('action'));
     } catch (Exception $e) {
         self::parseError($e, $app_name);
     }
     App::endApp($app_name);
     Language::translate(Language::getActiveLanguageISO());
 }
예제 #9
0
 /**
  * Evaluates all routes from the controller. The first found
  * route is executed. If this route does *not* return false,
  * the script is exited.
  * @return void
  */
 public function route()
 {
     foreach ($this->routes as $preg => $method) {
         $matches = null;
         if (preg_match($preg, Route::getPath(), $matches)) {
             $result = null;
             if (is_array($method)) {
                 $requestType = strtolower($_SERVER['REQUEST_METHOD']);
                 if (array_key_exists($requestType, $method)) {
                     $func = $method[$requestType];
                     if (is_callable($func)) {
                         $result = $func($matches);
                     } else {
                         $result = $this->{$func}($matches);
                     }
                 }
             } elseif (is_callable($method)) {
                 $result = $method($matches);
             } else {
                 $result = $this->{$method}($matches);
             }
             if ($result !== false) {
                 exit;
             }
         }
     }
 }
예제 #10
0
 /**
  *
  */
 private function addStaticRoute($routeData, Route $route)
 {
     $handler = $route->getHandler();
     $routeStr = $routeData[0];
     foreach ($route->getMethod() as $method) {
         if (isset($this->staticRoutes[$method][$routeStr])) {
             throw new Exception("Cannot register two routes matching \"{$routeStr}\" for method \"{$method}\"");
         }
         if (isset($this->dynamicRoutes[$method])) {
             foreach ($this->dynamicRoutes[$method] as $route) {
                 if ($route->matches($routeStr)) {
                     throw new Exception("Static route \"{$routeStr}\" is shadowed by previously defined variable route \"{$route->getPath()}\" for method \"{$method}\"");
                 }
             }
         }
         $this->staticRoutes[$method][$routeStr] = $handler;
     }
 }
예제 #11
0
 public function testGetPath()
 {
     $this->assertSame('/foo', $this->route->getPath());
 }
예제 #12
0
 /**
  * Pull the variables out of a path given the actual path and the
  * path that is is representing.
  * @param string $pathToMatch The path that is being matched.
  * @param Route $route The route to use.
  * @return array
  */
 private static function pullVariables($pathToMatch, Route $route)
 {
     $rtn = array();
     $pathToMatch = self::fixPath($pathToMatch);
     $nameMatches = array();
     $valueMatches = array();
     preg_match_all(self::IDENT_PREG, $route->getPath(), $nameMatches, PREG_PATTERN_ORDER);
     preg_match_all($route->getRegexPath(), $pathToMatch, $valueMatches, PREG_PATTERN_ORDER);
     for ($i = 0; $i < count($nameMatches[1]); $i++) {
         $value = $valueMatches[$i + 1][0];
         $rtn = array_merge($rtn, array($nameMatches[1][$i] => $value));
     }
     return $rtn;
 }
예제 #13
0
 /**
  * @param Route $route Route object matching rules
  */
 protected function setGetData($route)
 {
     $routePath = str_replace(array('(', ')'), array('', ''), $route->getPath());
     $trim = explode('<', $routePath);
     $parsed_url = str_replace(array($this->basePath), array(''), $this->url);
     $parsed_url = preg_replace("#{$trim['0']}#", '', $parsed_url, 1);
     // sets the parameters passed in the URL
     foreach ($route->getParams() as $key => $param) {
         preg_match("#{$param}#", $parsed_url, $results);
         if (isset($results[0])) {
             $_GET[$key] = $results[0];
             $parsed_url = str_replace($results[0], '', $parsed_url);
         }
     }
     // if no parameter in the URL, it sets the default values from the table
     foreach ($route->getDefaults() as $key => $default) {
         if (!isset($_GET[$key])) {
             $_GET[$key] = $default;
         }
     }
 }
예제 #14
0
파일: Table.php 프로젝트: mheydt/scalr
 /**
  * Appends route to routing table
  *
  * @param    Route   $route     The route object
  * @param    array   $parts     optional The list of the parts in the route path
  * @return   Item    Returns the terminal item which contains the route
  */
 public function appendRoute(Route $route, array &$parts = null)
 {
     if ($parts === null) {
         $parts = explode('/', trim($route->getPath(), '/'));
     }
     $part = array_shift($parts);
     $pathPart = new PathPart($part);
     if (strpos($part, ':') !== false) {
         //This is regexp part
         $pathPart->type = PathPart::TYPE_REGEXP;
         $pathPart->value = '~^' . preg_replace_callback('~:([\\w]+)\\+?~', function ($m) use($route, $pathPart) {
             $requirements = $route->getRequirements();
             if (isset($requirements[$m[1]])) {
                 return '(?<' . $m[1] . '>' . $requirements[$m[1]] . ')';
             }
             if (substr($m[0], -1) === '+') {
                 $pathPart->type = PathPart::TYPE_REGEXP_PATH;
                 return '(?<' . $m[1] . '>.+)';
             }
             return '(?<' . $m[1] . '>[^/]+)';
         }, str_replace(')', ')?', $part)) . '$~';
     }
     if (!$this->offsetExists($pathPart->value)) {
         /* @var $item Item */
         $item = new Item($pathPart);
         $this[$pathPart->value] = $item;
     } else {
         $item = $this[$pathPart->value];
     }
     if (empty($parts)) {
         //This is the terminal item
         if (!in_array($route, $item->routes)) {
             $item->routes[] = $route;
         }
         return $item;
     }
     return $item->getTable()->appendRoute($route, $parts);
 }
예제 #15
0
 public function testSetPath()
 {
     $route = new Route("/");
     $route->setPath("/home");
     $this->assertEquals("/home", $route->getPath());
 }
예제 #16
0
 /**
  * @test
  */
 public function pathGetsReturnedAsIs()
 {
     $route = new Route('get', '/info', 'test');
     $this->assertSame('/info', $route->getPath());
 }