1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSRestBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\RestBundle\Routing\Loader; |
13
|
|
|
|
14
|
|
|
use FOS\RestBundle\Routing\RestRouteCollection; |
15
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
16
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
17
|
|
|
use Symfony\Component\Routing\Loader\YamlFileLoader; |
18
|
|
|
use Symfony\Component\Routing\RouteCollection; |
19
|
|
|
use Symfony\Component\Yaml\Yaml; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* RestYamlCollectionLoader YAML file collections loader. |
23
|
|
|
*/ |
24
|
|
|
class RestYamlCollectionLoader extends YamlFileLoader |
25
|
|
|
{ |
26
|
|
|
protected $collectionParents = []; |
27
|
|
|
private $processor; |
28
|
|
|
private $includeFormat; |
29
|
|
|
private $formats; |
30
|
|
|
private $defaultFormat; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initializes yaml loader. |
34
|
|
|
* |
35
|
|
|
* @param FileLocatorInterface $locator |
36
|
|
|
* @param RestRouteProcessor $processor |
37
|
|
|
* @param bool $includeFormat |
38
|
|
|
* @param string[] $formats |
39
|
|
|
* @param string $defaultFormat |
40
|
|
|
*/ |
41
|
23 |
|
public function __construct( |
42
|
|
|
FileLocatorInterface $locator, |
43
|
|
|
RestRouteProcessor $processor, |
44
|
|
|
$includeFormat = true, |
45
|
|
|
array $formats = [], |
46
|
|
|
$defaultFormat = null |
47
|
|
|
) { |
48
|
23 |
|
parent::__construct($locator); |
49
|
|
|
|
50
|
23 |
|
$this->processor = $processor; |
51
|
23 |
|
$this->includeFormat = $includeFormat; |
52
|
23 |
|
$this->formats = $formats; |
53
|
23 |
|
$this->defaultFormat = $defaultFormat; |
54
|
23 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
11 |
|
public function load($file, $type = null) |
60
|
|
|
{ |
61
|
11 |
|
$path = $this->locator->locate($file); |
62
|
|
|
|
63
|
11 |
|
$config = Yaml::parse(file_get_contents($path)); |
64
|
|
|
|
65
|
11 |
|
$collection = new RouteCollection(); |
66
|
11 |
|
$collection->addResource(new FileResource($path)); |
|
|
|
|
67
|
|
|
|
68
|
|
|
// process routes and imports |
69
|
11 |
|
foreach ($config as $name => $config) { |
70
|
11 |
|
if (isset($config['resource'])) { |
71
|
7 |
|
$resource = $config['resource']; |
72
|
7 |
|
$prefix = isset($config['prefix']) ? $config['prefix'] : null; |
73
|
7 |
|
$namePrefix = isset($config['name_prefix']) ? $config['name_prefix'] : null; |
74
|
7 |
|
$parent = isset($config['parent']) ? $config['parent'] : null; |
75
|
7 |
|
$type = isset($config['type']) ? $config['type'] : null; |
76
|
7 |
|
$requirements = isset($config['requirements']) ? $config['requirements'] : []; |
77
|
7 |
|
$defaults = isset($config['defaults']) ? $config['defaults'] : []; |
78
|
7 |
|
$options = isset($config['options']) ? $config['options'] : []; |
79
|
7 |
|
$currentDir = dirname($path); |
80
|
|
|
|
81
|
7 |
|
$parents = []; |
82
|
7 |
View Code Duplication |
if (!empty($parent)) { |
|
|
|
|
83
|
3 |
|
if (!isset($this->collectionParents[$parent])) { |
84
|
|
|
throw new \InvalidArgumentException(sprintf('Cannot find parent resource with name %s', $parent)); |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
$parents = $this->collectionParents[$parent]; |
88
|
3 |
|
} |
89
|
|
|
|
90
|
7 |
|
$imported = $this->processor->importResource($this, $resource, $parents, $prefix, $namePrefix, $type, $currentDir); |
91
|
|
|
|
92
|
7 |
|
if ($imported instanceof RestRouteCollection) { |
93
|
7 |
|
$parents[] = ($prefix ? $prefix.'/' : '').$imported->getSingularName(); |
94
|
7 |
|
$prefix = null; |
95
|
7 |
|
$namePrefix = null; |
96
|
|
|
|
97
|
7 |
|
$this->collectionParents[$name] = $parents; |
98
|
7 |
|
} |
99
|
|
|
|
100
|
7 |
|
$imported->addRequirements($requirements); |
101
|
7 |
|
$imported->addDefaults($defaults); |
102
|
7 |
|
$imported->addOptions($options); |
103
|
|
|
|
104
|
7 |
|
$imported->addPrefix($prefix); |
105
|
|
|
|
106
|
|
|
// Add name prefix from parent config files |
107
|
7 |
|
$imported = $this->addParentNamePrefix($imported, $namePrefix); |
108
|
|
|
|
109
|
7 |
|
$collection->addCollection($imported); |
110
|
11 |
|
} elseif (isset($config['pattern']) || isset($config['path'])) { |
111
|
|
|
// the YamlFileLoader of the Routing component only checks for |
112
|
|
|
// the path option |
113
|
4 |
|
if (!isset($config['path'])) { |
114
|
|
|
$config['path'] = $config['pattern']; |
115
|
|
|
|
116
|
|
|
@trigger_error(sprintf('The "pattern" option at "%s" in file "%s" is deprecated. Use the "path" option instead.', $name, $path), E_USER_DEPRECATED); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
4 |
|
if ($this->includeFormat) { |
120
|
|
|
// append format placeholder if not present |
121
|
3 |
|
if (false === strpos($config['path'], '{_format}')) { |
122
|
3 |
|
$config['path'] .= '.{_format}'; |
123
|
3 |
|
} |
124
|
|
|
|
125
|
|
|
// set format requirement if configured globally |
126
|
3 |
View Code Duplication |
if (!isset($config['requirements']['_format']) && !empty($this->formats)) { |
|
|
|
|
127
|
3 |
|
$config['requirements']['_format'] = implode('|', array_keys($this->formats)); |
128
|
3 |
|
} |
129
|
3 |
|
} |
130
|
|
|
|
131
|
|
|
// set the default format if configured |
132
|
4 |
|
if (null !== $this->defaultFormat) { |
133
|
1 |
|
$config['defaults']['_format'] = $this->defaultFormat; |
134
|
1 |
|
} |
135
|
|
|
|
136
|
4 |
|
$this->parseRoute($collection, $name, $config, $path); |
|
|
|
|
137
|
4 |
|
} else { |
138
|
|
|
throw new \InvalidArgumentException(sprintf('Unable to parse the "%s" route.', $name)); |
139
|
|
|
} |
140
|
11 |
|
} |
141
|
|
|
|
142
|
11 |
|
return $collection; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
7 |
|
public function supports($resource, $type = null) |
149
|
|
|
{ |
150
|
7 |
|
return is_string($resource) && |
151
|
7 |
|
'yml' === pathinfo($resource, PATHINFO_EXTENSION) && |
152
|
7 |
|
'rest' === $type; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Adds a name prefix to the route name of all collection routes. |
157
|
|
|
* |
158
|
|
|
* @param RouteCollection $collection Route collection |
159
|
|
|
* @param array $namePrefix NamePrefix to add in each route name of the route collection |
160
|
|
|
* |
161
|
|
|
* @return RouteCollection |
162
|
|
|
*/ |
163
|
7 |
|
public function addParentNamePrefix(RouteCollection $collection, $namePrefix) |
164
|
|
|
{ |
165
|
7 |
|
if (!isset($namePrefix) || ($namePrefix = trim($namePrefix)) === '') { |
166
|
7 |
|
return $collection; |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
$iterator = $collection->getIterator(); |
170
|
|
|
|
171
|
1 |
|
foreach ($iterator as $key1 => $route1) { |
172
|
1 |
|
$collection->add($namePrefix.$key1, $route1); |
173
|
1 |
|
$collection->remove($key1); |
174
|
1 |
|
} |
175
|
|
|
|
176
|
1 |
|
return $collection; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.