Loader.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\LoaderLoadException;
  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. * {@inheritdoc}
  22. */
  23. public function getResolver()
  24. {
  25. return $this->resolver;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function setResolver(LoaderResolverInterface $resolver)
  31. {
  32. $this->resolver = $resolver;
  33. }
  34. /**
  35. * Imports a resource.
  36. *
  37. * @param mixed $resource A resource
  38. * @param string|null $type The resource type or null if unknown
  39. *
  40. * @return mixed
  41. */
  42. public function import($resource, string $type = null)
  43. {
  44. return $this->resolve($resource, $type)->load($resource, $type);
  45. }
  46. /**
  47. * Finds a loader able to load an imported resource.
  48. *
  49. * @param mixed $resource A resource
  50. * @param string|null $type The resource type or null if unknown
  51. *
  52. * @return $this|LoaderInterface
  53. *
  54. * @throws LoaderLoadException If no loader is found
  55. */
  56. public function resolve($resource, string $type = null)
  57. {
  58. if ($this->supports($resource, $type)) {
  59. return $this;
  60. }
  61. $loader = null === $this->resolver ? false : $this->resolver->resolve($resource, $type);
  62. if (false === $loader) {
  63. throw new LoaderLoadException($resource, null, 0, null, $type);
  64. }
  65. return $loader;
  66. }
  67. }