|
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 |
View Code Duplication |
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 |
|
$parmArray = $this->getParameterNames($result); |
|
65
|
|
|
|
|
66
|
2 |
|
return [$result, $parmArray]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
5 |
|
public function getMappings() |
|
70
|
|
|
{ |
|
71
|
|
|
// enforce we're actually hooked up to a controller |
|
72
|
5 |
|
assert($this instanceof Controller, get_class($this)); |
|
73
|
|
|
// enforce that mapping is actually not empty |
|
74
|
5 |
|
assert(0 < count($this->mapping), "Mapping array must not be empty"); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
5 |
|
$allMappings = []; |
|
77
|
|
|
|
|
78
|
|
|
// check that mapping array is well formed and sane, rather than waiting to stab us with a spatula |
|
79
|
5 |
|
foreach ($this->mapping as $key => $map) { |
|
80
|
5 |
|
if (!is_array($map)) { |
|
81
|
1 |
|
throw new \Exception('Metadata mapping for model '.$key.' not an array'); |
|
82
|
|
|
} |
|
83
|
4 |
|
foreach ($map as $verb => $method) { |
|
84
|
4 |
View Code Duplication |
if (!in_array(strtolower($verb), $this->crudVerbs)) { |
|
|
|
|
|
|
85
|
1 |
|
throw new \Exception('CRUD verb '.$verb.' not defined'); |
|
86
|
|
|
} |
|
87
|
3 |
|
if (!isset($method)) { |
|
88
|
1 |
|
throw new \Exception('Metadata mapping for CRUD verb '. $verb.' on model '.$key.' null'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
if (!method_exists($this, $method)) { |
|
92
|
1 |
|
throw new \Exception( |
|
93
|
1 |
|
'Metadata target for CRUD verb '. $verb.' on model '.$key.' does not exist' |
|
94
|
1 |
|
); |
|
95
|
|
|
} |
|
96
|
1 |
|
$parmArray = $this->getParameterNames($method); |
|
97
|
1 |
|
if (!array_key_exists($key, $allMappings)) { |
|
98
|
1 |
|
$allMappings[$key] = []; |
|
99
|
1 |
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
$allMappings[$key][$verb] = [ $method, $parmArray]; |
|
102
|
1 |
|
} |
|
103
|
1 |
|
} |
|
104
|
1 |
|
return $allMappings; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param $result |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
3 |
|
protected function getParameterNames($result) |
|
112
|
|
|
{ |
|
113
|
3 |
|
$parmArray = []; |
|
114
|
3 |
|
$r = new \ReflectionMethod($this, $result); |
|
115
|
3 |
|
$params = $r->getParameters(); |
|
116
|
3 |
|
foreach ($params as $parm) { |
|
117
|
3 |
|
$detail = []; |
|
118
|
3 |
|
$detail['class'] = get_class($this); |
|
119
|
3 |
|
$detail['name'] = $parm->getName(); |
|
|
|
|
|
|
120
|
3 |
|
if (null != $parm->getClass()) { |
|
121
|
2 |
|
$detail['type'] = $parm->getClass()->name; |
|
122
|
2 |
|
} |
|
123
|
|
|
|
|
124
|
3 |
|
$parmArray[] = $detail; |
|
125
|
|
|
|
|
126
|
3 |
|
} |
|
127
|
3 |
|
return $parmArray; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: