|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jasny\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Execute controller on given route |
|
9
|
|
|
*/ |
|
10
|
|
|
trait RouteAction |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Run the controller |
|
14
|
|
|
* |
|
15
|
|
|
* @return ResponseInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
public function run() { |
|
18
|
|
|
$request = $this->getRequest(); |
|
|
|
|
|
|
19
|
|
|
if (!$request) { |
|
20
|
|
|
throw new \RuntimeException("Request object is not set for controller"); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$route = $request->getAttribute('route'); |
|
24
|
|
|
$method = $this->getActionMethod(isset($route->action) ? $route->action : null); |
|
25
|
|
|
|
|
26
|
|
|
if (!method_exists($this, $method)) { |
|
27
|
|
|
throw new \RuntimeException("No method $method in conrtoller to route to"); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$args = isset($route->args) ? |
|
31
|
|
|
$route->args : |
|
32
|
|
|
$this->getFunctionArgs($route, new \ReflectionMethod($this, $method)); |
|
33
|
|
|
|
|
34
|
|
|
$response = call_user_func_array([$this, $method], $args); |
|
35
|
|
|
|
|
36
|
|
|
return $response ?: $this->getResponse(); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get the method name of the action |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $action |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function getActionMethod($action) |
|
46
|
|
|
{ |
|
47
|
|
|
return \Jasny\camelcase($action) . 'Action'; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get the arguments for a function from a route using reflection |
|
52
|
|
|
* |
|
53
|
|
|
* @param object $route |
|
54
|
|
|
* @param \ReflectionFunctionAbstract $refl |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function getFunctionArgs($route, \ReflectionFunctionAbstract $refl) |
|
58
|
|
|
{ |
|
59
|
|
|
$args = []; |
|
60
|
|
|
$params = $refl->getParameters(); |
|
61
|
|
|
|
|
62
|
|
|
foreach ($params as $param) { |
|
63
|
|
|
$key = $param->name; |
|
64
|
|
|
|
|
65
|
|
|
if (property_exists($route, $key)) { |
|
66
|
|
|
$value = $route->{$key}; |
|
67
|
|
|
} else { |
|
68
|
|
|
if (!$param->isOptional()) { |
|
69
|
|
|
$fn = $refl instanceof \ReflectionMethod |
|
70
|
|
|
? $refl->getDeclaringClass()->getName() . ':' . $refl->getName() |
|
|
|
|
|
|
71
|
|
|
: $refl->getName(); |
|
72
|
|
|
|
|
73
|
|
|
throw new \RuntimeException("Missing argument '$key' for $fn()"); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$value = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$args[$key] = $value; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $args; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.