|
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
|
|
|
* Get paths to annotation classes |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getPaths() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->paths; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Process all routes defined |
|
44
|
|
|
* @param array $routes |
|
45
|
|
|
* @param ClassMetaData $metadata |
|
46
|
|
|
* @throws DrestException |
|
47
|
|
|
*/ |
|
48
|
47 |
|
protected function processRoutes(array $routes, ClassMetaData $metadata) |
|
49
|
|
|
{ |
|
50
|
47 |
|
$originFound = false; |
|
51
|
47 |
|
foreach ($routes as $route) { |
|
52
|
47 |
|
$routeMetaData = new RouteMetaData(); |
|
53
|
|
|
|
|
54
|
|
|
// Set name |
|
55
|
47 |
|
$route['name'] = preg_replace("/[^a-zA-Z0-9_\s]/", "", $route['name']); |
|
56
|
47 |
|
if ($route['name'] == '') { |
|
57
|
2 |
|
throw DrestException::routeNameIsEmpty(); |
|
58
|
|
|
} |
|
59
|
45 |
|
if ($metadata->getRouteMetaData($route['name']) !== false) { |
|
60
|
2 |
|
throw DrestException::routeAlreadyDefinedWithName($metadata->getClassName(), $route['name']); |
|
61
|
|
|
} |
|
62
|
45 |
|
$routeMetaData->setName($route['name']); |
|
63
|
|
|
|
|
64
|
|
|
// Set verbs (will throw if invalid) |
|
65
|
45 |
|
if (isset($route['verbs'])) { |
|
66
|
45 |
|
$routeMetaData->setVerbs($route['verbs']); |
|
67
|
43 |
|
} |
|
68
|
|
|
|
|
69
|
43 |
|
if (isset($route['collection'])) { |
|
70
|
34 |
|
$routeMetaData->setCollection($route['collection']); |
|
71
|
34 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Add the route pattern |
|
74
|
43 |
|
$routeMetaData->setRoutePattern($route['routePattern']); |
|
75
|
|
|
|
|
76
|
43 |
|
if (isset($route['routeConditions']) && is_array($route['routeConditions'])) { |
|
77
|
36 |
|
$routeMetaData->setRouteConditions($route['routeConditions']); |
|
78
|
36 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
// Set the exposure array |
|
81
|
43 |
|
if (isset($route['expose']) && is_array($route['expose'])) { |
|
82
|
35 |
|
$routeMetaData->setExpose($route['expose']); |
|
83
|
35 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
// Set disable expose lookup |
|
86
|
43 |
|
if (isset($route['disableExpose'])) { |
|
87
|
|
|
$routeMetaData->setDisableExpose((bool) $route['disableExpose']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Set the allow options value |
|
91
|
43 |
|
if (isset($route['allowOptions'])) { |
|
92
|
|
|
$routeMetaData->setAllowedOptionRequest($route['allowOptions']); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// If the origin flag is set, set the name on the class meta data |
|
96
|
43 |
|
if (isset($route['origin']) && !is_null($route['origin'])) { |
|
97
|
32 |
|
if ($originFound) { |
|
98
|
|
|
throw DrestException::resourceCanOnlyHaveOneRouteSetAsOrigin(); |
|
99
|
|
|
} |
|
100
|
32 |
|
$metadata->originRouteName = $route['name']; |
|
101
|
32 |
|
$originFound = true; |
|
102
|
32 |
|
} |
|
103
|
|
|
|
|
104
|
43 |
|
$metadata->addRouteMetaData($routeMetaData); |
|
105
|
43 |
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |