LoaderResolver.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\Config\Loader;
  11. /**
  12. * LoaderResolver selects a loader for a given resource.
  13. *
  14. * A resource can be anything (e.g. a full path to a config file or a Closure).
  15. * Each loader determines whether it can load a resource and how.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class LoaderResolver implements LoaderResolverInterface
  20. {
  21. /**
  22. * @var LoaderInterface[] An array of LoaderInterface objects
  23. */
  24. private $loaders = array();
  25. /**
  26. * Constructor.
  27. *
  28. * @param LoaderInterface[] $loaders An array of loaders
  29. */
  30. public function __construct(array $loaders = array())
  31. {
  32. foreach ($loaders as $loader) {
  33. $this->addLoader($loader);
  34. }
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function resolve($resource, $type = null)
  40. {
  41. foreach ($this->loaders as $loader) {
  42. if ($loader->supports($resource, $type)) {
  43. return $loader;
  44. }
  45. }
  46. return false;
  47. }
  48. /**
  49. * Adds a loader.
  50. *
  51. * @param LoaderInterface $loader A LoaderInterface instance
  52. */
  53. public function addLoader(LoaderInterface $loader)
  54. {
  55. $this->loaders[] = $loader;
  56. $loader->setResolver($this);
  57. }
  58. /**
  59. * Returns the registered loaders.
  60. *
  61. * @return LoaderInterface[] An array of LoaderInterface instances
  62. */
  63. public function getLoaders()
  64. {
  65. return $this->loaders;
  66. }
  67. }