1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlgoWeb\PODataLaravel\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Controller as BaseController; |
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 BaseController, 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 |
|
$this->checkCrudVerbDefined($crudVerb); |
42
|
1 |
|
|
43
|
|
|
$lookup = $this->mapping[$modelName]; |
44
|
|
|
if (!is_array($lookup)) { |
45
|
6 |
|
throw new \Exception('Metadata mapping for model '.$modelName.' not an array'); |
46
|
6 |
|
} |
47
|
1 |
|
|
48
|
|
|
if (!array_key_exists($crudVerb, $lookup)) { |
49
|
|
|
throw new \Exception('Metadata mapping for CRUD verb '.$crudVerb.' on model '.$modelName.' not defined'); |
50
|
5 |
|
} |
51
|
1 |
|
$result = $lookup[$crudVerb]; |
52
|
|
View Code Duplication |
if (!isset($result)) { |
53
|
4 |
|
throw new \Exception('Metadata mapping for CRUD verb '.$crudVerb.' on model '.$modelName.' null'); |
54
|
4 |
|
} |
55
|
1 |
|
|
56
|
|
View Code Duplication |
if (!method_exists($this, $result)) { |
57
|
|
|
throw new \Exception( |
58
|
3 |
|
'Metadata target for CRUD verb '.$crudVerb.' on model '.$modelName.' does not exist' |
59
|
1 |
|
); |
60
|
1 |
|
} |
61
|
1 |
|
|
62
|
|
|
$class = get_class($this); |
63
|
|
|
$parmArray = $this->getParameterNames($result); |
64
|
2 |
|
|
65
|
2 |
|
return ['method' => $result, 'controller' => $class, 'parameters' => $parmArray]; |
66
|
|
|
} |
67
|
2 |
|
|
68
|
|
|
public function getMappings() |
69
|
|
|
{ |
70
|
68 |
|
// enforce we're actually hooked up to a controller |
71
|
|
|
assert($this instanceof BaseController, get_class($this)); |
72
|
|
|
// enforce that mapping is actually not empty |
73
|
68 |
|
assert(!empty($this->mapping), 'Mapping array must not be empty'); |
74
|
|
|
|
75
|
68 |
|
$allMappings = []; |
76
|
|
|
|
77
|
68 |
|
// check that mapping array is well formed and sane, rather than waiting to stab us with a spatula |
78
|
|
|
foreach ($this->mapping as $key => $map) { |
79
|
|
|
if (!is_array($map)) { |
80
|
68 |
|
throw new \Exception('Metadata mapping for model '.$key.' not an array'); |
81
|
68 |
|
} |
82
|
1 |
|
foreach ($map as $verb => $method) { |
83
|
|
|
$this->checkCrudVerbDefined($verb); |
84
|
68 |
|
if (!isset($method)) { |
85
|
68 |
|
throw new \Exception('Metadata mapping for CRUD verb '.$verb.' on model '.$key.' null'); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
68 |
|
if (!method_exists($this, $method)) { |
89
|
1 |
|
throw new \Exception( |
90
|
|
|
'Metadata target for CRUD verb '.$verb.' on model '.$key.' does not exist' |
91
|
|
|
); |
92
|
68 |
|
} |
93
|
1 |
|
$parmArray = $this->getParameterNames($method); |
94
|
1 |
|
if (!array_key_exists($key, $allMappings)) { |
95
|
1 |
|
$allMappings[$key] = []; |
96
|
|
|
} |
97
|
68 |
|
|
98
|
68 |
|
$class = get_class($this); |
99
|
68 |
|
$allMappings[$key][$verb] = ['method' => $method, 'controller' => $class, 'parameters' => $parmArray]; |
100
|
68 |
|
} |
101
|
|
|
} |
102
|
68 |
|
return $allMappings; |
103
|
68 |
|
} |
104
|
68 |
|
|
105
|
68 |
|
/** |
106
|
68 |
|
* @param $result |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
protected function getParameterNames($result) |
110
|
|
|
{ |
111
|
|
|
$parmArray = []; |
112
|
|
|
$reflec = new \ReflectionMethod($this, $result); |
113
|
68 |
|
$params = $reflec->getParameters(); |
114
|
|
|
foreach ($params as $parm) { |
115
|
68 |
|
$detail = []; |
116
|
68 |
|
$detail['name'] = $parm->name; |
117
|
68 |
|
$classHint = $parm->getClass(); |
118
|
68 |
|
$isRequest = false; |
119
|
68 |
|
if (null != $classHint) { |
120
|
68 |
|
// check to see if this is a request |
121
|
68 |
|
$className = $classHint->name; |
122
|
68 |
|
$class = new $className(); |
123
|
68 |
|
$isRequest = $class instanceof \Illuminate\Http\Request; |
124
|
|
|
$detail['type'] = $className; |
125
|
68 |
|
} |
126
|
68 |
|
|
127
|
68 |
|
$detail['isRequest'] = $isRequest; |
128
|
68 |
|
$parmArray[$parm->name] = $detail; |
129
|
68 |
|
} |
130
|
|
|
return $parmArray; |
131
|
68 |
|
} |
132
|
68 |
|
|
133
|
|
|
/** |
134
|
68 |
|
* @param string $crudVerb |
135
|
68 |
|
* @throws \Exception |
136
|
|
|
*/ |
137
|
|
|
private function checkCrudVerbDefined($crudVerb) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
assert(is_string($crudVerb)); |
140
|
|
|
if (!in_array(strtolower($crudVerb), $this->crudVerbs)) { |
141
|
|
|
throw new \Exception('CRUD verb ' . $crudVerb . ' not defined'); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|