FileLoader.php 4.0 KB

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