Skip to content

Commit

Permalink
added a method to normalize config entries coming from YAML and XML
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 21, 2011
1 parent acb19bc commit 69f0ec3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 44 deletions.
Expand Up @@ -131,15 +131,15 @@ protected function createAuthorization($config, ContainerBuilder $container)
if (isset($access['host'])) {
$host = $access['host'];
}
if (count($tMethods = $this->fixConfig($access, 'method')) > 0) {
if (count($tMethods = $this->normalizeConfig($access, 'method')) > 0) {
$methods = $tMethods;
}
if (isset($access['ip'])) {
$ip = $access['ip'];
}

$matchAttributes = array();
$attributes = $this->fixConfig($access, 'attribute');
$attributes = $this->normalizeConfig($access, 'attribute');
foreach ($attributes as $key => $attribute) {
if (isset($attribute['key'])) {
$key = $attribute['key'];
Expand All @@ -158,7 +158,7 @@ protected function createFirewalls($config, ContainerBuilder $container)

$this->createEncoders($config, $container);

if (!$firewalls = $this->fixConfig($config, 'firewall')) {
if (!$firewalls = $this->normalizeConfig($config, 'firewall')) {
return;
}

Expand All @@ -177,7 +177,7 @@ protected function createFirewalls($config, ContainerBuilder $container)
$loader = new XmlFileLoader($c, array(__DIR__.'/../Resources/config', __DIR__.'/Resources/config'));
$loader->load('security_templates.xml');

foreach ($this->fixConfig($config, 'template') as $template) {
foreach ($this->normalizeConfig($config, 'template') as $template) {
$loader->load($c->getParameterBag()->resolveValue($template));
}
$container->merge($c);
Expand Down Expand Up @@ -260,7 +260,7 @@ protected function createFirewall(ContainerBuilder $container, $firewall, $provi
$listener->addMethodCall('addHandler', array(new Reference('security.logout.handler.session')));
}

if (count($cookies = $this->fixConfig($firewall['logout'], 'cookie')) > 0) {
if (count($cookies = $this->normalizeConfig($firewall['logout'], 'cookie')) > 0) {
$cookieHandlerId = 'security.logout.handler.cookie_clearing.'.$id;
$cookieHandler = $container->setDefinition($cookieHandlerId, clone $container->getDefinition('security.logout.handler.cookie_clearing'));
$cookieHandler->setArguments(array($cookies));
Expand Down Expand Up @@ -348,7 +348,7 @@ protected function createAuthenticationListeners($container, $id, $firewall, $de
// Parses user providers and returns an array of their ids
protected function createUserProviders($config, ContainerBuilder $container)
{
$providers = $this->fixConfig($config, 'provider');
$providers = $this->normalizeConfig($config, 'provider');
if (!$providers) {
return array();
}
Expand All @@ -369,7 +369,7 @@ protected function createUserProviders($config, ContainerBuilder $container)

protected function createEncoders($config, ContainerBuilder $container)
{
$encoders = $this->fixConfig($config, 'encoder');
$encoders = $this->normalizeConfig($config, 'encoder');
if (!$encoders) {
return array();
}
Expand Down Expand Up @@ -514,7 +514,7 @@ protected function createUserDaoProvider($name, $provider, ContainerBuilder $con
// In-memory DAO provider
$definition = $container->register($name, '%security.user.provider.in_memory.class%');
$definition->setPublic(false);
foreach ($this->fixConfig($provider, 'user') as $username => $user) {
foreach ($this->normalizeConfig($provider, 'user') as $username => $user) {
if (isset($user['name'])) {
$username = $user['name'];
}
Expand Down Expand Up @@ -667,21 +667,4 @@ public function getAlias()
{
return 'security';
}

protected function fixConfig($config, $key)
{
$values = array();
if (isset($config[$key.'s'])) {
$values = $config[$key.'s'];
} elseif (isset($config[$key])) {
if (is_string($config[$key]) || !is_int(key($config[$key]))) {
// only one
$values = array($config[$key]);
} else {
$values = $config[$key];
}
}

return $values;
}
}
Expand Up @@ -67,7 +67,7 @@ protected function doConfigLoad(array $config, ContainerBuilder $container)

// globals
$def = $container->getDefinition('twig');
$globals = $this->fixConfig($config, 'global');
$globals = $this->normalizeConfig($config, 'global');
if (isset($globals[0])) {
foreach ($globals as $global) {
if (isset($global['type']) && 'service' === $global['type']) {
Expand All @@ -90,7 +90,7 @@ protected function doConfigLoad(array $config, ContainerBuilder $container)
unset($config['globals'], $config['global']);

// extensions
$extensions = $this->fixConfig($config, 'extension');
$extensions = $this->normalizeConfig($config, 'extension');
if (isset($extensions[0]) && is_array($extensions[0])) {
foreach ($extensions as $extension) {
$container->getDefinition($extension['id'])->addTag('twig.extension');
Expand Down Expand Up @@ -132,21 +132,4 @@ public function getAlias()
{
return 'twig';
}

protected function fixConfig($config, $key)
{
$values = array();
if (isset($config[$key.'s'])) {
$values = $config[$key.'s'];
} elseif (isset($config[$key])) {
if (is_string($config[$key]) || !is_int(key($config[$key]))) {
// only one
$values = array($config[$key]);
} else {
$values = $config[$key];
}
}

return $values;
}
}
37 changes: 37 additions & 0 deletions src/Symfony/Component/HttpKernel/DependencyInjection/Extension.php
Expand Up @@ -37,4 +37,41 @@ protected function addClassesToCompile(array $classes)
{
$this->classes = array_merge($this->classes, $classes);
}

/**
* Normalizes a configuration entry.
*
* This method returns a normalize configuration array for a given key
* to remove the differences due to the original format (YAML and XML mainly).
*
* Here is an example.
*
* The configuration is XML:
*
* <twig:extension id="twig.extension.foo" />
* <twig:extension id="twig.extension.bar" />
*
* And the same configuration in YAML:
*
* twig.extensions: ['twig.extension.foo', 'twig.extension.bar']
*
* @param array A config array
* @param key The key to normalize
*/
protected function normalizeConfig($config, $key)
{
$values = array();
if (isset($config[$key.'s'])) {
$values = $config[$key.'s'];
} elseif (isset($config[$key])) {
if (is_string($config[$key]) || !is_int(key($config[$key]))) {
// only one
$values = array($config[$key]);
} else {
$values = $config[$key];
}
}

return $values;
}
}

0 comments on commit 69f0ec3

Please sign in to comment.