IniFileLoader.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. * Loads a resource.
  22. *
  23. * @param mixed $file The resource
  24. * @param string $type The resource type
  25. *
  26. * @throws InvalidArgumentException When ini file is not valid
  27. */
  28. public function load($file, $type = null)
  29. {
  30. $path = $this->locator->locate($file);
  31. $this->container->addResource(new FileResource($path));
  32. $result = parse_ini_file($path, true);
  33. if (false === $result || array() === $result) {
  34. throw new InvalidArgumentException(sprintf('The "%s" file is not valid.', $file));
  35. }
  36. if (isset($result['parameters']) && is_array($result['parameters'])) {
  37. foreach ($result['parameters'] as $key => $value) {
  38. $this->container->setParameter($key, $value);
  39. }
  40. }
  41. }
  42. /**
  43. * Returns true if this class supports the given resource.
  44. *
  45. * @param mixed $resource A resource
  46. * @param string $type The resource type
  47. *
  48. * @return Boolean true if this class supports the given resource, false otherwise
  49. */
  50. public function supports($resource, $type = null)
  51. {
  52. return is_string($resource) && 'ini' === pathinfo($resource, PATHINFO_EXTENSION);
  53. }
  54. }