+ */
+class YamlFileLoader extends BaseYamlFileLoader
+{
+ private static $availableKeys = [
+ 'template' => ['context', 'max_age', 'shared_max_age', 'private'],
+ 'redirect_to_route' => ['permanent', 'ignore_attributes', 'keep_request_method', 'keep_query_params'],
+ 'redirect_to_url' => ['permanent', 'scheme', 'http_port', 'https_port', 'keep_request_method'],
+ 'gone' => ['permanent'],
+ ];
+
+ protected function validate($config, $name, $path)
+ {
+ if (\count($types = array_intersect_key($config, self::$availableKeys)) > 1) {
+ throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify only one route type among "%s" keys for "%s".', str_replace('/', \DIRECTORY_SEPARATOR, $path), implode('", "', array_keys($types)), $name));
+ }
+
+ foreach (self::$availableKeys as $routeType => $availableKeys) {
+ if (!isset($config[$routeType])) {
+ continue;
+ }
+
+ if (isset($config['controller'])) {
+ throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "controller" and the "%s" keys for "%s".', str_replace('/', \DIRECTORY_SEPARATOR, $path), $routeType, $name));
+ }
+
+ // keys would be invalid for parent::validate(), but we use them below
+ unset($config[$routeType]);
+ foreach ($availableKeys as $key) {
+ unset($config[$key]);
+ }
+ }
+
+ parent::validate($config, $name, $path);
+ }
+
+ protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
+ {
+ if (isset($config['template'])) {
+ $config['defaults'] = array_merge($config['defaults'] ?? [], [
+ '_controller' => TemplateController::class,
+ 'template' => $config['template'],
+ 'context' => $config['context'] ?? [],
+ 'maxAge' => $config['max_age'] ?? null,
+ 'sharedAge' => $config['shared_max_age'] ?? null,
+ 'private' => $config['private'] ?? null,
+ ]);
+ } elseif (isset($config['redirect_to_route'])) {
+ $config['defaults'] = array_merge($config['defaults'] ?? [], [
+ '_controller' => RedirectController::class.'::redirectAction',
+ 'route' => $config['redirect_to_route'],
+ 'permanent' => $config['permanent'] ?? false,
+ 'ignoreAttributes' => $config['ignore_attributes'] ?? false,
+ 'keepRequestMethod' => $config['keep_request_method'] ?? false,
+ 'keepQueryParams' => $config['keep_query_params'] ?? false,
+ ]);
+ } elseif (isset($config['redirect_to_url'])) {
+ $config['defaults'] = array_merge($config['defaults'] ?? [], [
+ '_controller' => RedirectController::class.'::urlRedirectAction',
+ 'path' => $config['redirect_to_url'],
+ 'permanent' => $config['permanent'] ?? false,
+ 'scheme' => $config['scheme'] ?? null,
+ 'keepRequestMethod' => $config['keep_request_method'] ?? false,
+ ]);
+
+ if (\array_key_exists('http_port', $config)) {
+ $config['defaults']['httpPort'] = (int) $config['http_port'] ?: null;
+ } elseif (\array_key_exists('http_port', $config)) {
+ $config['defaults']['httpsPort'] = (int) $config['https_port'] ?: null;
+ }
+ } elseif (isset($config['gone'])) {
+ $config['defaults'] = array_merge($config['defaults'] ?? [], [
+ '_controller' => RedirectController::class.'::redirectAction',
+ 'route' => '',
+ ]);
+
+ if (isset($config['permanent'])) {
+ $config['defaults']['permanent'] = $config['permanent'];
+ }
+ }
+
+ parent::parseRoute($collection, $name, $config, $path);
+ }
+}
diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/config/routing/routes.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/config/routing/routes.xml
new file mode 100644
index 0000000000000..6392fc5d4a08f
--- /dev/null
+++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/config/routing/routes.xml
@@ -0,0 +1,54 @@
+
+
+
+
+
+
+
+ bar
+
+ true
+ abc
+
+
+
+ true
+
+
+
+ true
+
+
+
+ true
+
+
+
+ true
+
+
+
diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/config/routing/routes.yaml b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/config/routing/routes.yaml
new file mode 100644
index 0000000000000..6369855bc57b0
--- /dev/null
+++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/config/routing/routes.yaml
@@ -0,0 +1,47 @@
+classic_route:
+ path: /classic
+
+template_route:
+ path: /static
+ template: static.html.twig
+ context:
+ foo: bar
+ max_age: 300
+ shared_max_age: 100
+ private: true
+ methods: GET
+ options: { utf8: true }
+ condition: abc
+
+redirect_route:
+ path: /redirect
+ redirect_to_route: target_route
+ permanent: true
+ ignore_attributes: ['attr', 'ibutes']
+ keep_request_method: true
+ keep_query_params: true
+ schemes: http
+ host: legacy
+ options: { utf8: true }
+
+url_redirect_route:
+ path: /redirect-url
+ redirect_to_url: /url-target
+ permanent: true
+ scheme: http
+ http_port: 1
+ keep_request_method: true
+ host: legacy
+ options: { utf8: true }
+
+not_a_route:
+ path: /not-a-path
+ gone: true
+ host: legacy
+ options: { utf8: true }
+
+gone_route:
+ path: /gone-path
+ gone: true
+ permanent: true
+ options: { utf8: true }
diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/config/routing/template_and_redirect.yaml b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/config/routing/template_and_redirect.yaml
new file mode 100644
index 0000000000000..50c2300ee698a
--- /dev/null
+++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/config/routing/template_and_redirect.yaml
@@ -0,0 +1,4 @@
+invalid_route:
+ path: '/path'
+ template: 'template.html.twig'
+ redirect_to_route: 'target_route'
diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/config/routing/with_controller_attribute.yaml b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/config/routing/with_controller_attribute.yaml
new file mode 100644
index 0000000000000..7dc728d5571b4
--- /dev/null
+++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/config/routing/with_controller_attribute.yaml
@@ -0,0 +1,4 @@
+invalid_route:
+ path: '/path'
+ template: 'template.html.twig'
+ controller: 'SomeControllerClass'
diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/Loader/XmlFileLoaderTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/Loader/XmlFileLoaderTest.php
new file mode 100644
index 0000000000000..91a28b0aeb355
--- /dev/null
+++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/Loader/XmlFileLoaderTest.php
@@ -0,0 +1,28 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\FrameworkBundle\Tests\Routing\Loader;
+
+use Symfony\Bundle\FrameworkBundle\Routing\Loader\XmlFileLoader;
+use Symfony\Component\Config\Loader\LoaderInterface;
+
+class XmlFileLoaderTest extends AbstractLoaderTest
+{
+ protected function getLoader(): LoaderInterface
+ {
+ return new XmlFileLoader($this->getLocator());
+ }
+
+ protected function getType(): string
+ {
+ return 'xml';
+ }
+}
diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/Loader/YamlFileLoaderTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/Loader/YamlFileLoaderTest.php
new file mode 100644
index 0000000000000..0e1a62643e704
--- /dev/null
+++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/Loader/YamlFileLoaderTest.php
@@ -0,0 +1,49 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\FrameworkBundle\Tests\Routing\Loader;
+
+use Symfony\Bundle\FrameworkBundle\Routing\Loader\YamlFileLoader;
+use Symfony\Component\Config\Loader\LoaderInterface;
+
+class YamlFileLoaderTest extends AbstractLoaderTest
+{
+ protected function getLoader(): LoaderInterface
+ {
+ return new YamlFileLoader($this->getLocator());
+ }
+
+ protected function getType(): string
+ {
+ return 'yaml';
+ }
+
+ /**
+ * @dataProvider getPathsToInvalidFiles
+ */
+ public function testLoadThrowsExceptionWithInvalidFile(string $filePath, string $exception)
+ {
+ $loader = $this->getLoader();
+
+ $message = sprintf($exception, __DIR__.'/../../Fixtures/Resources/config/routing/'.$filePath);
+
+ $this->expectException(\InvalidArgumentException::class);
+ $this->expectExceptionMessage(str_replace('/', \DIRECTORY_SEPARATOR, $message));
+
+ $loader->load($filePath);
+ }
+
+ public function getPathsToInvalidFiles()
+ {
+ yield 'defining controller' => ['with_controller_attribute.yaml', 'The routing file "%s" must not specify both the "controller" and the "template" keys for "invalid_route".'];
+ yield 'defining template and redirect' => ['template_and_redirect.yaml', 'The routing file "%s" must not specify only one route type among "template", "redirect_to_route" keys for "invalid_route".'];
+ }
+}
diff --git a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php
index 29d3e4a7714d5..ca8564e046050 100644
--- a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php
+++ b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php
@@ -30,7 +30,7 @@ class XmlFileLoader extends FileLoader
use PrefixTrait;
const NAMESPACE_URI = 'http://symfony.com/schema/routing';
- const SCHEME_PATH = '/schema/routing/routing-1.0.xsd';
+ const SCHEME_PATH = __DIR__.'/schema/routing/routing-1.0.xsd';
/**
* Loads an XML file.
@@ -229,7 +229,7 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, s
*/
protected function loadFile(string $file)
{
- return XmlUtils::loadFile($file, __DIR__.static::SCHEME_PATH);
+ return XmlUtils::loadFile($file, static::SCHEME_PATH);
}
/**
@@ -303,7 +303,7 @@ private function parseConfigs(\DOMElement $node, string $path): array
if (isset($defaults['_stateless'])) {
$name = $node->hasAttribute('id') ? sprintf('"%s"', $node->getAttribute('id')) : sprintf('the "%s" tag', $node->tagName);
- throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "stateless" attribute and the defaults key "_stateless" for %s.', $path, $name));
+ throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "stateless" attribute and the defaults key "_stateless" for "%s".', $path, $name));
}
$defaults['_stateless'] = XmlUtils::phpize($stateless);
@@ -317,7 +317,7 @@ private function parseConfigs(\DOMElement $node, string $path): array
*
* @return array|bool|float|int|string|null The parsed value of the "default" element
*/
- private function parseDefaultsConfig(\DOMElement $element, string $path)
+ final protected function parseDefaultsConfig(\DOMElement $element, string $path)
{
if ($this->isElementValueNull($element)) {
return null;
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy