This commit is contained in:
1 parent
f7c532a648
commit
640bd67556
5801 files changed
+484532
-55857
No files matched your search
+42
-43
@@ -46,11 +46,11 @@ class YamlFileLoader extends FileLoader
|
||||
$path = $this->locator->locate($file);
|
||||
|
||||
if (!stream_is_local($path)) {
|
||||
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path));
|
||||
throw new \InvalidArgumentException(\sprintf('This is not a local file "%s".', $path));
|
||||
}
|
||||
|
||||
if (!file_exists($path)) {
|
||||
throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path));
|
||||
throw new \InvalidArgumentException(\sprintf('File "%s" not found.', $path));
|
||||
}
|
||||
|
||||
$this->yamlParser ??= new YamlParser();
|
||||
@@ -58,7 +58,7 @@ class YamlFileLoader extends FileLoader
|
||||
try {
|
||||
$parsedConfig = $this->yamlParser->parseFile($path, Yaml::PARSE_CONSTANT);
|
||||
} catch (ParseException $e) {
|
||||
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML: ', $path).$e->getMessage(), 0, $e);
|
||||
throw new \InvalidArgumentException(\sprintf('The file "%s" does not contain valid YAML: ', $path).$e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
$collection = new RouteCollection();
|
||||
@@ -71,36 +71,10 @@ class YamlFileLoader extends FileLoader
|
||||
|
||||
// not an array
|
||||
if (!\is_array($parsedConfig)) {
|
||||
throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
|
||||
throw new \InvalidArgumentException(\sprintf('The file "%s" must contain a YAML array.', $path));
|
||||
}
|
||||
|
||||
foreach ($parsedConfig as $name => $config) {
|
||||
if (str_starts_with($name, 'when@')) {
|
||||
if (!$this->env || 'when@'.$this->env !== $name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($config as $name => $config) {
|
||||
$this->validate($config, $name.'" when "@'.$this->env, $path);
|
||||
|
||||
if (isset($config['resource'])) {
|
||||
$this->parseImport($collection, $config, $path, $file);
|
||||
} else {
|
||||
$this->parseRoute($collection, $name, $config, $path);
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->validate($config, $name, $path);
|
||||
|
||||
if (isset($config['resource'])) {
|
||||
$this->parseImport($collection, $config, $path, $file);
|
||||
} else {
|
||||
$this->parseRoute($collection, $name, $config, $path);
|
||||
}
|
||||
}
|
||||
$this->loadContent($collection, $parsedConfig, $path, $file);
|
||||
|
||||
return $collection;
|
||||
}
|
||||
@@ -135,7 +109,7 @@ class YamlFileLoader extends FileLoader
|
||||
|
||||
foreach ($requirements as $placeholder => $requirement) {
|
||||
if (\is_int($placeholder)) {
|
||||
throw new \InvalidArgumentException(sprintf('A placeholder name must be a string (%d given). Did you forget to specify the placeholder key for the requirement "%s" of route "%s" in "%s"?', $placeholder, $requirement, $name, $path));
|
||||
throw new \InvalidArgumentException(\sprintf('A placeholder name must be a string (%d given). Did you forget to specify the placeholder key for the requirement "%s" of route "%s" in "%s"?', $placeholder, $requirement, $name, $path));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +129,7 @@ class YamlFileLoader extends FileLoader
|
||||
$defaults['_stateless'] = $config['stateless'];
|
||||
}
|
||||
|
||||
$routes = $this->createLocalizedRoute($collection, $name, $config['path']);
|
||||
$routes = $this->createLocalizedRoute(new RouteCollection(), $name, $config['path']);
|
||||
$routes->addDefaults($defaults);
|
||||
$routes->addRequirements($requirements);
|
||||
$routes->addOptions($options);
|
||||
@@ -166,6 +140,8 @@ class YamlFileLoader extends FileLoader
|
||||
if (isset($config['host'])) {
|
||||
$this->addHost($routes, $config['host']);
|
||||
}
|
||||
|
||||
$collection->addCollection($routes);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -244,7 +220,7 @@ class YamlFileLoader extends FileLoader
|
||||
protected function validate(mixed $config, string $name, string $path): void
|
||||
{
|
||||
if (!\is_array($config)) {
|
||||
throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
|
||||
throw new \InvalidArgumentException(\sprintf('The definition of "%s" in "%s" must be an array.', $name, $path));
|
||||
}
|
||||
if (isset($config['alias'])) {
|
||||
$this->validateAlias($config, $name, $path);
|
||||
@@ -252,22 +228,45 @@ class YamlFileLoader extends FileLoader
|
||||
return;
|
||||
}
|
||||
if ($extraKeys = array_diff(array_keys($config), self::AVAILABLE_KEYS)) {
|
||||
throw new \InvalidArgumentException(sprintf('The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".', $path, $name, implode('", "', $extraKeys), implode('", "', self::AVAILABLE_KEYS)));
|
||||
throw new \InvalidArgumentException(\sprintf('The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".', $path, $name, implode('", "', $extraKeys), implode('", "', self::AVAILABLE_KEYS)));
|
||||
}
|
||||
if (isset($config['resource']) && isset($config['path'])) {
|
||||
throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "resource" key and the "path" key for "%s". Choose between an import and a route definition.', $path, $name));
|
||||
throw new \InvalidArgumentException(\sprintf('The routing file "%s" must not specify both the "resource" key and the "path" key for "%s". Choose between an import and a route definition.', $path, $name));
|
||||
}
|
||||
if (!isset($config['resource']) && isset($config['type'])) {
|
||||
throw new \InvalidArgumentException(sprintf('The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.', $name, $path));
|
||||
throw new \InvalidArgumentException(\sprintf('The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.', $name, $path));
|
||||
}
|
||||
if (!isset($config['resource']) && !isset($config['path'])) {
|
||||
throw new \InvalidArgumentException(sprintf('You must define a "path" for the route "%s" in file "%s".', $name, $path));
|
||||
throw new \InvalidArgumentException(\sprintf('You must define a "path" for the route "%s" in file "%s".', $name, $path));
|
||||
}
|
||||
if (isset($config['controller']) && isset($config['defaults']['_controller'])) {
|
||||
throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "controller" key and the defaults key "_controller" for "%s".', $path, $name));
|
||||
throw new \InvalidArgumentException(\sprintf('The routing file "%s" must not specify both the "controller" key and the defaults key "_controller" for "%s".', $path, $name));
|
||||
}
|
||||
if (isset($config['stateless']) && isset($config['defaults']['_stateless'])) {
|
||||
throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "stateless" key and the defaults key "_stateless" for "%s".', $path, $name));
|
||||
throw new \InvalidArgumentException(\sprintf('The routing file "%s" must not specify both the "stateless" key and the defaults key "_stateless" for "%s".', $path, $name));
|
||||
}
|
||||
}
|
||||
|
||||
private function loadContent(RouteCollection $collection, array $config, string $path, string $file): void
|
||||
{
|
||||
foreach ($config as $name => $config) {
|
||||
if (!str_starts_with($when = $name, 'when@')) {
|
||||
$config = [$name => $config];
|
||||
} elseif (!$this->env || 'when@'.$this->env !== $name) {
|
||||
continue;
|
||||
} else {
|
||||
$when .= '" when "@'.$this->env;
|
||||
}
|
||||
|
||||
foreach ($config as $name => $config) {
|
||||
$this->validate($config, $when, $path);
|
||||
|
||||
if (isset($config['resource'])) {
|
||||
$this->parseImport($collection, $config, $path, $file);
|
||||
} else {
|
||||
$this->parseRoute($collection, $name, $config, $path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,16 +278,16 @@ class YamlFileLoader extends FileLoader
|
||||
{
|
||||
foreach ($config as $key => $value) {
|
||||
if (!\in_array($key, ['alias', 'deprecated'], true)) {
|
||||
throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify other keys than "alias" and "deprecated" for "%s".', $path, $name));
|
||||
throw new \InvalidArgumentException(\sprintf('The routing file "%s" must not specify other keys than "alias" and "deprecated" for "%s".', $path, $name));
|
||||
}
|
||||
|
||||
if ('deprecated' === $key) {
|
||||
if (!isset($value['package'])) {
|
||||
throw new \InvalidArgumentException(sprintf('The routing file "%s" must specify the attribute "package" of the "deprecated" option for "%s".', $path, $name));
|
||||
throw new \InvalidArgumentException(\sprintf('The routing file "%s" must specify the attribute "package" of the "deprecated" option for "%s".', $path, $name));
|
||||
}
|
||||
|
||||
if (!isset($value['version'])) {
|
||||
throw new \InvalidArgumentException(sprintf('The routing file "%s" must specify the attribute "version" of the "deprecated" option for "%s".', $path, $name));
|
||||
throw new \InvalidArgumentException(\sprintf('The routing file "%s" must specify the attribute "version" of the "deprecated" option for "%s".', $path, $name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user