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