1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HexMakina\kadro\Controllers; |
4
|
|
|
|
5
|
|
|
use Psr\Container\{ContainerInterface,ContainerExceptionInterface,NotFoundExceptionInterface}; |
6
|
|
|
use HexMakina\kadro\Auth\{OperatorInterface, AccessRefusedException}; |
7
|
|
|
use HexMakina\Hopper\RouterInterface; |
8
|
|
|
use HexMakina\LogLaddy\LoggerInterface; |
9
|
|
|
use HexMakina\LeMarchand\LeMarchand; |
10
|
|
|
|
11
|
|
|
class Base implements Interfaces\BaseControllerInterface, \Psr\Container\ContainerInterface |
12
|
|
|
{ |
13
|
|
|
use \HexMakina\Traitor\Traitor; |
|
|
|
|
14
|
|
|
|
15
|
|
|
protected $route_back = null; |
16
|
|
|
protected $errors = []; |
17
|
|
|
|
18
|
|
|
public function errors(): array |
19
|
|
|
{ |
20
|
|
|
return $this->errors; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function container(): ContainerInterface |
24
|
|
|
{ |
25
|
|
|
return LeMarchand::box(); |
26
|
|
|
} |
27
|
|
|
public function has($key) |
28
|
|
|
{ |
29
|
|
|
return $this->container()->has($key); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function get($key) |
33
|
|
|
{ |
34
|
|
|
return $this->container()->get($key); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function add_error($message, $context = []) |
38
|
|
|
{ |
39
|
|
|
$this->errors[] = [$message, $context]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function logger(): LoggerInterface |
43
|
|
|
{ |
44
|
|
|
return $this->get('LoggerInterface'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// -------- Router |
48
|
|
|
|
49
|
|
|
public function router(): RouterInterface |
50
|
|
|
{ |
51
|
|
|
return $this->get('RouterInterface'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function prepare() |
55
|
|
|
{ |
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function execute() |
60
|
|
|
{ |
61
|
|
|
$ret = null; |
62
|
|
|
|
63
|
|
|
$method = $this->router()->targetMethod(); |
64
|
|
|
|
65
|
|
|
// before and after hooks, should they be in basecontroller ? |
66
|
|
|
// i think so, but pascal just proposed me pastis.. tomorrow |
67
|
|
|
|
68
|
|
|
foreach (['prepare', "before_$method", $method, "after_$method"] as $step => $chainling) { |
69
|
|
|
$this->traitor($chainling); |
70
|
|
|
|
71
|
|
|
if (method_exists($this, $chainling) && empty($this->errors())) { |
72
|
|
|
$res = $this->$chainling(); |
73
|
|
|
|
74
|
|
|
if ($this->logger()->hasHaltingMessages()) { // logger handled a critical error during the chailing execution |
75
|
|
|
break; // dont go on with other |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($chainling === $method) { |
79
|
|
|
$ret = $res; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->conclude(); |
85
|
|
|
|
86
|
|
|
return $ret; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function conclude() |
90
|
|
|
{ |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function has_route_back(): bool |
95
|
|
|
{ |
96
|
|
|
return is_null($this->route_back); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/* |
100
|
|
|
* returns string, a URL formatted by RouterInterface::pre_hop() |
101
|
|
|
* |
102
|
|
|
* USAGE |
103
|
|
|
* route_back($route_name=null) returns previously set $route_back or RouterInterface::ROUTE_HOME_NAME |
104
|
|
|
* route_back($route_name [,$route_params]), sets $route_back using route_factory() |
105
|
|
|
* |
106
|
|
|
*/ |
107
|
|
|
public function route_back($route_name = null, $route_params = []): string |
108
|
|
|
{ |
109
|
|
|
if (is_null($route_name)) { |
110
|
|
|
return $this->route_back ?? $this->router()->hyp(RouterInterface::ROUTE_HOME_NAME); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->route_back = $this->route_factory($route_name, $route_params); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function route_factory($route_name = null, $route_params = []): string |
117
|
|
|
{ |
118
|
|
|
$route = null; |
119
|
|
|
|
120
|
|
|
if (is_string($route_name) && !empty($route_name)) { |
121
|
|
|
if ($this->router()->routeExists($route_name)) { |
122
|
|
|
$route = $this->router()->hyp($route_name, $route_params); |
123
|
|
|
} else { |
124
|
|
|
$route = $route_name; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $route; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
throw new \Exception('ROUTE_FACTORY_PARAM_TYPE_ERROR'); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|