LoaderResolver.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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;
  25. /**
  26. * Constructor.
  27. *
  28. * @param LoaderInterface[] $loaders An array of loaders
  29. */
  30. public function __construct(array $loaders = array())
  31. {
  32. $this->loaders = array();
  33. foreach ($loaders as $loader) {
  34. $this->addLoader($loader);
  35. }
  36. }
  37. /**
  38. * Returns a loader able to load the resource.
  39. *
  40. * @param mixed $resource A resource
  41. * @param string $type The resource type
  42. *
  43. * @return LoaderInterface|false A LoaderInterface instance
  44. */
  45. public function resolve($resource, $type = null)
  46. {
  47. foreach ($this->loaders as $loader) {
  48. if ($loader->supports($resource, $type)) {
  49. return $loader;
  50. }
  51. }
  52. return false;
  53. }
  54. /**
  55. * Adds a loader.
  56. *
  57. * @param LoaderInterface $loader A LoaderInterface instance
  58. */
  59. public function addLoader(LoaderInterface $loader)
  60. {
  61. $this->loaders[] = $loader;
  62. $loader->setResolver($this);
  63. }
  64. /**
  65. * Returns the registered loaders.
  66. *
  67. * @return LoaderInterface[] An array of LoaderInterface instances
  68. */
  69. public function getLoaders()
  70. {
  71. return $this->loaders;
  72. }
  73. }