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