2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-01-04 14:26:20 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-01-04 14:26:20 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\DependencyInjection;
|
|
|
|
|
2010-04-07 01:51:29 +01:00
|
|
|
/**
|
|
|
|
* SimpleXMLElement class.
|
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-04-07 01:51:29 +01:00
|
|
|
*/
|
2010-01-04 14:26:20 +00:00
|
|
|
class SimpleXMLElement extends \SimpleXMLElement
|
|
|
|
{
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Converts an attribute as a php type.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function getAttributeAsPhp($name)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return self::phpize($this[$name]);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Returns arguments as valid php types.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function getArgumentsAsPhp($name)
|
|
|
|
{
|
|
|
|
$arguments = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($this->$name as $arg) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$key = isset($arg['key']) ? (string) $arg['key'] : (!$arguments ? 0 : max(array_keys($arguments)) + 1);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// parameter keys are case insensitive
|
2010-05-07 15:09:11 +01:00
|
|
|
if ('parameter' == $name) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$key = strtolower($key);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-01-26 23:14:31 +00:00
|
|
|
// this is used by DefinitionDecorator to overwrite a specific
|
|
|
|
// argument of the parent definition
|
|
|
|
if (isset($arg['index'])) {
|
|
|
|
$key = 'index_'.$arg['index'];
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
switch ($arg['type']) {
|
2010-05-06 12:25:53 +01:00
|
|
|
case 'service':
|
2010-06-27 17:28:29 +01:00
|
|
|
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
|
2010-05-07 15:09:11 +01:00
|
|
|
if (isset($arg['on-invalid']) && 'ignore' == $arg['on-invalid']) {
|
2010-06-27 17:28:29 +01:00
|
|
|
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
|
2010-05-07 15:09:11 +01:00
|
|
|
} elseif (isset($arg['on-invalid']) && 'null' == $arg['on-invalid']) {
|
2010-06-27 17:28:29 +01:00
|
|
|
$invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2011-01-17 22:28:59 +00:00
|
|
|
|
|
|
|
if (isset($arg['strict'])) {
|
|
|
|
$strict = self::phpize($arg['strict']);
|
|
|
|
} else {
|
|
|
|
$strict = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$arguments[$key] = new Reference((string) $arg['id'], $invalidBehavior, $strict);
|
2010-05-06 12:25:53 +01:00
|
|
|
break;
|
|
|
|
case 'collection':
|
|
|
|
$arguments[$key] = $arg->getArgumentsAsPhp($name);
|
|
|
|
break;
|
|
|
|
case 'string':
|
|
|
|
$arguments[$key] = (string) $arg;
|
|
|
|
break;
|
|
|
|
case 'constant':
|
|
|
|
$arguments[$key] = constant((string) $arg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$arguments[$key] = self::phpize($arg);
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $arguments;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Converts an xml value to a php type.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
static public function phpize($value)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$value = (string) $value;
|
|
|
|
$lowercaseValue = strtolower($value);
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
switch (true) {
|
2010-05-06 12:25:53 +01:00
|
|
|
case 'null' === $lowercaseValue:
|
|
|
|
return null;
|
|
|
|
case ctype_digit($value):
|
|
|
|
return '0' == $value[0] ? octdec($value) : intval($value);
|
|
|
|
case 'true' === $lowercaseValue:
|
|
|
|
return true;
|
|
|
|
case 'false' === $lowercaseValue:
|
|
|
|
return false;
|
|
|
|
case is_numeric($value):
|
|
|
|
return '0x' == $value[0].$value[1] ? hexdec($value) : floatval($value);
|
|
|
|
case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $value):
|
|
|
|
return floatval(str_replace(',', '', $value));
|
|
|
|
default:
|
|
|
|
return $value;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
}
|