Skip to content

Commit

Permalink
update routing
Browse files Browse the repository at this point in the history
  • Loading branch information
schpill committed Sep 15, 2016
1 parent 3e524c6 commit 6a81556
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
25 changes: 22 additions & 3 deletions lib/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -252,11 +256,26 @@ private static function html($controller)
}
}

public static function compile($content)
{
$rows = explode('<partial file=', $content);
array_shift($rows);

foreach ($rows as $row) {
$file = cut('"', '"', $row);
$content = str_replace('<partial file="' . $file . '">', '<?php $this->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,
Expand Down
22 changes: 20 additions & 2 deletions lib/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6a81556

Please sign in to comment.