Loader.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. use Symfony\Component\Config\Exception\FileLoaderLoadException;
  12. /**
  13. * Loader is the abstract class used by all built-in loaders.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. abstract class Loader implements LoaderInterface
  18. {
  19. protected $resolver;
  20. /**
  21. * Gets the loader resolver.
  22. *
  23. * @return LoaderResolver A LoaderResolver instance
  24. */
  25. public function getResolver()
  26. {
  27. return $this->resolver;
  28. }
  29. /**
  30. * Sets the loader resolver.
  31. *
  32. * @param LoaderResolver $resolver A LoaderResolver instance
  33. */
  34. public function setResolver(LoaderResolver $resolver)
  35. {
  36. $this->resolver = $resolver;
  37. }
  38. /**
  39. * Imports a resource.
  40. *
  41. * @param mixed $resource A Resource
  42. * @param string $type The resource type
  43. *
  44. * @return mixed
  45. */
  46. public function import($resource, $type = null)
  47. {
  48. return $this->resolve($resource)->load($resource, $type);
  49. }
  50. /**
  51. * Finds a loader able to load an imported resource.
  52. *
  53. * @param mixed $resource A Resource
  54. * @param string $type The resource type
  55. *
  56. * @return LoaderInterface A LoaderInterface instance
  57. *
  58. * @throws FileLoaderLoadException if no loader is found
  59. */
  60. public function resolve($resource, $type = null)
  61. {
  62. if ($this->supports($resource, $type)) {
  63. return $this;
  64. }
  65. $loader = null === $this->resolver ? false : $this->resolver->resolve($resource, $type);
  66. if (false === $loader) {
  67. throw new FileLoaderLoadException($resource);
  68. }
  69. return $loader;
  70. }
  71. }