diff options
author | Arnold Daniels <arnold@jasny.net> | 2013-08-09 21:17:15 +0200 |
---|---|---|
committer | Arnold Daniels <arnold@jasny.net> | 2013-08-09 23:11:05 +0200 |
commit | 617fa916ab3df2a91026642806216b6d6874bee5 (patch) | |
tree | bd3e75dba02f20dccd05da72d4fb191273d14ff4 | |
parent | 6ac9ade8337e7d880b74567b8774acf727319f89 (diff) | |
download | router-617fa916ab3df2a91026642806216b6d6874bee5.zip router-617fa916ab3df2a91026642806216b6d6874bee5.tar.gz router-617fa916ab3df2a91026642806216b6d6874bee5.tar.bz2 |
Fixup router
Fixup View
-rw-r--r-- | src/Jasny/Router.php | 49 | ||||
-rw-r--r-- | src/Jasny/View.php | 2 | ||||
-rw-r--r-- | src/Jasny/View/Twig.php | 2 |
3 files changed, 32 insertions, 21 deletions
diff --git a/src/Jasny/Router.php b/src/Jasny/Router.php index 60ec936..5aa49c0 100644 --- a/src/Jasny/Router.php +++ b/src/Jasny/Router.php @@ -9,7 +9,7 @@ namespace Jasny; * ? Single character * # One or more digits (custom extension) * * One or more characters - * ** Any number of characters cross subdirs + * ** Any number of subdirs * [abc] Match character 'a', 'b' or 'c' * [a-z] Match character 'a' to 'z' * {png,gif} Match 'png' or 'gif' @@ -58,6 +58,7 @@ class Router public static function i() { if (!isset(self::$instance)) self::$instance = new static(); + return self::$instance; } @@ -207,16 +208,19 @@ class Router * * @return mixed Whatever the controller returns */ - public static function execute() + public function execute() { $route = $this->getRoute(); + // No route + if (!$route) return $this->notFound(); + // Route to file - if (isset($route['file'])) { - $file = rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/' . ltrim(static::rebase($route['file']), '/'); + if (isset($route->file)) { + $file = rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/' . ltrim(static::rebase($route->file), '/'); if (!file_exists($file)) { - trigger_error("Failed to route using '{$route['route']}': File '$file' doesn't exist." + trigger_error("Failed to route using '{$route->route}': File '$file' doesn't exist." , E_USER_WARNING); return $this->notFound(); } @@ -225,15 +229,17 @@ class Router } // Route to controller - if (empty($route['controller']) || empty($route['action'])) { - trigger_error("Failed to route using '{$route['route']}': " - . (empty($route['controller']) ? 'Controller' : 'Action') . " is not set", E_USER_WARNING); + if (empty($route->controller) || empty($route->action)) { + trigger_error("Failed to route using '{$route->route}': " + . (empty($route->controller) ? 'Controller' : 'Action') . " is not set", E_USER_WARNING); return $this->notFound(); } - $class = $route['controller'] . 'Controller'; - $method = $route['action'] . 'Action'; - $args = isset($route['args']) ? $route['args'] : []; + $class = ucfirst($route->controller) . 'Controller'; + $method = $route->action . 'Action'; + $args = isset($route->args) ? $route->args : []; + + if (!class_exists($class)) return $this->notFound(); $controller = new $class(); if (!is_callable([$controller, $method])) return $this->notFound(); @@ -309,7 +315,7 @@ class Router public static function fnmatch($pattern, $path) { $regex = preg_quote($pattern, '~'); - $regex = strtr($regex, ['\?'=>'[^/]', '\*'=>'[^/]*', '\*\*'=>'.*', '#'=>'\d+', '\['=>'[', '\]'=>']', '\-'=>'-', '\{'=>'{', '\}'=>'}']); + $regex = strtr($regex, ['\?'=>'[^/]', '\*'=>'[^/]*', '/\*\*'=>'(?:/.+)?', '#'=>'\d+', '\['=>'[', '\]'=>']', '\-'=>'-', '\{'=>'{', '\}'=>'}']); $regex = preg_replace_callback('~{[^\}]+}~', function($part) { return '(' . substr(strtr($part[0], ',', '|'), 1, -1) . ')'; }, $regex); $regex = rawurldecode($regex); @@ -317,13 +323,13 @@ class Router } /** - * Fill out the routes variables based on the url parts + * Fill out the routes variables based on the url parts. * - * @param array $vars Route variables - * @param array $parts URL parts + * @param array|object $vars Route variables + * @param array $parts URL parts * @return array */ - protected static function bind(array $vars, array $parts) + protected static function bind($vars, array $parts) { $pos = 0; foreach ($vars as $key=>&$var) { @@ -333,25 +339,30 @@ class Router } $options = explode('|', $var); + $var = null; + foreach ($options as $option) { if ($option[0] === '$') { $i = (int)substr($option, 1); - if ($i > 0) $i++; + if ($i > 0) $i--; + + $slice = array_slice($parts, $i); if (substr($option, -1, 1) != '+') { - $var = array_slice($parts, $i)[0]; + if (!empty($slice)) $var = array_slice($parts, $i)[0]; } elseif (!is_array($vars) || is_string($key)) { trigger_error("Binding multiple parts using \$+, like '$option', " . "are only allow in (numeric) arrays", E_USER_WARING); $var = null; } else { - $slice = array_slice($parts, $i); array_splice($vars, $pos, 1, $slice); $pos += count($slice) - 1; } } else { $var = preg_replace('/^([\'"])(.*)\1$/', '$2', $option); // Unquote } + + if (!empty($var)) break; // continues if option can't be used } $pos++; diff --git a/src/Jasny/View.php b/src/Jasny/View.php index 379fc0c..d35a6ec 100644 --- a/src/Jasny/View.php +++ b/src/Jasny/View.php @@ -59,7 +59,7 @@ abstract class View $class = get_called_class(); $refl = new \ReflectionClass($class); - if ($refl->isAbstract()) $class = (self::$default[0] == '/' ? '' : __CLASS__ . '/') . ucfirst(self::$default); + if ($refl->isAbstract()) $class = (self::$default[0] == '\\' ? '' : __CLASS__ . '\\') . ucfirst(self::$default); return $class; } diff --git a/src/Jasny/View/Twig.php b/src/Jasny/View/Twig.php index 5a0172f..cf76df3 100644 --- a/src/Jasny/View/Twig.php +++ b/src/Jasny/View/Twig.php @@ -65,7 +65,7 @@ class Twig extends \Jasny\View */ public static function init($path, $cache=false) { - if (!isset(self::$default)) self::$default = get_called_class(); + if (!isset(self::$default)) self::$default = '\\' . get_called_class(); $loader = new \Twig_Loader_Filesystem($path); |