1 | <?php |
||
11 | trait RouteAction |
||
12 | { |
||
13 | /** |
||
14 | * @var boolean |
||
15 | */ |
||
16 | protected $actionCancelled = false; |
||
17 | |||
18 | |||
19 | /** |
||
20 | * Get request, set for controller |
||
21 | * |
||
22 | * @return ServerRequestInterface |
||
23 | */ |
||
24 | abstract public function getRequest(); |
||
25 | |||
26 | /** |
||
27 | * Get response. set for controller |
||
28 | * |
||
29 | * @return ResponseInterface |
||
30 | */ |
||
31 | abstract public function getResponse(); |
||
32 | |||
33 | /** |
||
34 | * Respond with a server error |
||
35 | * |
||
36 | * @param string $message |
||
37 | * @param int $code HTTP status code |
||
38 | */ |
||
39 | abstract public function notFound($message = '', $code = 404); |
||
40 | |||
41 | |||
42 | /** |
||
43 | * Get the route |
||
44 | * |
||
45 | * @return \stdClass |
||
46 | */ |
||
47 | 13 | protected function getRoute() |
|
48 | { |
||
49 | 13 | $route = $this->getRequest()->getAttribute('route'); |
|
50 | |||
51 | 13 | if (!isset($route)) { |
|
52 | 1 | throw new \LogicException("Route has not been set"); |
|
53 | } |
||
54 | |||
55 | 12 | if (is_array($route)) { |
|
56 | 1 | $route = (object)$route; |
|
57 | } |
||
58 | |||
59 | 12 | if (!$route instanceof \stdClass) { |
|
60 | 1 | $type = (is_object($route) ? get_class($route) . ' ' : '') . gettype($route); |
|
61 | 1 | throw new \UnexpectedValueException("Expected route to be a stdClass object, not a $type"); |
|
62 | } |
||
63 | |||
64 | 11 | return $route; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Get the method name of the action |
||
69 | * |
||
70 | * @param string $action |
||
71 | * @return string |
||
72 | */ |
||
73 | 11 | protected function getActionMethod($action) |
|
77 | |||
78 | /** |
||
79 | * Called before executing the action. |
||
80 | * @codeCoverageIgnore |
||
81 | * |
||
82 | * <code> |
||
83 | * protected function beforeAction() |
||
84 | * { |
||
85 | * $this->respondWith('json'); // Respond with JSON by default |
||
86 | * |
||
87 | * if ($this->auth->getUser()->getCredits() <= 0) { |
||
88 | * $this->paymentRequired(); |
||
89 | * } |
||
90 | * } |
||
91 | * </code> |
||
92 | */ |
||
93 | protected function before() |
||
96 | |||
97 | /** |
||
98 | * Called before executing the action. |
||
99 | * @codeCoverageIgnore |
||
100 | */ |
||
101 | protected function after() |
||
104 | |||
105 | /** |
||
106 | * Cancel the action |
||
107 | */ |
||
108 | 1 | public function cancel() |
|
112 | |||
113 | /** |
||
114 | * Check if the action is cancelled |
||
115 | * |
||
116 | * @return boolean |
||
117 | */ |
||
118 | 10 | public function isCancelled() |
|
122 | |||
123 | /** |
||
124 | * Run the controller |
||
125 | * |
||
126 | * @return ResponseInterface |
||
127 | */ |
||
128 | 13 | public function run() |
|
148 | |||
149 | /** |
||
150 | * Get the arguments for a function from a route using reflection |
||
151 | * |
||
152 | * @param \stdClass $route |
||
153 | * @param \ReflectionFunctionAbstract $refl |
||
154 | * @return array |
||
155 | */ |
||
156 | 6 | protected function getFunctionArgs($route, \ReflectionFunctionAbstract $refl) |
|
180 | } |
||
181 | |||
182 |
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.