Skip to content

Commit

Permalink
[Routing] throw an exception if route config has unsupported keys
Browse files Browse the repository at this point in the history
  • Loading branch information
everzet committed Mar 19, 2011
1 parent cdfc731 commit 3fd50ea
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Expand Up @@ -24,6 +24,10 @@
*/
class YamlFileLoader extends FileLoader
{
private static $availableKeys = array(
'type', 'resource', 'prefix', 'pattern', 'options', 'defaults', 'requirements'
);

/**
* Loads a Yaml file.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
@@ -0,0 +1,3 @@
someroute:
resource: path/to/some.yml
name_prefix: test_
Expand Up @@ -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');
}
}

0 comments on commit 3fd50ea

Please sign in to comment.