4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Loader;
14 use Symfony\Component\Config\Resource\FileResource;
15 use Symfony\Component\Config\Util\XmlUtils;
16 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
19 * IniFileLoader loads parameters from INI files.
21 * @author Fabien Potencier <fabien@symfony.com>
23 class IniFileLoader extends FileLoader
28 public function load($resource, $type = null)
30 $path = $this->locator->locate($resource);
32 $this->container->addResource(new FileResource($path));
34 // first pass to catch parsing errors
35 $result = parse_ini_file($path, true);
36 if (false === $result || array() === $result) {
37 throw new InvalidArgumentException(sprintf('The "%s" file is not valid.', $resource));
41 $result = parse_ini_file($path, true, INI_SCANNER_RAW);
43 if (isset($result['parameters']) && is_array($result['parameters'])) {
44 foreach ($result['parameters'] as $key => $value) {
45 $this->container->setParameter($key, $this->phpize($value));
53 public function supports($resource, $type = null)
55 return is_string($resource) && 'ini' === pathinfo($resource, PATHINFO_EXTENSION);
59 * Note that the following features are not supported:
60 * * strings with escaped quotes are not supported "foo\"bar";
61 * * string concatenation ("foo" "bar").
63 private function phpize($value)
65 // trim on the right as comments removal keep whitespaces
66 $value = rtrim($value);
67 $lowercaseValue = strtolower($value);
71 return constant($value);
72 case 'yes' === $lowercaseValue || 'on' === $lowercaseValue:
74 case 'no' === $lowercaseValue || 'off' === $lowercaseValue || 'none' === $lowercaseValue:
76 case isset($value[1]) && (
77 ("'" === $value[0] && "'" === $value[strlen($value) - 1]) ||
78 ('"' === $value[0] && '"' === $value[strlen($value) - 1])
81 return substr($value, 1, -1);
83 return XmlUtils::phpize($value);