diff --git a/lib/router.php b/lib/router.php index 273e1ec..825f9d5 100644 --- a/lib/router.php +++ b/lib/router.php @@ -83,8 +83,10 @@ public function getUri() return !strlen($uri) ? '/' : $uri; } - public function run($cb404 = null) + public function run($namespace = null, $cb404 = null) { + $namespace = empty($namespace) ? __NAMESPACE__ : $namespace; + if (empty($cb404)) { $cb404 = Registry::get('cb.404', null); } @@ -127,7 +129,7 @@ public function run($cb404 = null) require_once $controllerFile; - $class = '\\Octo\\App' . ucfirst(Inflector::lower($controllerName)) . 'Controller'; + $class = '\\' . $namespace . '\\App' . ucfirst(Inflector::lower($controllerName)) . 'Controller'; $actions = get_class_methods($class); $father = get_parent_class($class); @@ -178,7 +180,7 @@ public function run($cb404 = null) } if (true === $render) { - self::render($controller, Registry::get('cb.404', null)); + self::render($controller, Registry::get('cb.404', $cb404)); } } } @@ -222,6 +224,8 @@ private static function html($controller) } } + $content = self::compile($content); + $content = str_replace( '$this->partial(\'', '\\Octo\\Router::partial($controller, \'' . path('app') . DS . 'views' . DS . 'partials' . DS, @@ -252,11 +256,26 @@ private static function html($controller) } } + public static function compile($content) + { + $rows = explode('', 'partial(\'' . str_replace('.', DS, $file) . '.phtml\'); ?>', $content); + } + + return $content; + } + public static function partial($controller, $partial, $args = []) { if (File::exists($partial)) { $content = File::read($partial); + $content = self::compile($content); + $content = str_replace( '$this->partial(\'', '\\Octo\\Router::partial($controller, \'' . path('app') . DS . 'views' . DS . 'partials' . DS, diff --git a/lib/routes.php b/lib/routes.php index 0cb42cc..65aba90 100644 --- a/lib/routes.php +++ b/lib/routes.php @@ -23,6 +23,16 @@ public static function __callStatic($m, $a) $uri = array_shift($a); $callback = array_shift($a); + if (!$callback instanceof \Closure) { + $callback = function () use ($callback) { + list($controller, $action, $render) = explode('#', $callback, 3); + + $render = empty($render) ? true : false; + + return [$controller, $action, $render]; + }; + } + $method = Strings::lower($m); $methods = []; @@ -99,8 +109,12 @@ public static function __callStatic($m, $a) $name = str_replace_first("($seg)", $param, $name); } + $name = str_replace('/', '.', $name); + + $name = '.' === $name ? 'home' : $name; + $route = model('uri', [ - 'name' => str_replace('/', '.', $name), + 'name' => $name, 'uri' => $uri, 'url' => $url, 'method' => $method, @@ -146,7 +160,11 @@ public static function getName($url, callable $callback) $name = str_replace_first("($seg)", $param, $name); } - return str_replace('/', '.', $name); + $name = str_replace('/', '.', $name); + + $name = '.' === $name ? 'home' : $name; + + return $name; } public static function getUri($url, callable $callback)