vendor/symfony/dependency-injection/Loader/IniFileLoader.php line 27

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