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