aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilly Micieli <micieli@laposte.net>2019-10-25 11:34:00 +0200
committerWilly Micieli <micieli@laposte.net>2019-10-25 11:34:00 +0200
commitb3a195e6d49c1c3af3b64a6dabeedfc62263a21e (patch)
tree1019321fd49fa0d20c82ff81f058f58cdbc76fe5
parent4cfad712a75e6573ea5a2e63ef8a2f5f0c0667d2 (diff)
downloadimperium-b3a195e6d49c1c3af3b64a6dabeedfc62263a21e.zip
imperium-b3a195e6d49c1c3af3b64a6dabeedfc62263a21e.tar.gz
clean code
-rw-r--r--helpers/admin.php30
-rw-r--r--helpers/web.php90
-rw-r--r--imperium/Action/Crud.php12
-rw-r--r--imperium/App.php25
-rw-r--r--imperium/Html/Form/Form.php24
-rw-r--r--imperium/Management.php33
-rw-r--r--imperium/Routing/Router.php2
-rw-r--r--imperium/View/View.php4
-rw-r--r--tests/AppTest.php2
-rw-r--r--tests/Helpers/AdminTest.php2
-rw-r--r--tests/Helpers/WebTest.php4
-rw-r--r--tests/RouterTest.php4
12 files changed, 114 insertions, 118 deletions
diff --git a/helpers/admin.php b/helpers/admin.php
index cc57310..fa6e722 100644
--- a/helpers/admin.php
+++ b/helpers/admin.php
@@ -115,20 +115,20 @@
}
if( ! function_exists('redirect'))
{
- /**
- *
- * Redirect to a route
- *
- * @param string $route_name
- * @param string $message
- * @param bool $success
- *
- * @throws DependencyException
- * @throws Kedavra
- * @throws NotFoundException
- * @return RedirectResponse
- */
- function redirect(string $route_name, string $message = '', bool $success = true) : RedirectResponse
+ /**
+ *
+ * Redirect to a route
+ *
+ * @param string $url
+ * @param string $message
+ * @param bool $success
+ *
+ * @return RedirectResponse
+ *
+ * @throws DependencyException
+ * @throws NotFoundException
+ */
+ function redirect(string $url, string $message = '', bool $success = true) : RedirectResponse
{
if(def($message))
@@ -137,7 +137,7 @@
$success ? $flash->success($message) : $flash->failure($message);
}
- return (new RedirectResponse(route($route_name)))->send();
+ return (new RedirectResponse($url))->send();
}
}
if( ! function_exists('base'))
diff --git a/helpers/web.php b/helpers/web.php
index 6073584..5435c6a 100644
--- a/helpers/web.php
+++ b/helpers/web.php
@@ -358,10 +358,8 @@ use Imperium\Model\Web;
*
* @method route
*
- * @param mixed $name
- * @param bool $web
- * @param bool $admin
- * @param bool $task
+ * @param string $db
+ * @param string $route
* @param mixed $args
*
* @return string
@@ -370,35 +368,23 @@ use Imperium\Model\Web;
* @throws Kedavra
* @throws NotFoundException
*/
- function route(string $name,bool $web,bool $admin = false,bool $task = false,array $args = []): string
+ function route(string $db,string $route,array $args =[]): string
{
-
- $admin_r = Admin::where('name', EQUAL, $name)->fetch(true)->all();
- $web_r = Web::where('name', EQUAL, $name)->fetch(true)->all();
- $task_r = Task::where('name',EQUAL,$name)->fetch(true)->all();
- if ($admin)
- {
- is_true(not_def($admin_r),true,"The $name route was not found");
- }
- if ($web)
+ switch ($db)
{
- is_true(not_def($web_r),true,"The $name route was not found");
+ case 'admin':
+ $x = Admin::where('name',EQUAL,$route)->fetch(true)->all();
+ break;
+ case 'web':
+ $x = Web::where('name',EQUAL,$route)->fetch(true)->all();
+ break;
+ case 'task':
+ $x = Task::where('name',EQUAL,$route)->fetch(true)->all();
+ break;
+ default:
+ throw new Kedavra("The db parameter must be web, admin or task");
+ break;
}
- if ($task)
- {
- is_true(not_def($task_r),true,"The $name route was not found");
- }
-
-
- if ($admin)
-
- $x = $admin_r;
- elseif ($web)
- $x = $web_r;
- else
- $x = $task_r;
-
-
if (def($args))
{
@@ -446,26 +432,40 @@ use Imperium\Model\Web;
if (!function_exists('detect_method'))
{
- function detect_method(string $route,bool $web = true,bool $admin =false,bool $task = false)
+ /**
+ *
+ * Detect a route method
+ *
+ * @param string $db
+ * @param string $route
+ *
+ * @return string
+ *
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws NotFoundException
+ *
+ */
+ function detect_method(string $db,string $route): string
{
- if ($web)
- {
- $web = Web::where('name',EQUAL,$route)->fetch(true)->all();
- return $web->method;
- }
- if ($admin)
- {
- $admin = Admin::where('name',EQUAL,$route)->fetch(true)->all();
- return $admin->method;
- }
- if ($task)
+ switch ($db)
{
- $task = Task::where('name',EQUAL,$route)->fetch(true)->all();
- return $task->method;
+ case 'admin':
+ $x = Admin::where('name',EQUAL,$route)->fetch(true)->all();
+ break;
+ case 'web':
+ $x = Web::where('name',EQUAL,$route)->fetch(true)->all();
+ break;
+ case 'task':
+ $x = Task::where('name',EQUAL,$route)->fetch(true)->all();
+ break;
+ default:
+ throw new Kedavra("The db parameter must be web, admin or task");
+ break;
}
+ return $x->method;
- return 'POST';
}
}
diff --git a/imperium/Action/Crud.php b/imperium/Action/Crud.php
index 9467e8c..8159996 100644
--- a/imperium/Action/Crud.php
+++ b/imperium/Action/Crud.php
@@ -203,7 +203,7 @@
{
$this->init();
- return $this->truncate($table) ? $this->to(route('show',false,true,false,[$table]),$this->truncated) : $this->to(route('show',false,true,false,[$table]),$this->no_truncated,false);
+ return $this->truncate($table) ? $this->to(route('admin','show',[$table]),$this->truncated) : $this->to(route('admin','show',[$table]),$this->no_truncated,false);
}
/**
@@ -225,7 +225,7 @@
*/
public function edit(string $table,int $id): Response
{
- $form = $this->form()->start('update',false,true,false,[$table,$id])->generate($table,$this->config('crud','update_text'),'',Form::EDIT,$id);
+ $form = $this->form()->start('admin','update',[$table,$id])->generate($table,$this->config('crud','update_text'),'',Form::EDIT,$id);
return $this->view('@crud/edit',compact('form','table'));
}
@@ -247,7 +247,7 @@
*/
public function create(string $table): Response
{
- $form = $this->form()->start('create',false,true,false,[$table])->generate($table,$this->config('crud','create_text'));
+ $form = $this->form()->start('admin','create',[$table])->generate($table,$this->config('crud','create_text'));
return $this->view('@crud/create',compact('form','table'));
}
@@ -331,7 +331,7 @@
{
$x = collect(['/' => $this->config('crud','select_table_text')]);
foreach ($this->tables() as $table)
- $x->put(route('show',false,true,false,[$table,1]),$table);
+ $x->put(route('admin','show',[$table,1]),$table);
$tables = $x->all();
$form = $this->form()->redirect('table',$tables)->get();
return $this->view('@crud/home',compact('tables','form'));
@@ -354,8 +354,8 @@
$confirm = $this->config('crud','sure');
$sure = "onclick=\"return confirm('$confirm');\"";
- append($html,'<td><a href="'.route('edit',false,true,false,[$this->current,$value->id]).'" class="'.$this->config('crud','edit_class').'">'.$this->config('crud','edit_text').'</a></td>');
- append($html,'<td><a href="'.route('remove',false,true,false,[$this->current,$value->id]).'" class="'.$this->config('crud','remove_class').'" '.$sure.'> '.$this->config('crud','remove_text').'</a></td>');
+ append($html,'<td><a href="'.route('admin','edit',[$this->current,$value->id]).'" class="'.$this->config('crud','edit_class').'">'.$this->config('crud','edit_text').'</a></td>');
+ append($html,'<td><a href="'.route('admin','remove',[$this->current,$value->id]).'" class="'.$this->config('crud','remove_class').'" '.$sure.'> '.$this->config('crud','remove_text').'</a></td>');
return $html . '</tr>';
}
/**
diff --git a/imperium/App.php b/imperium/App.php
index d856ece..b1ec985 100644
--- a/imperium/App.php
+++ b/imperium/App.php
@@ -407,20 +407,20 @@ namespace Imperium {
*
* Redirect user to a route
*
+ * @param string $db
* @param string $route
+ * @param array $args
* @param string $message
* @param bool $success
*
* @return RedirectResponse
- *
+ * @throws DependencyException
* @throws Kedavra
* @throws NotFoundException
- * @throws DependencyException
*/
- public function redirect(string $route, string $message = '', bool $success = true): RedirectResponse
+ public function redirect(string $db,string $route,array $args =[],string $message = '', bool $success = true) : RedirectResponse
{
-
- return redirect($route, $message, $success);
+ return redirect(route($db,$route,$args), $message, $success);
}
/**
@@ -436,7 +436,6 @@ namespace Imperium {
*/
public function back(string $message = '', bool $success = true): RedirectResponse
{
-
return back($message, $success);
}
@@ -454,7 +453,6 @@ namespace Imperium {
*/
public function to(string $url, string $message = '', bool $success = true): RedirectResponse
{
-
return to($url, $message, $success);
}
@@ -507,18 +505,19 @@ namespace Imperium {
*
* Generate url string
*
+ * @param string $db
* @param string $route
- * @param bool $admin
* @param mixed $args
*
* @return string
*
+ * @throws DependencyException
* @throws Kedavra
+ * @throws NotFoundException
*/
- public function url(string $route, bool $admin, ...$args): string
+ public function url(string $db,string $route,...$args) : string
{
-
- return route($route, $admin, $args);
+ return route($db,$route,$args);
}
/**
@@ -535,7 +534,6 @@ namespace Imperium {
*/
public function sql(string $table, bool $web = false, $admin = false): Query
{
-
return Query::from($table, $web, $admin);
}
@@ -547,7 +545,6 @@ namespace Imperium {
*/
public function curl(): Curl
{
-
return new Curl();
}
@@ -560,7 +557,6 @@ namespace Imperium {
*/
public function shop(): Shop
{
-
return new Shop();
}
@@ -575,7 +571,6 @@ namespace Imperium {
*/
public function save(): bool
{
-
return (new Dump(true, []))->dump();
}
diff --git a/imperium/Html/Form/Form.php b/imperium/Html/Form/Form.php
index 14e46bd..c09c5e6 100644
--- a/imperium/Html/Form/Form.php
+++ b/imperium/Html/Form/Form.php
@@ -566,10 +566,8 @@
*
* @method start
*
+ * @param string $db
* @param string $route
- * @param bool $web
- * @param bool $admin
- * @param bool $task
* @param array $args
* @param string $confirm The confirm text
* @param string $class The form class
@@ -581,10 +579,10 @@
* @throws Kedavra
* @throws NotFoundException
*/
- public function start(string $route,bool $web,bool $admin,bool $task,array $args= [],string $confirm = '', string $class = '', bool $enctype = false, string $charset = 'utf-8') : Form
+ public function start(string $db,string $route,array $args= [],string $confirm = '', string $class = '', bool $enctype = false, string $charset = 'utf-8') : Form
{
- $this->method = detect_method($route,$web,$admin,$task);
+ $this->method = detect_method($db,$route);
$method = POST;
@@ -594,16 +592,16 @@
if($enctype)
{
if(not_def($class))
- append($this->form, '<form action="' . route($route,$web,$admin,$task,$args) . '" method="' . $method . '" class="' . self::VALIDATE . '" accept-charset="' . $charset . '" enctype="multipart/form-data" onsubmit="return confirm(' . "'" . $confirm . "'" . ')" >');
+ append($this->form, '<form action="' . route($db,$route,$args) . '" method="' . $method . '" class="' . self::VALIDATE . '" accept-charset="' . $charset . '" enctype="multipart/form-data" onsubmit="return confirm(' . "'" . $confirm . "'" . ')" >');
else
- append($this->form, '<form action="' . route($route,$web,$admin,$task,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . ' ' . self::VALIDATE . '" enctype="multipart/form-data" onsubmit="return confirm(' . "'" . $confirm . "'" . ')">');
+ append($this->form, '<form action="' . route($db,$route,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . ' ' . self::VALIDATE . '" enctype="multipart/form-data" onsubmit="return confirm(' . "'" . $confirm . "'" . ')">');
}
else
{
if(not_def($class))
- append($this->form, '<form action="' . route($route,$web,$admin,$task,$args) . '" method="' . $method . '" class="' . self::VALIDATE . '" accept-charset="' . $charset . '" onsubmit="return confirm(' . "'" . $confirm . "'" . ')">');
+ append($this->form, '<form action="' . route($db,$route,$args) . '" method="' . $method . '" class="' . self::VALIDATE . '" accept-charset="' . $charset . '" onsubmit="return confirm(' . "'" . $confirm . "'" . ')">');
else
- append($this->form, '<form action="' . route($route,$web,$admin,$task,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . ' ' . self::VALIDATE . '" onsubmit="return confirm(' . "'" . $confirm . "'" . ')" >');
+ append($this->form, '<form action="' . route($db,$route,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . ' ' . self::VALIDATE . '" onsubmit="return confirm(' . "'" . $confirm . "'" . ')" >');
}
}
else
@@ -611,16 +609,16 @@
if($enctype)
{
if(not_def($class))
- append($this->form, '<form action="' . route($route,$web,$admin,$task,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" enctype="multipart/form-data">');
+ append($this->form, '<form action="' . route($db,$route,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" enctype="multipart/form-data">');
else
- append($this->form, '<form action="' . route($route,$web,$admin,$task,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . '" enctype="multipart/form-data">');
+ append($this->form, '<form action="' . route($db,$route,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . '" enctype="multipart/form-data">');
}
else
{
if(not_def($class))
- append($this->form, '<form action="' . route($route,$web,$admin,$task,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" >');
+ append($this->form, '<form action="' . route($db,$route,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" >');
else
- append($this->form, '<form action="' . route($route,$web,$admin,$task,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . '" >');
+ append($this->form, '<form action="' . route($db,$route,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . '" >');
}
}
if(config($this->file, 'large'))
diff --git a/imperium/Management.php b/imperium/Management.php
index d1f7bbc..f8a3824 100644
--- a/imperium/Management.php
+++ b/imperium/Management.php
@@ -283,21 +283,20 @@
* @return Response
*/
public function view(string $name,array $args = [],int $status= 200,array $headers = []) : Response;
-
- /**
- *
- * Redirect user to a route
- *
- * @param string $route
- * @param string $message
- * @param bool $success
- *
- * @throws Kedavra
- *
- * @return RedirectResponse
- *
- */
- public function redirect(string $route, string $message = '', bool $success = true) : RedirectResponse;
+
+ /**
+ *
+ * Redirect user to a route
+ *
+ * @param string $db
+ * @param string $route
+ * @param array $args
+ * @param string $message
+ * @param bool $success
+ *
+ * @return RedirectResponse
+ */
+ public function redirect(string $db,string $route,array $args =[],string $message = '', bool $success = true) : RedirectResponse;
/**
*
@@ -393,12 +392,14 @@
* Generate url string
*
* @param string $route
+ * @param bool $web
* @param bool $admin
+ * @param bool $task
* @param mixed $args
*
* @return string
*/
- public function url(string $route,bool $admin ,...$args) : string;
+ public function url(string $db,string $route,...$args) : string;
/**
*
diff --git a/imperium/Routing/Router.php b/imperium/Routing/Router.php
index ac99c41..e66bf7e 100644
--- a/imperium/Routing/Router.php
+++ b/imperium/Routing/Router.php
@@ -129,7 +129,7 @@
- return to(route('404'));
+ return to(route('web','404'));
}
/**
diff --git a/imperium/View/View.php b/imperium/View/View.php
index cf7c3ee..5bfc9bb 100644
--- a/imperium/View/View.php
+++ b/imperium/View/View.php
@@ -172,10 +172,10 @@
}, [ 'is_safe' => [ 'html' ] ]),
- new TwigFunction('route', function(string $name, bool $web,bool $admin,bool $task,...$args)
+ new TwigFunction('route', function(string $db, string $route,...$args)
{
- return route($name,$web,$admin,$task, $args);
+ return route($db,$route,$args);
}, [ 'is_safe' => [ 'html' ] ]),new TwigFunction('logged', function()
{
diff --git a/tests/AppTest.php b/tests/AppTest.php
index aa4933d..61175c7 100644
--- a/tests/AppTest.php
+++ b/tests/AppTest.php
@@ -31,7 +31,7 @@
$this->assertInstanceOf(Request::class,app()->request());
$this->assertInstanceOf(Oauth::class,app()->auth());
$this->assertInstanceOf(Write::class,app()->write('test','i am a test','micieli@laposte.net','micieli@laposte.net'));
- $this->assertInstanceOf(RedirectResponse::class,app()->redirect('root'));
+ $this->assertInstanceOf(RedirectResponse::class,app()->redirect('web','root'));
$this->assertInstanceOf(RedirectResponse::class,app()->back());
$this->assertInstanceOf(RedirectResponse::class,app()->to('/'));
$this->assertInstanceOf(Cache::class,app()->cache());
diff --git a/tests/Helpers/AdminTest.php b/tests/Helpers/AdminTest.php
index 092014e..2282aa8 100644
--- a/tests/Helpers/AdminTest.php
+++ b/tests/Helpers/AdminTest.php
@@ -24,7 +24,7 @@ class AdminTest extends Unit
public function test_redirect()
{
- $this->assertTrue(redirect('root','a',true)->isRedirect('/'));
+ $this->assertTrue(redirect('/','a',true)->isRedirect('/'));
}
public function test_history()
diff --git a/tests/Helpers/WebTest.php b/tests/Helpers/WebTest.php
index eb48937..65e32b5 100644
--- a/tests/Helpers/WebTest.php
+++ b/tests/Helpers/WebTest.php
@@ -70,8 +70,8 @@ class WebTest extends Unit
public function test_route()
{
- $this->assertEquals('/',route('root'));
- $this->assertEquals('/game/imperium',route('game',false,['imperium']));
+ $this->assertEquals('/',route('web','root'));
+ $this->assertEquals('/game/imperium',route('web','game',['imperium']));
}
public function test_is_mobile()
diff --git a/tests/RouterTest.php b/tests/RouterTest.php
index c13c760..c566432 100644
--- a/tests/RouterTest.php
+++ b/tests/RouterTest.php
@@ -48,7 +48,9 @@ namespace Testing {
}
/**
+ * @throws DependencyException
* @throws Kedavra
+ * @throws NotFoundException
*/
public function test_add_route()
{
@@ -93,7 +95,7 @@ namespace Testing {
$this->assertEquals(302,$response->getStatusCode());
$this->assertTrue($response->isRedirection());
$this->assertTrue($response->isRedirect('/error'));
- $this->assertTrue($response->isRedirect(route('404')));
+ $this->assertTrue($response->isRedirect(route('web','404')));
}