diff --git a/lib/checker.php b/lib/checker.php index b2186be..935dead 100644 --- a/lib/checker.php +++ b/lib/checker.php @@ -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; @@ -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; @@ -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; @@ -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)); @@ -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; @@ -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)); @@ -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)); @@ -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; @@ -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)); @@ -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); @@ -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); @@ -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)]); diff --git a/lib/lib.php b/lib/lib.php index 83ca6a6..717bbe4 100755 --- a/lib/lib.php +++ b/lib/lib.php @@ -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')) { @@ -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); @@ -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]); - } - } diff --git a/lib/octalia.php b/lib/octalia.php index 7622800..10b53b0 100644 --- a/lib/octalia.php +++ b/lib/octalia.php @@ -3179,7 +3179,7 @@ private function intersect($tab1, $tab2) return $collection; } - public function has(Octalia $query) + public function has($query) { $ids = []; diff --git a/lib/router.php b/lib/router.php index 676365e..61a1d12 100644 --- a/lib/router.php +++ b/lib/router.php @@ -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]; diff --git a/lib/routes.php b/lib/routes.php index c2e634f..0ded18f 100644 --- a/lib/routes.php +++ b/lib/routes.php @@ -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) { @@ -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); @@ -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); @@ -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); });