|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Controllers\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Dispatcher\Resolver\ClassResolver\NameFormatter; |
|
6
|
|
|
use Nip\Http\Response\Response; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Trait ActionCallTrait |
|
12
|
|
|
* @package Nip\Controllers\Traits |
|
13
|
|
|
*/ |
|
14
|
|
|
trait ActionCallTrait |
|
15
|
|
|
{ |
|
16
|
|
|
protected $action = null; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param bool $action |
|
20
|
|
|
* |
|
21
|
|
|
* @return Response|ResponseInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
public function dispatchAction($action = false) |
|
24
|
|
|
{ |
|
25
|
|
|
$action = NameFormatter::formatActionName($action); |
|
26
|
|
|
|
|
27
|
|
|
return $this->callAction($action); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param bool $method |
|
32
|
|
|
* @param array $parameters |
|
33
|
|
|
* @return ResponseInterface |
|
34
|
|
|
*/ |
|
35
|
3 |
|
public function callAction($method = false, $parameters = []) |
|
36
|
|
|
{ |
|
37
|
3 |
|
if ($method) { |
|
38
|
2 |
|
if ($this->validAction($method)) { |
|
39
|
2 |
|
$this->setAction($method); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
2 |
|
return $this->runAction($method, $parameters); |
|
42
|
|
|
} else { |
|
43
|
|
|
throw new NotFoundHttpException( |
|
44
|
|
|
'Controller method ['.$method.'] not found for '.get_class($this) |
|
|
|
|
|
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
throw new NotFoundHttpException('No action specified for '.get_class($this)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param $method |
|
54
|
|
|
* @param array $parameters |
|
55
|
|
|
* @return ResponseInterface |
|
56
|
|
|
*/ |
|
57
|
2 |
|
protected function runAction($method, $parameters = []) |
|
58
|
|
|
{ |
|
59
|
2 |
|
$this->invokeStage('parseRequest'); |
|
|
|
|
|
|
60
|
2 |
|
$this->invokeStage('beforeAction'); |
|
61
|
|
|
|
|
62
|
2 |
|
$response = call_user_func_array([$this, $method], $parameters); |
|
63
|
|
|
|
|
64
|
2 |
|
if ($response instanceof ResponseInterface) { |
|
65
|
|
|
$this->setResponse($response); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
$this->invokeStage('afterAction'); |
|
69
|
|
|
|
|
70
|
2 |
|
if ($this->hasResponse()) { |
|
|
|
|
|
|
71
|
1 |
|
return $this->getResponse(); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
return $this->getResponse(true); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param $action |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
2 |
|
protected function validAction($action) |
|
82
|
|
|
{ |
|
83
|
2 |
|
return in_array($action, get_class_methods(get_class($this))); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
2 |
|
public function getAction() |
|
90
|
|
|
{ |
|
91
|
2 |
|
return $this->action; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $action |
|
96
|
|
|
* @return self |
|
97
|
|
|
*/ |
|
98
|
2 |
|
public function setAction($action) |
|
99
|
|
|
{ |
|
100
|
2 |
|
$this->action = $action; |
|
101
|
|
|
|
|
102
|
2 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|