1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlgoWeb\PODataLaravel\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Controller; |
|
|
|
|
6
|
|
|
|
7
|
|
|
trait MetadataControllerTrait |
8
|
|
|
{ |
9
|
|
|
/* |
10
|
|
|
* Allowed crud verbs |
11
|
|
|
*/ |
12
|
|
|
protected $crudVerbs = ['create', 'read', 'update', 'delete']; |
13
|
|
|
|
14
|
|
|
/* |
15
|
|
|
* Array to record mapping betweeen model-verb combos and names |
16
|
|
|
* First-level key is fully-qualified model name |
17
|
|
|
* (eg Alt\Swedish\Chef\Bork\Bork\Bork) |
18
|
|
|
* Second-level key is CRUD verb |
19
|
|
|
*/ |
20
|
|
|
protected $mapping; |
21
|
|
|
|
22
|
|
|
/* |
23
|
|
|
* Given model and verb, get method name and parameter list |
24
|
|
|
* |
25
|
|
|
* @param $modelName |
26
|
|
|
* @param $crudVerb |
27
|
|
|
* @return array |
28
|
|
|
* @throws \Exception |
29
|
|
|
*/ |
30
|
9 |
|
public function getMethodName($modelName, $crudVerb) |
31
|
|
|
{ |
32
|
|
|
// enforce we're actually hooked up to a controller |
33
|
9 |
|
assert($this instanceof Controller, get_class($this)); |
34
|
|
|
// enforce that mapping is actually not empty |
35
|
9 |
|
assert(0 < count($this->mapping), "Mapping array must not be empty"); |
|
|
|
|
36
|
|
|
|
37
|
8 |
|
if (!array_key_exists($modelName, $this->mapping)) { |
38
|
1 |
|
throw new \Exception('Metadata mapping for model '.$modelName.' not defined'); |
39
|
|
|
} |
40
|
|
|
|
41
|
7 |
|
if (!in_array(strtolower($crudVerb), $this->crudVerbs)) { |
|
|
|
|
42
|
1 |
|
throw new \Exception('CRUD verb '.$crudVerb.' not defined'); |
43
|
|
|
} |
44
|
|
|
|
45
|
6 |
|
$lookup = $this->mapping[$modelName]; |
46
|
6 |
|
if (!is_array($lookup)) { |
47
|
1 |
|
throw new \Exception('Metadata mapping for model '.$modelName.' not an array'); |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
if (!array_key_exists($crudVerb, $lookup)) { |
51
|
1 |
|
throw new \Exception('Metadata mapping for CRUD verb '. $crudVerb.' on model '.$modelName.' not defined'); |
52
|
|
|
} |
53
|
4 |
|
$result = $lookup[$crudVerb]; |
54
|
4 |
View Code Duplication |
if (!isset($result)) { |
|
|
|
|
55
|
1 |
|
throw new \Exception('Metadata mapping for CRUD verb '. $crudVerb.' on model '.$modelName.' null'); |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
View Code Duplication |
if (!method_exists($this, $result)) { |
|
|
|
|
59
|
1 |
|
throw new \Exception( |
60
|
1 |
|
'Metadata target for CRUD verb '. $crudVerb.' on model '.$modelName.' does not exist' |
61
|
1 |
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
$class = get_class($this); |
65
|
2 |
|
$parmArray = $this->getParameterNames($result); |
66
|
|
|
|
67
|
2 |
|
return [ 'method' => $result, 'controller' => $class, 'parameters' => $parmArray]; |
68
|
|
|
} |
69
|
|
|
|
70
|
8 |
|
public function getMappings() |
71
|
|
|
{ |
72
|
|
|
// enforce we're actually hooked up to a controller |
73
|
8 |
|
assert($this instanceof Controller, get_class($this)); |
74
|
|
|
// enforce that mapping is actually not empty |
75
|
8 |
|
assert(0 < count($this->mapping), "Mapping array must not be empty"); |
|
|
|
|
76
|
|
|
|
77
|
8 |
|
$allMappings = []; |
78
|
|
|
|
79
|
|
|
// check that mapping array is well formed and sane, rather than waiting to stab us with a spatula |
80
|
8 |
|
foreach ($this->mapping as $key => $map) { |
81
|
8 |
|
if (!is_array($map)) { |
82
|
1 |
|
throw new \Exception('Metadata mapping for model '.$key.' not an array'); |
83
|
|
|
} |
84
|
7 |
|
foreach ($map as $verb => $method) { |
85
|
7 |
|
if (!in_array(strtolower($verb), $this->crudVerbs)) { |
|
|
|
|
86
|
1 |
|
throw new \Exception('CRUD verb '.$verb.' not defined'); |
87
|
|
|
} |
88
|
6 |
|
if (!isset($method)) { |
89
|
1 |
|
throw new \Exception('Metadata mapping for CRUD verb '. $verb.' on model '.$key.' null'); |
90
|
|
|
} |
91
|
|
|
|
92
|
5 |
|
if (!method_exists($this, $method)) { |
93
|
1 |
|
throw new \Exception( |
94
|
1 |
|
'Metadata target for CRUD verb '. $verb.' on model '.$key.' does not exist' |
95
|
1 |
|
); |
96
|
|
|
} |
97
|
4 |
|
$parmArray = $this->getParameterNames($method); |
98
|
4 |
|
if (!array_key_exists($key, $allMappings)) { |
99
|
4 |
|
$allMappings[$key] = []; |
100
|
4 |
|
} |
101
|
|
|
|
102
|
4 |
|
$class = get_class($this); |
103
|
4 |
|
$allMappings[$key][$verb] = [ 'method' => $method, 'controller' => $class, 'parameters' => $parmArray]; |
104
|
4 |
|
} |
105
|
4 |
|
} |
106
|
4 |
|
return $allMappings; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $result |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
6 |
|
protected function getParameterNames($result) |
114
|
|
|
{ |
115
|
6 |
|
$parmArray = []; |
116
|
6 |
|
$r = new \ReflectionMethod($this, $result); |
117
|
6 |
|
$params = $r->getParameters(); |
118
|
6 |
|
foreach ($params as $parm) { |
119
|
6 |
|
$detail = []; |
120
|
6 |
|
$detail['name'] = $parm->getName(); |
|
|
|
|
121
|
6 |
|
$classHint = $parm->getClass(); |
122
|
6 |
|
$isRequest = false; |
123
|
6 |
|
if (null != $classHint) { |
124
|
|
|
// check to see if this is a request |
125
|
5 |
|
$className = $classHint->name; |
126
|
5 |
|
$class = new $className(); |
127
|
5 |
|
$isRequest = $class instanceof \Illuminate\Http\Request; |
128
|
5 |
|
$detail['type'] = $className; |
129
|
5 |
|
} |
130
|
|
|
|
131
|
6 |
|
$detail['isRequest'] = $isRequest; |
132
|
6 |
|
$parmArray[] = $detail; |
133
|
|
|
|
134
|
6 |
|
} |
135
|
6 |
|
return $parmArray; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: