@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | |
27 | 27 | // Add base_url to $pattern, if needed |
28 | 28 | |
29 | - $link = "%s/%s"; |
|
29 | + $link = "%s/%s"; |
|
30 | 30 | $pattern = sprintf($link, Application::getAppConfig()->base_url, $pattern); |
31 | 31 | // If it matches, return the match |
32 | 32 | $parameters = $resolver->match($pattern); |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | // Check if callback is a controller method |
35 | 35 | $controller = $callback[0]; |
36 | 36 | $method = $callback[1]; |
37 | - if (! method_exists($controller, $method)) { |
|
37 | + if (!method_exists($controller, $method)) { |
|
38 | 38 | throw new Exceptions\ControllerNotFoundException("Invalid Controller and/or method in routes.php"); |
39 | 39 | } |
40 | 40 | return new MatchRoute(controller: $controller, method: $method, parameters: $parameters); |
@@ -51,25 +51,25 @@ discard block |
||
51 | 51 | */ |
52 | 52 | protected function handle(BaseMatch $matcher): HttpResponse |
53 | 53 | { |
54 | - if($matcher instanceof Match404) { |
|
54 | + if ($matcher instanceof Match404) { |
|
55 | 55 | return new NotFoundResponse(); |
56 | 56 | } |
57 | 57 | |
58 | - if($matcher instanceof MatchRoute) { |
|
58 | + if ($matcher instanceof MatchRoute) { |
|
59 | 59 | |
60 | - foreach($this->middlewares as $middleware => $args){ |
|
60 | + foreach ($this->middlewares as $middleware => $args) { |
|
61 | 61 | $middlewareInstance = new $middleware(); |
62 | 62 | $middlewareInstance->addMatcher($matcher); |
63 | 63 | $middlewareInstance->setRequest($this->request); |
64 | 64 | $middlewareResult = $middlewareInstance(...$args); |
65 | - if($middlewareResult instanceof HttpResponse){ |
|
65 | + if ($middlewareResult instanceof HttpResponse) { |
|
66 | 66 | return $middlewareResult; |
67 | 67 | } |
68 | 68 | } |
69 | 69 | |
70 | 70 | Application::$controller = $matcher->controller; |
71 | 71 | $controller = new $matcher->controller(); |
72 | - $method = $matcher->method; |
|
72 | + $method = $matcher->method; |
|
73 | 73 | return $controller->$method(...$matcher->parameters); |
74 | 74 | } |
75 | 75 | throw new \Exception("Wrong matcher!"); |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | |
18 | 18 | $root = Application::$documentRoot; |
19 | 19 | $root .= Application::getAppConfig()->base_url; |
20 | - $filePath = $root."/".Application::getAppConfig()->static_files_dir. "/". $url; |
|
20 | + $filePath = $root."/".Application::getAppConfig()->static_files_dir."/".$url; |
|
21 | 21 | |
22 | 22 | // Check if the file exists |
23 | 23 | if (file_exists($filePath)) { |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | } |
58 | 58 | |
59 | 59 | public function handle($match): FileResponse|NotFoundResponse{ |
60 | - if($match instanceof Match404){ |
|
60 | + if ($match instanceof Match404) { |
|
61 | 61 | return new NotFoundResponse(); |
62 | 62 | } |
63 | 63 | return new FileResponse($match->filePath, $match->contentType); |
@@ -18,16 +18,16 @@ |
||
18 | 18 | Liquid::set('INCLUDE_SUFFIX', 'html'); |
19 | 19 | Liquid::set('INCLUDE_PREFIX', ''); |
20 | 20 | $path = "app/Views"; |
21 | - $template = new Template($path); |
|
21 | + $template = new Template($path); |
|
22 | 22 | $template->registerFilter(new SiteFilter()); |
23 | 23 | |
24 | - if(isset($this->custom_filters)){ |
|
25 | - foreach($this->custom_filters as $filter){ |
|
24 | + if (isset($this->custom_filters)) { |
|
25 | + foreach ($this->custom_filters as $filter) { |
|
26 | 26 | $template->registerFilter(new $filter()); |
27 | 27 | } |
28 | 28 | } |
29 | 29 | $template->parseFile($view); |
30 | - $parameters = array_map(function ($x) { |
|
30 | + $parameters = array_map(function($x) { |
|
31 | 31 | if ($x instanceof QuerySet) { |
32 | 32 | return $x->do(); |
33 | 33 | } else { |
@@ -14,18 +14,17 @@ discard block |
||
14 | 14 | |
15 | 15 | protected function handle(mixed ...$middlewareParams): HttpResponse|Request |
16 | 16 | { |
17 | - if($this->match instanceof MatchRoute) { |
|
17 | + if ($this->match instanceof MatchRoute) { |
|
18 | 18 | $reflection = new \ReflectionMethod($this->match->controller, $this->match->method); |
19 | 19 | $attributes = $reflection->getAttributes(); |
20 | 20 | |
21 | 21 | foreach ($attributes as $attribute) { |
22 | - if(is_subclass_of($attribute->getName(), AbstractAccessControlAttribute::class)) { |
|
22 | + if (is_subclass_of($attribute->getName(), AbstractAccessControlAttribute::class)) { |
|
23 | 23 | return |
24 | - $this->checkPermissions($attribute->getName(), ...($attribute->getArguments()))? |
|
25 | - $this->request : |
|
26 | - new RedirectResponse( |
|
24 | + $this->checkPermissions($attribute->getName(), ...($attribute->getArguments())) ? |
|
25 | + $this->request : new RedirectResponse( |
|
27 | 26 | Application::getAuthConfig()->login_url, |
28 | - redirect_after: $this->request->url |
|
27 | + redirect_after : $this->request->url |
|
29 | 28 | ); |
30 | 29 | } |
31 | 30 | |
@@ -36,7 +35,7 @@ discard block |
||
36 | 35 | |
37 | 36 | |
38 | 37 | protected function checkPermissions(string $modifier, mixed ...$params):bool{ |
39 | - if($modifier == LoginRequired::class){ |
|
38 | + if ($modifier == LoginRequired::class) { |
|
40 | 39 | $authenticator = new \Lepton\Authenticator\UserAuthenticator(); |
41 | 40 | return $authenticator->isLoggedIn(); |
42 | 41 | } |
@@ -30,18 +30,18 @@ |
||
30 | 30 | protected function checkPermissions(string $modifier, mixed ...$params): bool |
31 | 31 | { |
32 | 32 | |
33 | - if($modifier == LoginRequired::class) { |
|
33 | + if ($modifier == LoginRequired::class) { |
|
34 | 34 | |
35 | 35 | $level = $params[0] ?? 1; |
36 | 36 | $authenticator = new \Lepton\Authenticator\UserAuthenticator(); |
37 | 37 | $loggedIn = $authenticator->isLoggedIn(); |
38 | - if(! $loggedIn){ |
|
38 | + if (!$loggedIn) { |
|
39 | 39 | return false; |
40 | 40 | } |
41 | 41 | $user = $authenticator->getLoggedUser(); |
42 | 42 | $splitted = explode("__", $this->level_field); |
43 | 43 | $user_level = $user; |
44 | - foreach($splitted as $part){ |
|
44 | + foreach ($splitted as $part) { |
|
45 | 45 | $user_level = $user_level->$part; |
46 | 46 | } |
47 | 47 | return ($user_level >= $level); |
@@ -20,14 +20,14 @@ discard block |
||
20 | 20 | $this->rbac_class = $middlewareParams["rbac_class"] ?? throw new \Exception("You have to define a RBAC class"); |
21 | 21 | |
22 | 22 | $rbac_interfaces = class_implements($this->rbac_class); |
23 | - if(! in_array(\Lepton\Authenticator\RBAC\RBACInterface::class, $rbac_interfaces)) { |
|
23 | + if (!in_array(\Lepton\Authenticator\RBAC\RBACInterface::class, $rbac_interfaces)) { |
|
24 | 24 | throw new \Exception("RBAC class has to implement \Lepton\Authenticator\RBAC\RBACInterface"); |
25 | 25 | } |
26 | 26 | |
27 | 27 | $this->user_class = $middlewareParams["user_class"] ?? throw new \Exception("You have to define a User class"); |
28 | 28 | |
29 | 29 | $user_interfaces = class_implements($this->user_class); |
30 | - if(! in_array(\Lepton\Authenticator\RBAC\UserInterface::class, $user_interfaces)) { |
|
30 | + if (!in_array(\Lepton\Authenticator\RBAC\UserInterface::class, $user_interfaces)) { |
|
31 | 31 | throw new \Exception("User class has to implement \Lepton\Authenticator\RBAC\UserInterface"); |
32 | 32 | } |
33 | 33 | |
@@ -38,18 +38,18 @@ discard block |
||
38 | 38 | protected function checkPermissions(string $modifier, mixed ...$params): bool |
39 | 39 | { |
40 | 40 | |
41 | - if($modifier == LoginRequired::class) { |
|
41 | + if ($modifier == LoginRequired::class) { |
|
42 | 42 | |
43 | 43 | $level = isset($params[0]) ? $params[0] : 1; |
44 | 44 | $authenticator = new \Lepton\Authenticator\UserAuthenticator(); |
45 | 45 | $loggedIn = $authenticator->isLoggedIn(); |
46 | - if(! $loggedIn) { |
|
46 | + if (!$loggedIn) { |
|
47 | 47 | return false; |
48 | 48 | } |
49 | 49 | $user = $authenticator->getLoggedUser(); |
50 | 50 | $num_privileges = $user->privileges->and(livello__gte: $level)->count(); |
51 | 51 | return ($num_privileges > 0); |
52 | - } elseif($modifier == PermissionRequired::class){ |
|
52 | + } elseif ($modifier == PermissionRequired::class) { |
|
53 | 53 | $user = (new UserAuthenticator)->getLoggedUser(); |
54 | 54 | die(print_r($params)); |
55 | 55 | } |
@@ -12,7 +12,7 @@ |
||
12 | 12 | parent::__construct(headers: ["Content-Type" => "application/json"]); |
13 | 13 | } |
14 | 14 | |
15 | - public function sendBody(){ |
|
15 | + public function sendBody() { |
|
16 | 16 | echo json_encode($this->array); |
17 | 17 | } |
18 | 18 |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | |
89 | 89 | |
90 | 90 | |
91 | - public function register($username, $password=null, $password_length=6) |
|
91 | + public function register($username, $password = null, $password_length = 6) |
|
92 | 92 | { |
93 | 93 | // Check if username is already taken |
94 | 94 | if ($this->getUserByUsername($username)) { |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | } |
97 | 97 | |
98 | 98 | // Hash the password |
99 | - if (! $password) { |
|
99 | + if (!$password) { |
|
100 | 100 | $password = $this->randomPassword(length: $password_length); |
101 | 101 | } |
102 | 102 |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | public function rewind(): void |
179 | 179 | { |
180 | 180 | $this->index = -1; |
181 | - if (! isset($this->result)) { |
|
181 | + if (!isset($this->result)) { |
|
182 | 182 | $this->cache = array(); |
183 | 183 | $this->do(); |
184 | 184 | } |
@@ -219,8 +219,8 @@ discard block |
||
219 | 219 | $db_columns = $this->current->db_columns(); |
220 | 220 | // Convert column names to field names |
221 | 221 | $fields = array(); |
222 | - foreach($items as $column => $value){ |
|
223 | - $fieldName = array_search($column ,$db_columns); |
|
222 | + foreach ($items as $column => $value) { |
|
223 | + $fieldName = array_search($column, $db_columns); |
|
224 | 224 | $fields[$fieldName] = $value; |
225 | 225 | } |
226 | 226 | $this->current->load(...$fields); |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | $this->current->clearEditedFields(); |
229 | 229 | } else { |
230 | 230 | $this->current = null; |
231 | - $this->index = -1; |
|
231 | + $this->index = -1; |
|
232 | 232 | } |
233 | 233 | |
234 | 234 | |
@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | |
240 | 240 | public function first() |
241 | 241 | { |
242 | - if(!isset($this->cache)) { |
|
242 | + if (!isset($this->cache)) { |
|
243 | 243 | $this->rewind(); |
244 | 244 | } |
245 | 245 | |
@@ -384,7 +384,7 @@ discard block |
||
384 | 384 | } |
385 | 385 | |
386 | 386 | $this->modifiers["ORDER BY"] = []; |
387 | - foreach($filters as $filter){ |
|
387 | + foreach ($filters as $filter) { |
|
388 | 388 | $this->modifiers["ORDER BY"][] = is_array($filter) ? $filter : [$filter => "ASC"]; |
389 | 389 | } |
390 | 390 | $this->modifiers["ORDER BY"] = $filters; |
@@ -403,7 +403,7 @@ discard block |
||
403 | 403 | |
404 | 404 | public function count(): int |
405 | 405 | { |
406 | - if (! isset($this->result)) { |
|
406 | + if (!isset($this->result)) { |
|
407 | 407 | $this->do(); |
408 | 408 | } |
409 | 409 | return $this->result->num_rows; |
@@ -438,7 +438,7 @@ discard block |
||
438 | 438 | |
439 | 439 | |
440 | 440 | // Get the result |
441 | - $this->result = $result->fetch_result() ; |
|
441 | + $this->result = $result->fetch_result(); |
|
442 | 442 | return $this; |
443 | 443 | } |
444 | 444 | |
@@ -473,12 +473,12 @@ discard block |
||
473 | 473 | $modifiers = ""; |
474 | 474 | |
475 | 475 | // it there are any ORDER BY, build the clause |
476 | - if (count($this->modifiers)> 0) { |
|
476 | + if (count($this->modifiers) > 0) { |
|
477 | 477 | list($modifiers, $join) = $this->buildModifiers(); |
478 | 478 | } |
479 | 479 | |
480 | 480 | // if there are any filters build WHERE clause |
481 | - if (count($this->filters)> 0) { |
|
481 | + if (count($this->filters) > 0) { |
|
482 | 482 | list($whereClause, $values, $join_t) = $this->buildWhereClause($this->filters); |
483 | 483 | } |
484 | 484 | |
@@ -488,12 +488,12 @@ discard block |
||
488 | 488 | $query .= " ".$this->buildJoin($join); |
489 | 489 | $query .= " ".$this->buildJoin($join_t); |
490 | 490 | |
491 | - if(count($this->filters)>0){ |
|
491 | + if (count($this->filters) > 0) { |
|
492 | 492 | $query .= sprintf(" WHERE %s ", $whereClause); |
493 | 493 | } |
494 | 494 | |
495 | 495 | // it there are any ORDER BY, build the clause |
496 | - if (count($this->modifiers)> 0) { |
|
496 | + if (count($this->modifiers) > 0) { |
|
497 | 497 | $query .= sprintf(" ORDER BY %s", $modifiers); |
498 | 498 | } |
499 | 499 | |
@@ -523,10 +523,10 @@ discard block |
||
523 | 523 | |
524 | 524 | $clause = array(); |
525 | 525 | for ($i = 1; $i < count($join); $i++) { |
526 | - $clause[] = sprintf( |
|
526 | + $clause[] = sprintf( |
|
527 | 527 | " %s ON %s.%s = %s.%s", |
528 | 528 | $join[$i]["table"], |
529 | - $join[$i-1]["table"], |
|
529 | + $join[$i - 1]["table"], |
|
530 | 530 | $join[$i]["column"], |
531 | 531 | $join[$i]["table"], |
532 | 532 | $join[$i]["column"] |
@@ -544,8 +544,8 @@ discard block |
||
544 | 544 | $conditions = array(); |
545 | 545 | $join = array(); |
546 | 546 | |
547 | - foreach($order_by as $order_clause) { |
|
548 | - if(is_array($order_clause)) { |
|
547 | + foreach ($order_by as $order_clause) { |
|
548 | + if (is_array($order_clause)) { |
|
549 | 549 | $raw = array_key_first($order_clause); |
550 | 550 | $method = $order_clause[$raw]; |
551 | 551 | } else { |
@@ -615,7 +615,7 @@ discard block |
||
615 | 615 | $values = array(); |
616 | 616 | $join = array(); |
617 | 617 | |
618 | - foreach($filters as $key => $value) { |
|
618 | + foreach ($filters as $key => $value) { |
|
619 | 619 | $lookup = $this->lookup($key); |
620 | 620 | |
621 | 621 | $column = $lookup["column"]; |
@@ -664,8 +664,8 @@ discard block |
||
664 | 664 | |
665 | 665 | $join = array(); |
666 | 666 | foreach ($match as $k) { |
667 | - if($last->isForeignKey($k)) { |
|
668 | - $new= new ($last->getRelationshipParentModel($k))(); |
|
667 | + if ($last->isForeignKey($k)) { |
|
668 | + $new = new ($last->getRelationshipParentModel($k))(); |
|
669 | 669 | |
670 | 670 | $join[] = array( |
671 | 671 | "column"=> $last->getColumnFromField($k), |