Skip to content

Commit

Permalink
routing
Browse files Browse the repository at this point in the history
  • Loading branch information
schpill committed Jul 27, 2017
1 parent 6d17938 commit 708f733
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 30 deletions.
5 changes: 5 additions & 0 deletions lib/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/octalia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions lib/octaliaiterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down
24 changes: 21 additions & 3 deletions lib/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -845,7 +858,7 @@ protected function groupByClause()
return '';
}

return ' GROUP BY '.implode(' , ', $this->groups);
return ' GROUP BY ' . implode(' , ', $this->groups);
}

public function groupBy($columns)
Expand Down Expand Up @@ -904,6 +917,7 @@ protected function offsetClause()

return ' OFFSET ' . $this->offset;
}

public function latest($column = 'created_at')
{
return $this->orderBy($column, 'DESC');
Expand Down Expand Up @@ -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);
}
Expand Down
62 changes: 42 additions & 20 deletions lib/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -351,30 +356,35 @@ 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);

if (!in_array($action, $actions)) {
return $this->is404();
}

$controller = maker($class, [], false);
$controller = foundry($class);
actual('controller', $controller);

$callable = [$controller, $action];

Expand Down Expand Up @@ -416,6 +426,7 @@ private function params($route, $params)
$p++;
}


$this->controllerBoot($controller);

$return = call($callable, $params);
Expand Down Expand Up @@ -444,6 +455,8 @@ private function params($route, $params)
null
)
);
} else {
return item()->render(false);
}
} else {
return null;
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 12 additions & 2 deletions lib/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

/*
Expand Down Expand Up @@ -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'];
});

/*
Expand Down Expand Up @@ -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;

Expand Down
39 changes: 39 additions & 0 deletions tests/RoutesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
use Octo\Route;
use Octo\ControllerBase;
use function Octo\context;

class DummyController extends ControllerBase
{
public function getIndex()
{
context('app')['test'] += 1;
}

public function getTest($add)
{
context('app')['test'] += $add;
}
}

class RoutesTest extends TestCase
{
/** @test */
public function withArray()
{
Route::get('/', [DummyController::class, 'index', false]);
Route::get('add/(.*)', [DummyController::class, 'test', false]);

$_SERVER['SERVER_PROTOCOL'] = 'HTTP';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/';

$this->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']);
}
}

0 comments on commit 708f733

Please sign in to comment.