|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Blitz PHP framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace BlitzPHP\Middlewares; |
|
13
|
|
|
|
|
14
|
|
|
use BlitzPHP\Utilities\String\Text; |
|
15
|
|
|
|
|
16
|
|
|
abstract class BaseMiddleware |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Liste des arguments envoyes au middleware |
|
20
|
|
|
*/ |
|
21
|
|
|
protected array $arguments = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Liste des arguments que peut avoir le middleware |
|
25
|
|
|
*/ |
|
26
|
|
|
protected array $fillable = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Chemin url de la requette actuelle |
|
30
|
|
|
*/ |
|
31
|
|
|
protected string $path; |
|
32
|
|
|
|
|
33
|
|
|
public function init(array $arguments = []): static |
|
34
|
|
|
{ |
|
35
|
|
|
$this->path = $arguments['path'] ?: '/'; |
|
36
|
|
|
unset($arguments['path']); |
|
37
|
|
|
|
|
38
|
|
|
$this->arguments = array_merge($this->arguments, $arguments); |
|
39
|
|
|
|
|
40
|
|
|
foreach ($this->arguments as $argument => $value) { |
|
41
|
|
|
if (! is_string($argument)) { |
|
42
|
|
|
continue; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$method = Text::camel('set_' . $argument); |
|
46
|
|
|
if (method_exists($this, $method)) { |
|
47
|
|
|
call_user_func([$this, $method], $value); |
|
48
|
|
|
} elseif (property_exists($this, $argument)) { |
|
49
|
|
|
$this->{$argument} = $value; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function __get($name) |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->arguments[$name] ?? null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @internal |
|
63
|
|
|
*/ |
|
64
|
|
|
final public function fill(array $params): static |
|
65
|
|
|
{ |
|
66
|
|
|
foreach ($this->fillable as $key) { |
|
67
|
|
|
if (empty($params)) { |
|
68
|
|
|
break; |
|
69
|
|
|
} |
|
70
|
|
|
$this->arguments[$key] = array_shift($params); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|