9fea56be1b22dfef798d0da1790b2f3b24c73d24
[yaffs-website] / vendor / symfony / dependency-injection / Loader / IniFileLoader.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection\Loader;
13
14 use Symfony\Component\Config\Resource\FileResource;
15 use Symfony\Component\Config\Util\XmlUtils;
16 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
17
18 /**
19  * IniFileLoader loads parameters from INI files.
20  *
21  * @author Fabien Potencier <fabien@symfony.com>
22  */
23 class IniFileLoader extends FileLoader
24 {
25     /**
26      * {@inheritdoc}
27      */
28     public function load($resource, $type = null)
29     {
30         $path = $this->locator->locate($resource);
31
32         $this->container->addResource(new FileResource($path));
33
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));
38         }
39
40         // real raw parsing
41         $result = parse_ini_file($path, true, INI_SCANNER_RAW);
42
43         if (isset($result['parameters']) && is_array($result['parameters'])) {
44             foreach ($result['parameters'] as $key => $value) {
45                 $this->container->setParameter($key, $this->phpize($value));
46             }
47         }
48     }
49
50     /**
51      * {@inheritdoc}
52      */
53     public function supports($resource, $type = null)
54     {
55         return is_string($resource) && 'ini' === pathinfo($resource, PATHINFO_EXTENSION);
56     }
57
58     /**
59      * Note that the following features are not supported:
60      *  * strings with escaped quotes are not supported "foo\"bar";
61      *  * string concatenation ("foo" "bar").
62      */
63     private function phpize($value)
64     {
65         // trim on the right as comments removal keep whitespaces
66         $value = rtrim($value);
67         $lowercaseValue = strtolower($value);
68
69         switch (true) {
70             case defined($value):
71                 return constant($value);
72             case 'yes' === $lowercaseValue || 'on' === $lowercaseValue:
73                 return true;
74             case 'no' === $lowercaseValue || 'off' === $lowercaseValue || 'none' === $lowercaseValue:
75                 return false;
76             case isset($value[1]) && (
77                 ("'" === $value[0] && "'" === $value[strlen($value) - 1]) ||
78                 ('"' === $value[0] && '"' === $value[strlen($value) - 1])
79             ):
80                 // quoted string
81                 return substr($value, 1, -1);
82             default:
83                 return XmlUtils::phpize($value);
84         }
85     }
86 }