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
|
8 |
|
public function run() { |
18
|
8 |
|
$request = $this->getRequest(); |
|
|
|
|
19
|
8 |
|
if (!$request) { |
20
|
1 |
|
throw new \RuntimeException("Request object is not set for controller"); |
21
|
|
|
} |
22
|
|
|
|
23
|
7 |
|
$route = $request->getAttribute('route'); |
24
|
7 |
|
$method = $this->getActionMethod(isset($route->action) ? $route->action : null); |
25
|
|
|
|
26
|
7 |
|
if (!method_exists($this, $method)) { |
27
|
2 |
|
throw new \RuntimeException("No method $method in conrtoller to route to"); |
28
|
|
|
} |
29
|
|
|
|
30
|
5 |
|
$args = isset($route->args) ? |
31
|
1 |
|
$route->args : |
32
|
5 |
|
$this->getFunctionArgs($route, new \ReflectionMethod($this, $method)); |
33
|
|
|
|
34
|
3 |
|
$response = call_user_func_array([$this, $method], $args); |
35
|
|
|
|
36
|
3 |
|
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
|
7 |
|
protected function getActionMethod($action) |
46
|
|
|
{ |
47
|
7 |
|
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
|
4 |
|
protected function getFunctionArgs($route, \ReflectionFunctionAbstract $refl) |
58
|
|
|
{ |
59
|
4 |
|
$args = []; |
60
|
4 |
|
$params = $refl->getParameters(); |
61
|
|
|
|
62
|
4 |
|
foreach ($params as $param) { |
63
|
4 |
|
$key = $param->name; |
64
|
|
|
|
65
|
4 |
|
if (property_exists($route, $key)) { |
66
|
2 |
|
$value = $route->{$key}; |
67
|
|
|
} else { |
68
|
3 |
|
if (!$param->isOptional()) { |
69
|
2 |
|
$fn = $refl instanceof \ReflectionMethod |
70
|
2 |
|
? $refl->getDeclaringClass()->getName() . ':' . $refl->getName() |
|
|
|
|
71
|
2 |
|
: $refl->getName(); |
72
|
|
|
|
73
|
2 |
|
throw new \RuntimeException("Missing argument '$key' for $fn()"); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$value = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
$args[$key] = $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
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
Idable
provides a methodequalsId
that 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.