Skip to content

Commit

Permalink
routes
Browse files Browse the repository at this point in the history
  • Loading branch information
schpill committed Jul 3, 2017
1 parent f660a4d commit 4a9ffb1
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 30 deletions.
24 changes: 12 additions & 12 deletions lib/checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function fresh()
return $this;
}

public static function required($field, $message = '##field## is required')
public function required($field, $message = '##field## is required')
{
$check = strlen(isAke($this->bag, $field, null)) > 0;

Expand All @@ -31,7 +31,7 @@ public static function required($field, $message = '##field## is required')
return $check;
}

public static function min($field, $min, $message = '##field## is too short')
public function min($field, $min, $message = '##field## is too short')
{
$check = strlen(isAke($this->bag, $field, null)) < $min + 0;

Expand All @@ -42,7 +42,7 @@ public static function min($field, $min, $message = '##field## is too short')
return $check;
}

public static function max($field, $max, $message = '##field## is too long')
public function max($field, $max, $message = '##field## is too long')
{
$check = strlen(isAke($this->bag, $field, null)) > $max + 0;

Expand All @@ -53,7 +53,7 @@ public static function max($field, $max, $message = '##field## is too long')
return $check;
}

public static function fnmatch($field, $fnmatch, $message = '##field## is incorrect')
public function fnmatch($field, $fnmatch, $message = '##field## is incorrect')
{
$check = fnmatch($fnmatch, isAke($this->bag, $field, null));

Expand All @@ -64,7 +64,7 @@ public static function fnmatch($field, $fnmatch, $message = '##field## is incorr
return $check;
}

public static function match($field, $pattern, $message = '##field## is incorrect')
public function match($field, $pattern, $message = '##field## is incorrect')
{
$check = false;

Expand All @@ -79,7 +79,7 @@ public static function match($field, $pattern, $message = '##field## is incorrec
return $check;
}

public static function int($field, $message = '##field## is not an integer')
public function int($field, $message = '##field## is not an integer')
{
$check = reallyInt(isAke($this->bag, $field, null));

Expand All @@ -90,7 +90,7 @@ public static function int($field, $message = '##field## is not an integer')
return $check;
}

public static function float($field, $message = '##field## is not a float')
public function float($field, $message = '##field## is not a float')
{
$check = isAke($this->bag, $field, null) === floatval(isAke($this->bag, $field, null));

Expand All @@ -101,7 +101,7 @@ public static function float($field, $message = '##field## is not a float')
return $check;
}

public static function number($field, $message = '##field## is not a number')
public function number($field, $message = '##field## is not a number')
{
$check = isAke($this->bag, $field, null) === isAke($this->bag, $field, null) + 0;

Expand All @@ -112,7 +112,7 @@ public static function number($field, $message = '##field## is not a number')
return $check;
}

public static function numeric($field, $message = '##field## is not numeric')
public function numeric($field, $message = '##field## is not numeric')
{
$check = is_numeric(isAke($this->bag, $field, null));

Expand All @@ -123,7 +123,7 @@ public static function numeric($field, $message = '##field## is not numeric')
return $check;
}

public static function email($field, $message = '##field## is not an email')
public function email($field, $message = '##field## is not an email')
{
$check = filter_var(isAke($this->bag, $field, null), FILTER_VALIDATE_EMAIL);

Expand All @@ -134,7 +134,7 @@ public static function email($field, $message = '##field## is not an email')
return $check;
}

public static function bool($field, $message = '##field## is not a boolean')
public function bool($field, $message = '##field## is not a boolean')
{
$check = isAke($this->bag, $field, null) === (bool) isAke($this->bag, $field, null);

Expand All @@ -145,7 +145,7 @@ public static function bool($field, $message = '##field## is not a boolean')
return $check;
}

public static function custom($field, callable $custom, $message = '##field## is incorrect')
public function custom($field, callable $custom, $message = '##field## is incorrect')
{
$check = call($custom, [isAke($this->bag, $field, null)]);

Expand Down
24 changes: 12 additions & 12 deletions lib/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1920,6 +1920,9 @@ function systemBoot($dir = null)
}
}

Registry::set('core.routes.prefix', '');
Registry::set('core.routes.before', null);

Registry::set('octo.subdir', $subdir);

if (!defined('OCTO_STANDALONE')) {
Expand Down Expand Up @@ -6236,6 +6239,15 @@ function evaluate($path, $args = [])
return ltrim(ob_get_clean());
}

function compactCallback()
{
$args = func_get_args();

return function () use ($args) {
return $args;
};
}

function auth($em = 'user')
{
return guard($em);
Expand Down Expand Up @@ -6574,15 +6586,3 @@ public function __call($m, $a)
throw new \BadMethodCallException("Method {$m} does not exist.");
}
}

