コード例 #1
0
 /**
  * Matching the current route to the defined custom routes
  * @return string|boolean The matched route or false if no matched route is found
  */
 public static function match()
 {
     $realPath = explode('/', route_path());
     $routes = self::$routes;
     if (!(is_array($routes) && count($routes))) {
         return false;
     }
     $found = false;
     foreach ($routes as $key => $value) {
         $patternPath = explode('/', trim($value['path'], '/'));
         if (count($realPath) !== count($patternPath)) {
             continue;
         }
         $vars = array();
         $matchedPath = array();
         foreach ($patternPath as $i => $segment) {
             if ($segment === $realPath[$i]) {
                 $matchedPath[$i] = $segment;
                 continue;
             } else {
                 if (preg_match('/([a-z0-9\\-_\\.]*)?{([a-z0-9\\_]+)}([a-z0-9\\-_\\.]*)?/i', $segment, $matches)) {
                     $name = $matches[2];
                     $var = $realPath[$i];
                     if ($matches[1]) {
                         $var = ltrim($var, $matches[1] . '{');
                     }
                     if ($matches[3]) {
                         $var = rtrim($var, '}' . $matches[3]);
                     }
                     if (isset($value['patterns'][$name]) && $value['patterns'][$name]) {
                         $regex = $value['patterns'][$name];
                         if (!preg_match('/^' . $regex . '$/', $var)) {
                             throw new \InvalidArgumentException(sprintf('The Router does not satify the argument value "%s" for "%s".', $var, $regex));
                         }
                     }
                     $vars[$name] = $var;
                     $matchedPath[$i] = $realPath[$i];
                     continue;
                 }
                 break;
             }
         }
         if (route_path() === implode('/', $matchedPath)) {
             if (!in_array($_SERVER['REQUEST_METHOD'], $value['method'])) {
                 throw new \RuntimeException(sprintf('The Router does not allow the method "%s" for "%s".', $_SERVER['REQUEST_METHOD'], $key));
             }
             $found = true;
             break;
         }
     }
     if ($found) {
         $toRoute = trim($value['to'], '/');
         $_GET[ROUTE] = $toRoute;
         $_GET = array_merge($_GET, $vars);
         return $toRoute;
     }
     return false;
 }
コード例 #2
0
/**
 * @internal
 *
 * Route to a page according to request
 * interally called by /app/index.php
 *
 * @return string
 */
function router()
{
    global $lc_homeRouting;
    # Get a route from the defined custom routes (if any)
    $_page = route_match();
    if ($_page) {
        return route_getAbsolutePathToRoot($_page);
    }
    $q = route_path();
    # if the route is empty, get it from the config
    if (empty($q) && $lc_homeRouting) {
        $q = $lc_homeRouting;
    }
    # if it is still empty, set it to the system default
    if (empty($q)) {
        $q = 'home';
    }
    # Get the complete path to root
    $_page = route_getAbsolutePathToRoot($q);
    if (!empty($_page) && file_exists($_page)) {
        return $_page;
    }
    if (preg_match('/(.*)(401|403|404) {1}$/', $_page, $matches)) {
        return _i('inc/tpl/' . $matches[2] . '.php');
    }
    # Search the physical directory according to the routing path
    $_page = route_search();
    if ($_page && is_file($_page) && file_exists($_page)) {
        return $_page;
    }
    if (in_array(_arg(0), array('401', '403'))) {
        return _i('inc/tpl/' . _arg(0) . '.php');
    } else {
        return _i('inc/tpl/404.php');
    }
}
コード例 #3
0
/**
 * Return a component of the current path.
 * When viewing a page http://www.example.com/foo/bar and its path would be "foo/bar",
 * for example, arg(0) returns "foo" and arg(1) returns "bar"
 *
 * @param mixed $index
 *  The index of the component, where each component is separated by a '/'
 *  (forward-slash), and where the first component has an index of 0 (zero).
 * @param string $path
 *  A path to break into components. Defaults to the path of the current page.
 *
 * @return mixed
 *  The component specified by `$index`, or `null` if the specified component was not found.
 *  If called without arguments, it returns an array containing all the components of the current path.
 */
function _arg($index = null, $path = null)
{
    if (isset($_GET[$index])) {
        return _get($_GET[$index]);
    }
    if (is_null($path)) {
        $path = route_path();
    }
    $arguments = explode('/', $path);
    if (is_numeric($index)) {
        if (!isset($index)) {
            return $arguments;
        }
        if (isset($arguments[$index])) {
            return strip_tags(trim($arguments[$index]));
        }
    } elseif (is_string($index)) {
        $query = '-' . $index . '/';
        $pos = strpos($path, $query);
        if ($pos !== false) {
            $start = $pos + strlen($query);
            $path = substr($path, $start);
            $end = strpos($path, '/-');
            if ($end) {
                $path = substr($path, 0, $end);
            }
            if (substr_count($path, '/')) {
                return explode('/', $path);
            } else {
                return $path;
            }
        }
    } elseif (is_null($index)) {
        return explode('/', str_replace('/-', '/', $path));
    }
    return '';
}