IniFileLoader.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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($resource, string $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($path, true);
  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($path, true, \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. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function supports($resource, string $type = null)
  44. {
  45. if (!\is_string($resource)) {
  46. return false;
  47. }
  48. if (null === $type && 'ini' === pathinfo($resource, \PATHINFO_EXTENSION)) {
  49. return true;
  50. }
  51. return 'ini' === $type;
  52. }
  53. /**
  54. * Note that the following features are not supported:
  55. * * strings with escaped quotes are not supported "foo\"bar";
  56. * * string concatenation ("foo" "bar").
  57. *
  58. * @return mixed
  59. */
  60. private function phpize(string $value)
  61. {
  62. // trim on the right as comments removal keep whitespaces
  63. if ($value !== $v = rtrim($value)) {
  64. $value = '""' === substr_replace($v, '', 1, -1) ? substr($v, 1, -1) : $v;
  65. }
  66. $lowercaseValue = strtolower($value);
  67. switch (true) {
  68. case \defined($value):
  69. return \constant($value);
  70. case 'yes' === $lowercaseValue || 'on' === $lowercaseValue:
  71. return true;
  72. case 'no' === $lowercaseValue || 'off' === $lowercaseValue || 'none' === $lowercaseValue:
  73. return false;
  74. case isset($value[1]) && (
  75. ("'" === $value[0] && "'" === $value[\strlen($value) - 1]) ||
  76. ('"' === $value[0] && '"' === $value[\strlen($value) - 1])
  77. ):
  78. // quoted string
  79. return substr($value, 1, -1);
  80. default:
  81. return XmlUtils::phpize($value);
  82. }
  83. }
  84. }