From 708f7337dc0cc4ef39d60e0c07fb15098650e086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Plusquellec?= Date: Thu, 27 Jul 2017 17:11:41 +0200 Subject: [PATCH] routing --- lib/lib.php | 5 ++++ lib/octalia.php | 2 +- lib/octaliaiterator.php | 8 +++--- lib/orm.php | 24 ++++++++++++++-- lib/router.php | 62 ++++++++++++++++++++++++++++------------- lib/routes.php | 14 ++++++++-- tests/RoutesTest.php | 39 ++++++++++++++++++++++++++ 7 files changed, 124 insertions(+), 30 deletions(-) create mode 100644 tests/RoutesTest.php diff --git a/lib/lib.php b/lib/lib.php index 9dea476..64ae875 100755 --- a/lib/lib.php +++ b/lib/lib.php @@ -815,6 +815,11 @@ function item($attributes = []) return lib('fluent', [$attributes]); } + function q($attributes = []) + { + return item($attributes); + } + function record($attributes = [], $entity) { $attributes = arrayable($attributes) ? $attributes->toArray() : $attributes; diff --git a/lib/octalia.php b/lib/octalia.php index 2fd368e..2d1d5c2 100644 --- a/lib/octalia.php +++ b/lib/octalia.php @@ -2455,7 +2455,7 @@ public function unique($conditions) function search($conditions) { - $conditions = is_object($conditions) ? $conditions->toArray() : $conditions; + $conditions = arrayable($conditions) ? $conditions->toArray() : $conditions; foreach ($conditions as $field => $value) { $this->where($field, $value); diff --git a/lib/octaliaiterator.php b/lib/octaliaiterator.php index 58b9fc6..c9d0605 100644 --- a/lib/octaliaiterator.php +++ b/lib/octaliaiterator.php @@ -598,8 +598,8 @@ public function export($type = 'xls') $xls = str_replace(['##head##', '##body##'], [$head, $body], $xls); - header ("Content-type: application/excel"); - header ('Content-disposition: attachement; filename="Export.xls"'); + header("Content-type: application/excel"); + header('Content-disposition: attachement; filename="Export.xls"'); header("Content-Transfer-Encoding: binary"); header("Expires: 0"); header("Cache-Control: no-cache, must-revalidate"); @@ -611,8 +611,8 @@ public function export($type = 'xls') } elseif ($type == 'php') { $content = var_export($rows, true); - header ("Content-type: application/php"); - header ('Content-disposition: attachement; filename="Export.php"'); + header("Content-type: application/php"); + header('Content-disposition: attachement; filename="Export.php"'); header("Content-Transfer-Encoding: binary"); header("Expires: 0"); header("Cache-Control: no-cache, must-revalidate"); diff --git a/lib/orm.php b/lib/orm.php index 470e81e..4e77071 100644 --- a/lib/orm.php +++ b/lib/orm.php @@ -436,7 +436,20 @@ public function orIsNotNull($key) return $this->_whereNull($key, 'OR', true); } - public function many($conditions) + public function __invoke($concern = null) + { + if ($concern) { + if (reallyInt($concern)) { + return $this->getEntity()->find((int) $concern); + } else { + return $this->query($concern); + } + } + + return get_class($this->getEntity()); + } + + public function query($conditions) { $conditions = arrayable($conditions) ? $conditions->toArray() : $conditions; @@ -845,7 +858,7 @@ protected function groupByClause() return ''; } - return ' GROUP BY '.implode(' , ', $this->groups); + return ' GROUP BY ' . implode(' , ', $this->groups); } public function groupBy($columns) @@ -904,6 +917,7 @@ protected function offsetClause() return ' OFFSET ' . $this->offset; } + public function latest($column = 'created_at') { return $this->orderBy($column, 'DESC'); @@ -955,8 +969,12 @@ protected function isPaired(array $array) return array_keys($array) !== range(0, count($array) - 1); } - public function findBy($field, $value) + public function findBy($field, $value = null) { + if (is_null($value)) { + return $this->query($field); + } + if (is_array($value)) { return $this->in($field, $value); } diff --git a/lib/router.php b/lib/router.php index 54d5736..6c066ad 100644 --- a/lib/router.php +++ b/lib/router.php @@ -125,15 +125,21 @@ public function run($namespace = null, $cb404 = null) list($controllerName, $action, $render) = $this->route; - $controllerFile = path('app') . DS . 'controllers' . DS . $controllerName . '.php'; + if (!class_exists($controllerName)) { + $controllerFile = path('app') . DS . 'controllers' . DS . $controllerName . '.php'; - if (!is_file($controllerFile)) { - return $this->is404($cb404); - } + if (!is_file($controllerFile)) { + return $this->is404($cb404); + } - require_once $controllerFile; + require_once $controllerFile; - $class = '\\' . $namespace . '\\App' . ucfirst(Inflector::lower($controllerName)) . 'Controller'; + $class = '\\' . $namespace . '\\App' . ucfirst(Inflector::lower($controllerName)) . 'Controller'; + + actual('controller.file', $controllerFile); + } else { + $class = $controllerName; + } $actions = get_class_methods($class); $father = get_parent_class($class); @@ -148,12 +154,11 @@ public function run($namespace = null, $cb404 = null) ) ); - $controller = maker($class, [], false); + $controller = foundry($class); $controller->_name = $controllerName; $controller->action = $a; actual('controller', $controller); - actual('controller.file', $controllerFile); $this->controllerBoot($controller); @@ -351,22 +356,26 @@ private function params($route, $params) list($controllerName, $action, $render) = $route['callback'](); - $controllerFile = path('app') . DS . 'controllers' . DS . $controllerName . '.php'; - - if (!is_file($controllerFile)) { - return $this->is404(); - } - $method = $this->getMethod(); $action = lcfirst(Str::camelize(Str::lower($method) . '_' . $action)); - $code = File::read($controllerFile); - $ns = cut('namespace ', ';', $code); + if (!class_exists($controllerName)) { + $controllerFile = path('app') . DS . 'controllers' . DS . $controllerName . '.php'; + + if (!is_file($controllerFile)) { + return $this->is404(); + } - require_once $controllerFile; + $code = File::read($controllerFile); + $ns = cut('namespace ', ';', $code); - $class = '\\' . $ns . '\\App' . ucfirst(Str::lower($controllerName)) . 'Controller'; + require_once $controllerFile; + + $class = '\\' . $ns . '\\App' . ucfirst(Str::lower($controllerName)) . 'Controller'; + } else { + $class = $controllerName; + } $actions = get_class_methods($class); @@ -374,7 +383,8 @@ private function params($route, $params) return $this->is404(); } - $controller = maker($class, [], false); + $controller = foundry($class); + actual('controller', $controller); $callable = [$controller, $action]; @@ -416,6 +426,7 @@ private function params($route, $params) $p++; } + $this->controllerBoot($controller); $return = call($callable, $params); @@ -444,6 +455,8 @@ private function params($route, $params) null ) ); + } else { + return item()->render(false); } } else { return null; @@ -533,7 +546,16 @@ public function handling($routes, $quit = true) } if (!empty($params)) { - $this->params($route, $params); + $status = $this->params($route, $params); + + if (arrayable($status)) { + if (!$status->render) { + $this->route = $status; + $found++; + + break; + } + } } if ($quit) { diff --git a/lib/routes.php b/lib/routes.php index 5ede735..06d7c2b 100644 --- a/lib/routes.php +++ b/lib/routes.php @@ -102,7 +102,7 @@ public static function resource($model, $controller = null) | Create |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - self::get($prefix . '/add', $controller . '#add'); + self::get($prefix . '/create', $controller . '#create'); self::post($prefix . '/store', $controller . '#store'); /* @@ -141,7 +141,7 @@ public static function resource($model, $controller = null) self::getPost($prefix . '/delete/([0-9]+)', function ($id) use ($controller) { $_REQUEST['id'] = $id; - return [$controller, 'delete']; + return [$controller, 'destroy']; }); /* @@ -212,6 +212,16 @@ public static function __callStatic($m, $a) $uri = $prefix . array_shift($a); $callback = array_shift($a); + if (is_array($callback)) { + if (2 == count($callback)) { + $callback[] = true; + } + + list($controller, $action, $render) = $callback; + + $callback = compactCallback($controller, $action, $render); + } + if (!$callback instanceof \Closure) { $render = null; diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php new file mode 100644 index 0000000..2884818 --- /dev/null +++ b/tests/RoutesTest.php @@ -0,0 +1,39 @@ +foundry('router')->setBaseRoute('')->run(); + $this->assertEquals(1, context('app')['test']); + + $_SERVER['REQUEST_URI'] = '/add/15'; + + $this->foundry('router')->setBaseRoute('')->run(); + $this->assertEquals(16, context('app')['test']); + } + }