annotate vendor/symfony/dependency-injection/Loader/IniFileLoader.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
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@17 35 if (false === $result || [] === $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@17 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@17 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@17 73 if ($value !== $v = rtrim($value)) {
Chris@17 74 $value = '""' === substr_replace($v, '', 1, -1) ? substr($v, 1, -1) : $v;
Chris@17 75 }
Chris@0 76 $lowercaseValue = strtolower($value);
Chris@0 77
Chris@0 78 switch (true) {
Chris@17 79 case \defined($value):
Chris@17 80 return \constant($value);
Chris@0 81 case 'yes' === $lowercaseValue || 'on' === $lowercaseValue:
Chris@0 82 return true;
Chris@0 83 case 'no' === $lowercaseValue || 'off' === $lowercaseValue || 'none' === $lowercaseValue:
Chris@0 84 return false;
Chris@0 85 case isset($value[1]) && (
Chris@17 86 ("'" === $value[0] && "'" === $value[\strlen($value) - 1]) ||
Chris@17 87 ('"' === $value[0] && '"' === $value[\strlen($value) - 1])
Chris@0 88 ):
Chris@0 89 // quoted string
Chris@0 90 return substr($value, 1, -1);
Chris@0 91 default:
Chris@0 92 return XmlUtils::phpize($value);
Chris@0 93 }
Chris@0 94 }
Chris@0 95 }