1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drest\Mapping\Driver; |
4
|
|
|
|
5
|
|
|
use Drest\DrestException; |
6
|
|
|
use Drest\Mapping\ClassMetaData; |
7
|
|
|
use Drest\Mapping\RouteMetaData; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
abstract class AbstractDriver implements DriverInterface { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The paths to look for mapping files - immutable as classNames are cached, must be passed on construct. |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $paths; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Load metadata for the given class name |
20
|
|
|
* @param string $className |
21
|
|
|
* @return ClassMetadata |
22
|
|
|
*/ |
23
|
|
|
abstract public function loadMetadataForClass($className); |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
abstract protected function isDrestResource($className); |
27
|
|
|
|
28
|
|
|
|
29
|
67 |
|
public function __construct($paths = []) { |
30
|
67 |
|
$this->paths = (array) $paths; |
31
|
67 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Check handle calls. |
35
|
|
|
* @param array $routeMetaDataArray |
36
|
|
|
* @throws DrestException |
37
|
|
|
*/ |
38
|
39 |
|
public function checkHandleCalls($routeMetaDataArray) { |
39
|
|
|
// Error for any push metadata routes that don't have a handle |
40
|
39 |
|
foreach ($routeMetaDataArray as $routeMetaData) { |
41
|
|
|
/* @var RouteMetaData $routeMetaData */ |
42
|
39 |
|
if ($routeMetaData->needsHandleCall() && !$routeMetaData->hasHandleCall()) { |
43
|
|
|
throw DrestException::routeRequiresHandle($routeMetaData->getName()); |
44
|
|
|
} |
45
|
39 |
|
} |
46
|
39 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get paths to annotation classes |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getPaths() |
53
|
|
|
{ |
54
|
|
|
return $this->paths; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Process all routes defined |
59
|
|
|
* @param array $routes |
60
|
|
|
* @param ClassMetaData $metadata |
61
|
|
|
* @throws DrestException |
62
|
|
|
*/ |
63
|
47 |
|
protected function processRoutes(array $routes, ClassMetaData $metadata) |
64
|
|
|
{ |
65
|
47 |
|
$originFound = false; |
66
|
47 |
|
foreach ($routes as $route) { |
67
|
47 |
|
$routeMetaData = new RouteMetaData(); |
68
|
|
|
|
69
|
|
|
// Set name |
70
|
47 |
|
$route['name'] = preg_replace("/[^a-zA-Z0-9_\s]/", "", $route['name']); |
71
|
47 |
|
if ($route['name'] == '') { |
72
|
2 |
|
throw DrestException::routeNameIsEmpty(); |
73
|
|
|
} |
74
|
45 |
|
if ($metadata->getRouteMetaData($route['name']) !== false) { |
75
|
2 |
|
throw DrestException::routeAlreadyDefinedWithName($metadata->getClassName(), $route['name']); |
76
|
|
|
} |
77
|
45 |
|
$routeMetaData->setName($route['name']); |
78
|
|
|
|
79
|
|
|
// Set verbs (will throw if invalid) |
80
|
45 |
|
if (isset($route['verbs'])) { |
81
|
45 |
|
$routeMetaData->setVerbs($route['verbs']); |
82
|
43 |
|
} |
83
|
|
|
|
84
|
43 |
|
if (isset($route['collection'])) { |
85
|
34 |
|
$routeMetaData->setCollection($route['collection']); |
86
|
34 |
|
} |
87
|
|
|
|
88
|
|
|
// Add the route pattern |
89
|
43 |
|
$routeMetaData->setRoutePattern($route['routePattern']); |
90
|
|
|
|
91
|
43 |
|
if (isset($route['routeConditions']) && is_array($route['routeConditions'])) { |
92
|
36 |
|
$routeMetaData->setRouteConditions($route['routeConditions']); |
93
|
36 |
|
} |
94
|
|
|
|
95
|
|
|
// Set the exposure array |
96
|
43 |
|
if (isset($route['expose']) && is_array($route['expose'])) { |
97
|
35 |
|
$routeMetaData->setExpose($route['expose']); |
98
|
35 |
|
} |
99
|
|
|
|
100
|
|
|
// Set disable expose lookup |
101
|
43 |
|
if (isset($route['disableExpose'])) { |
102
|
|
|
$routeMetaData->setDisableExpose((bool) $route['disableExpose']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Set the allow options value |
106
|
43 |
|
if (isset($route['allowOptions'])) { |
107
|
|
|
$routeMetaData->setAllowedOptionRequest($route['allowOptions']); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// If the origin flag is set, set the name on the class meta data |
111
|
43 |
|
if (isset($route['origin']) && !is_null($route['origin'])) { |
112
|
32 |
|
if ($originFound) { |
113
|
|
|
throw DrestException::resourceCanOnlyHaveOneRouteSetAsOrigin(); |
114
|
|
|
} |
115
|
32 |
|
$metadata->originRouteName = $route['name']; |
116
|
32 |
|
$originFound = true; |
117
|
32 |
|
} |
118
|
|
|
|
119
|
43 |
|
$metadata->addRouteMetaData($routeMetaData); |
120
|
43 |
|
} |
121
|
|
|
} |
122
|
|
|
} |