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

  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.     public function load(mixed $resourcestring $type null): mixed
  21.     {
  22.         $path $this->locator->locate($resource);
  23.         $this->container->fileExists($path);
  24.         // first pass to catch parsing errors
  25.         $result parse_ini_file($pathtrue);
  26.         if (false === $result || [] === $result) {
  27.             throw new InvalidArgumentException(sprintf('The "%s" file is not valid.'$resource));
  28.         }
  29.         // real raw parsing
  30.         $result parse_ini_file($pathtrue\INI_SCANNER_RAW);
  31.         if (isset($result['parameters']) && \is_array($result['parameters'])) {
  32.             foreach ($result['parameters'] as $key => $value) {
  33.                 if (\is_array($value)) {
  34.                     $this->container->setParameter($keyarray_map($this->phpize(...), $value));
  35.                 } else {
  36.                     $this->container->setParameter($key$this->phpize($value));
  37.                 }
  38.             }
  39.         }
  40.         if ($this->env && \is_array($result['parameters@'.$this->env] ?? null)) {
  41.             foreach ($result['parameters@'.$this->env] as $key => $value) {
  42.                 $this->container->setParameter($key$this->phpize($value));
  43.             }
  44.         }
  45.         return null;
  46.     }
  47.     public function supports(mixed $resourcestring $type null): bool
  48.     {
  49.         if (!\is_string($resource)) {
  50.             return false;
  51.         }
  52.         if (null === $type && 'ini' === pathinfo($resource\PATHINFO_EXTENSION)) {
  53.             return true;
  54.         }
  55.         return 'ini' === $type;
  56.     }
  57.     /**
  58.      * Note that the following features are not supported:
  59.      *  * strings with escaped quotes are not supported "foo\"bar";
  60.      *  * string concatenation ("foo" "bar").
  61.      */
  62.     private function phpize(string $value): mixed
  63.     {
  64.         // trim on the right as comments removal keep whitespaces
  65.         if ($value !== $v rtrim($value)) {
  66.             $value '""' === substr_replace($v''1, -1) ? substr($v1, -1) : $v;
  67.         }
  68.         $lowercaseValue strtolower($value);
  69.         return match (true) {
  70.             \defined($value) => \constant($value),
  71.             'yes' === $lowercaseValue,
  72.             'on' === $lowercaseValue => true,
  73.             'no' === $lowercaseValue,
  74.             'off' === $lowercaseValue,
  75.             'none' === $lowercaseValue => false,
  76.             isset($value[1]) && (
  77.                 ("'" === $value[0] && "'" === $value[\strlen($value) - 1]) ||
  78.                 ('"' === $value[0] && '"' === $value[\strlen($value) - 1])
  79.             ) => substr($value1, -1), // quoted string
  80.             default => XmlUtils::phpize($value),
  81.         };
  82.     }
  83. }