|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Jasny\Controller; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Controller base class |
|
11
|
|
|
*/ |
|
|
|
|
|
|
12
|
|
|
abstract class Controller |
|
13
|
|
|
{ |
|
14
|
|
|
use Traits\Base, |
|
15
|
|
|
Traits\Header, |
|
16
|
|
|
Traits\Output, |
|
17
|
|
|
Traits\CheckRequest, |
|
18
|
|
|
Traits\CheckResponse, |
|
19
|
|
|
Traits\ContentNegotiation, |
|
20
|
|
|
Traits\Guarded; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Called before executing the action. |
|
24
|
|
|
* @codeCoverageIgnore |
|
|
|
|
|
|
25
|
|
|
* |
|
26
|
|
|
* <code> |
|
27
|
|
|
* protected function before() |
|
28
|
|
|
* { |
|
29
|
|
|
* if ($this->auth->getUser()->getCredits() <= 0) { |
|
30
|
|
|
* return $this->paymentRequired()->output("Sorry, you're out of credits"); |
|
31
|
|
|
* } |
|
32
|
|
|
* } |
|
33
|
|
|
* </code> |
|
34
|
|
|
* |
|
35
|
|
|
* @return void|null|ResponseInterface|static |
|
36
|
|
|
*/ |
|
37
|
2 |
|
protected function before() |
|
38
|
|
|
{ |
|
39
|
2 |
|
} |
|
40
|
1 |
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Called after executing the action. |
|
43
|
1 |
|
* @codeCoverageIgnore |
|
|
|
|
|
|
44
|
|
|
* |
|
45
|
|
|
* @return void|null|ResponseInterface|static |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function after() |
|
48
|
|
|
{ |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
/** |
|
|
|
|
|
|
52
|
|
|
* Get the method name of the action |
|
53
|
3 |
|
*/ |
|
|
|
|
|
|
54
|
1 |
|
protected function getActionMethod(string $action): string |
|
55
|
|
|
{ |
|
56
|
|
|
$sentence = preg_replace('/[\W_]+/', ' ', $action); |
|
57
|
2 |
|
return lcfirst(str_replace(' ', '', ucwords($sentence))); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
|
|
|
|
|
61
|
|
|
* Invoke the controller. |
|
62
|
|
|
*/ |
|
|
|
|
|
|
63
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface |
|
64
|
|
|
{ |
|
65
|
1 |
|
$this->request = $request; |
|
66
|
|
|
$this->response = $response; |
|
67
|
1 |
|
|
|
68
|
1 |
|
$method = $this->getActionMethod($request->getAttribute('route:action', 'process')); |
|
69
|
|
|
$refl = method_exists($this, $method) ? new \ReflectionMethod($this, $method) : null; |
|
70
|
|
|
|
|
71
|
|
|
if ($refl === null || !$refl->isPublic() || $refl->isConstructor() || $method === __METHOD__) { |
|
72
|
|
|
return $this->notFound()->output('Not found')->getResponse(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
try { |
|
76
|
|
|
$args = $this->getFunctionArgs($refl); |
|
77
|
|
|
} catch (ParameterException $exception) { |
|
78
|
|
|
return $this->badRequest()->output($exception->getMessage())->getResponse(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$result = $this->guard(new \ReflectionObject($this)); |
|
82
|
|
|
if ($result !== null) { |
|
83
|
|
|
return $result; |
|
84
|
|
|
} |
|
85
|
2 |
|
|
|
86
|
|
|
$result = $this->before(); |
|
|
|
|
|
|
87
|
2 |
|
if ($result !== null) { |
|
|
|
|
|
|
88
|
2 |
|
return $result instanceof ResponseInterface ? $result : $this->getResponse(); |
|
89
|
|
|
} |
|
90
|
2 |
|
|
|
91
|
1 |
|
$result = $this->guard($refl); |
|
92
|
|
|
if ($result !== null) { |
|
93
|
|
|
return $result; |
|
94
|
2 |
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
$result = [$this, $method](...$args); |
|
97
|
|
|
|
|
98
|
|
|
$response = $this->after() ?? $result; |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
return $response instanceof ResponseInterface ? $response : $this->getResponse(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|