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\Util\XmlUtils;
15 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
18 * IniFileLoader loads parameters from INI files.
20 * @author Fabien Potencier <fabien@symfony.com>
22 class IniFileLoader extends FileLoader
27 public function load($resource, $type = null)
29 $path = $this->locator->locate($resource);
31 $this->container->fileExists($path);
33 // first pass to catch parsing errors
34 $result = parse_ini_file($path, true);
35 if (false === $result || array() === $result) {
36 throw new InvalidArgumentException(sprintf('The "%s" file is not valid.', $resource));
40 $result = parse_ini_file($path, true, INI_SCANNER_RAW);
42 if (isset($result['parameters']) && \is_array($result['parameters'])) {
43 foreach ($result['parameters'] as $key => $value) {
44 $this->container->setParameter($key, $this->phpize($value));
52 public function supports($resource, $type = null)
54 if (!\is_string($resource)) {
58 if (null === $type && 'ini' === pathinfo($resource, PATHINFO_EXTENSION)) {
62 return 'ini' === $type;
66 * Note that the following features are not supported:
67 * * strings with escaped quotes are not supported "foo\"bar";
68 * * string concatenation ("foo" "bar").
70 private function phpize($value)
72 // trim on the right as comments removal keep whitespaces
73 $value = rtrim($value);
74 $lowercaseValue = strtolower($value);
77 case \defined($value):
78 return \constant($value);
79 case 'yes' === $lowercaseValue || 'on' === $lowercaseValue:
81 case 'no' === $lowercaseValue || 'off' === $lowercaseValue || 'none' === $lowercaseValue:
83 case isset($value[1]) && (
84 ("'" === $value[0] && "'" === $value[\strlen($value) - 1]) ||
85 ('"' === $value[0] && '"' === $value[\strlen($value) - 1])
88 return substr($value, 1, -1);
90 return XmlUtils::phpize($value);