IniFileLoader.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\Resource\FileResource;
  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, $type = null)
  24. {
  25. $path = $this->locator->locate($resource);
  26. $this->container->addResource(new FileResource($path));
  27. $result = parse_ini_file($path, true);
  28. if (false === $result || array() === $result) {
  29. throw new InvalidArgumentException(sprintf('The "%s" file is not valid.', $resource));
  30. }
  31. if (isset($result['parameters']) && \is_array($result['parameters'])) {
  32. foreach ($result['parameters'] as $key => $value) {
  33. $this->container->setParameter($key, $value);
  34. }
  35. }
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function supports($resource, $type = null)
  41. {
  42. return \is_string($resource) && 'ini' === pathinfo($resource, PATHINFO_EXTENSION);
  43. }
  44. }