function inner()
{
$args = func_get_args();

if (class_exists($args[0])) {
$args = array_shift($args);
$args = array_shift($args);

return call_user_func_array($args[0], $args[1]);
}
}
2 changes: 1 addition & 1 deletion lib/octalia.php
Original file line number Diff line number Diff line change
Expand Up @@ -3179,7 +3179,7 @@ private function intersect($tab1, $tab2)
return $collection;
}

public function has(Octalia $query)
public function has($query)
{
$ids = [];

Expand Down
6 changes: 6 additions & 0 deletions lib/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,12 @@ public function handling($routes, $quit = true)
if ($this->uri) {
route($route);

if ($before = $this->uri->getBefore()) {
if (is_callable($before)) {
$before();
}
}

if ($middleware = $this->uri->getMiddleware()) {
if (!is_array($middleware)) {
$middleware = [$middleware];
Expand Down
50 changes: 45 additions & 5 deletions lib/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,42 @@ public static function json(array $array)
return toClosure($array);
}

public static function prefix($prefix, callable $next)
{
Registry::set('core.routes.prefix', $prefix);

$next();

Registry::set('core.routes.prefix', '');
}

public static function before($before, callable $next)
{
if (is_string($before)) {
$class = maker($class);

$before = [$class, 'handle'];
}

if (is_callable($before)) {
Registry::set('core.routes.before', $before);

$next();

Registry::set('core.routes.before', null);
}
}

public static function __callStatic($m, $a)
{
if ('array' == $m) {
return toClosure($a);
}

$uri = array_shift($a);
$before = Registry::get('core.routes.before', null);
$prefix = Registry::get('core.routes.prefix', '');
$prefix = strlen($prefix) ? trim($prefix, '/') . '/' : $prefix;
$uri = $prefix . array_shift($a);
$callback = array_shift($a);

if (!$callback instanceof \Closure) {
Expand Down Expand Up @@ -193,9 +222,10 @@ public static function __callStatic($m, $a)

$render = empty($render);

$callback = function () use ($controller, $action, $render) {
return [$controller, $action, $render];
};
$callback = compactCallback($controller, $action, $render);
// $callback = function () use ($controller, $action, $render) {
// return [$controller, $action, $render];
// };
}

$method = Strings::lower($m);
Expand Down Expand Up @@ -252,7 +282,13 @@ public static function __callStatic($m, $a)
foreach ($methods as $meth) {
if (!isset(static::$routes[$meth])) static::$routes[$meth] = [];

array_unshift(static::$routes[$meth], ['uri' => $uri, 'callback' => $callback]);
$exists = coll(static::$routes[$meth])->where('uri', $uri)->count() > 0;

if (!$exists) {
array_unshift(static::$routes[$meth], ['uri' => $uri, 'callback' => $callback]);
} else {
exception('Routes', "The route with uri $uri ever exists for " . Strings::upper($meth) ." method.");
}
}

$reflection = reflectClosure($callback);
Expand Down Expand Up @@ -292,6 +328,10 @@ public static function __callStatic($m, $a)

$route = model('uri', $dataRoute);

if (is_callable($before)) {
$route->setBefore($before);
}

$route->macro('as', function ($name) use ($route) {
return $route->setName($name);
});
Expand Down

0 comments on commit 4a9ffb1

Please sign in to comment.