FileLoader.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\FileLocatorInterface;
  12. use Symfony\Component\Config\Exception\FileLoaderLoadException;
  13. use Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException;
  14. /**
  15. * FileLoader is the abstract class used by all built-in loaders that are file based.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. abstract class FileLoader extends Loader
  20. {
  21. /**
  22. * @var array
  23. */
  24. protected static $loading = array();
  25. /**
  26. * @var FileLocatorInterface
  27. */
  28. protected $locator;
  29. private $currentDir;
  30. /**
  31. * Constructor.
  32. *
  33. * @param FileLocatorInterface $locator A FileLocatorInterface instance
  34. */
  35. public function __construct(FileLocatorInterface $locator)
  36. {
  37. $this->locator = $locator;
  38. }
  39. /**
  40. * Sets the current directory.
  41. *
  42. * @param string $dir
  43. */
  44. public function setCurrentDir($dir)
  45. {
  46. $this->currentDir = $dir;
  47. }
  48. /**
  49. * Returns the file locator used by this loader.
  50. *
  51. * @return FileLocatorInterface
  52. */
  53. public function getLocator()
  54. {
  55. return $this->locator;
  56. }
  57. /**
  58. * Imports a resource.
  59. *
  60. * @param mixed $resource A Resource
  61. * @param string|null $type The resource type or null if unknown
  62. * @param bool $ignoreErrors Whether to ignore import errors or not
  63. * @param string|null $sourceResource The original resource importing the new resource
  64. *
  65. * @return mixed
  66. *
  67. * @throws FileLoaderLoadException
  68. * @throws FileLoaderImportCircularReferenceException
  69. */
  70. public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
  71. {
  72. try {
  73. $loader = $this->resolve($resource, $type);
  74. if ($loader instanceof self && null !== $this->currentDir) {
  75. // we fallback to the current locator to keep BC
  76. // as some some loaders do not call the parent __construct()
  77. // @deprecated should be removed in 3.0
  78. $locator = $loader->getLocator();
  79. if (null === $locator) {
  80. @trigger_error('Not calling the parent constructor in '.get_class($loader).' which extends '.__CLASS__.' is deprecated since version 2.7 and will not be supported anymore in 3.0.', E_USER_DEPRECATED);
  81. $locator = $this->locator;
  82. }
  83. $resource = $locator->locate($resource, $this->currentDir, false);
  84. }
  85. $resources = is_array($resource) ? $resource : array($resource);
  86. for ($i = 0; $i < $resourcesCount = count($resources); ++$i) {
  87. if (isset(self::$loading[$resources[$i]])) {
  88. if ($i == $resourcesCount - 1) {
  89. throw new FileLoaderImportCircularReferenceException(array_keys(self::$loading));
  90. }
  91. } else {
  92. $resource = $resources[$i];
  93. break;
  94. }
  95. }
  96. self::$loading[$resource] = true;
  97. try {
  98. $ret = $loader->load($resource, $type);
  99. } catch (\Exception $e) {
  100. unset(self::$loading[$resource]);
  101. throw $e;
  102. } catch (\Throwable $e) {
  103. unset(self::$loading[$resource]);
  104. throw $e;
  105. }
  106. unset(self::$loading[$resource]);
  107. return $ret;
  108. } catch (FileLoaderImportCircularReferenceException $e) {
  109. throw $e;
  110. } catch (\Exception $e) {
  111. if (!$ignoreErrors) {
  112. // prevent embedded imports from nesting multiple exceptions
  113. if ($e instanceof FileLoaderLoadException) {
  114. throw $e;
  115. }
  116. throw new FileLoaderLoadException($resource, $sourceResource, null, $e);
  117. }
  118. }
  119. }
  120. }