Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[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\Util\XmlUtils;
15 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
16
17 /**
18  * IniFileLoader loads parameters from INI files.
19  *
20  * @author Fabien Potencier <fabien@symfony.com>
21  */
22 class IniFileLoader extends FileLoader
23 {
24     /**
25      * {@inheritdoc}
26      */
27     public function load($resource, $type = null)
28     {
29         $path = $this->locator->locate($resource);
30
31         $this->container->fileExists($path);
32
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));
37         }
38
39         // real raw parsing
40         $result = parse_ini_file($path, true, INI_SCANNER_RAW);
41
42         if (isset($result['parameters']) && \is_array($result['parameters'])) {
43             foreach ($result['parameters'] as $key => $value) {
44                 $this->container->setParameter($key, $this->phpize($value));
45             }
46         }
47     }
48
49     /**
50      * {@inheritdoc}
51      */
52     public function supports($resource, $type = null)
53     {
54         if (!\is_string($resource)) {
55             return false;
56         }
57
58         if (null === $type && 'ini' === pathinfo($resource, PATHINFO_EXTENSION)) {
59             return true;
60         }
61
62         return 'ini' === $type;
63     }
64
65     /**
66      * Note that the following features are not supported:
67      *  * strings with escaped quotes are not supported "foo\"bar";
68      *  * string concatenation ("foo" "bar").
69      */
70     private function phpize($value)
71     {
72         // trim on the right as comments removal keep whitespaces
73         $value = rtrim($value);
74         $lowercaseValue = strtolower($value);
75
76         switch (true) {
77             case \defined($value):
78                 return \constant($value);
79             case 'yes' === $lowercaseValue || 'on' === $lowercaseValue:
80                 return true;
81             case 'no' === $lowercaseValue || 'off' === $lowercaseValue || 'none' === $lowercaseValue:
82                 return false;
83             case isset($value[1]) && (
84                 ("'" === $value[0] && "'" === $value[\strlen($value) - 1]) ||
85                 ('"' === $value[0] && '"' === $value[\strlen($value) - 1])
86             ):
87                 // quoted string
88                 return substr($value, 1, -1);
89             default:
90                 return XmlUtils::phpize($value);
91         }
92     }
93 }