diff --git a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php index c5a2c4b5afc7..567b8e988e94 100644 --- a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php @@ -24,6 +24,10 @@ */ class YamlFileLoader extends FileLoader { + private static $availableKeys = array( + 'type', 'resource', 'prefix', 'pattern', 'options', 'defaults', 'requirements' + ); + /** * Loads a Yaml file. * @@ -54,6 +58,8 @@ public function load($file, $type = null) } foreach ($config as $name => $config) { + $config = $this->normalizeRouteConfig($config); + if (isset($config['resource'])) { $type = isset($config['type']) ? $config['type'] : null; $prefix = isset($config['prefix']) ? $config['prefix'] : null; @@ -118,4 +124,27 @@ protected function loadFile($file) { return Yaml::load($file); } + + /** + * Normalize route configuration. + * + * @param array $config A resource config + * + * @return array + * + * @throws InvalidArgumentException if one of the provided config keys is not supported + */ + private function normalizeRouteConfig(array $config) + { + foreach ($config as $key => $value) { + if (!in_array($key, self::$availableKeys)) { + throw new \InvalidArgumentException(sprintf( + 'Unsupported config key given: "%s". Expected one of the (%s).', + $key, implode(', ', self::$availableKeys) + )); + } + } + + return $config; + } } diff --git a/tests/Symfony/Tests/Component/Routing/Fixtures/nonvalidkeys.yml b/tests/Symfony/Tests/Component/Routing/Fixtures/nonvalidkeys.yml new file mode 100644 index 000000000000..015e270fb187 --- /dev/null +++ b/tests/Symfony/Tests/Component/Routing/Fixtures/nonvalidkeys.yml @@ -0,0 +1,3 @@ +someroute: + resource: path/to/some.yml + name_prefix: test_ diff --git a/tests/Symfony/Tests/Component/Routing/Loader/YamlFileLoaderTest.php b/tests/Symfony/Tests/Component/Routing/Loader/YamlFileLoaderTest.php index cf820a597e9c..ca90e9ef1b6c 100644 --- a/tests/Symfony/Tests/Component/Routing/Loader/YamlFileLoaderTest.php +++ b/tests/Symfony/Tests/Component/Routing/Loader/YamlFileLoaderTest.php @@ -51,4 +51,13 @@ public function testLoadThrowsExceptionIfNotAnArray() $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures'))); $loader->load('nonvalid.yml'); } + + /** + * @expectedException \InvalidArgumentException + */ + public function testLoadThrowsExceptionIfArrayHasUnsupportedKeys() + { + $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures'))); + $loader->load('nonvalidkeys.yml'); + } }