FileLoader.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\FileLoaderImportException;
  13. /**
  14. * FileLoader is the abstract class used by all built-in loaders that are file based.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. abstract class FileLoader extends Loader
  19. {
  20. protected $locator;
  21. private $currentDir;
  22. /**
  23. * Constructor.
  24. */
  25. public function __construct(FileLocatorInterface $locator)
  26. {
  27. $this->locator = $locator;
  28. }
  29. public function setCurrentDir($dir)
  30. {
  31. $this->currentDir = $dir;
  32. }
  33. public function getLocator()
  34. {
  35. return $this->locator;
  36. }
  37. /**
  38. * Adds definitions and parameters from a resource.
  39. *
  40. * @param mixed $resource A Resource
  41. * @param string $type The resource type
  42. * @param Boolean $ignoreErrors Whether to ignore import errors or not
  43. *
  44. * @return mixed
  45. */
  46. public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
  47. {
  48. try {
  49. $loader = $this->resolve($resource, $type);
  50. if ($loader instanceof FileLoader && null !== $this->currentDir) {
  51. $resource = $this->locator->locate($resource, $this->currentDir);
  52. }
  53. return $loader->load($resource);
  54. } catch (\Exception $e) {
  55. if (!$ignoreErrors) {
  56. // prevent embedded imports from nesting multiple exceptions
  57. if ($e instanceof FileLoaderImportException) {
  58. throw $e;
  59. }
  60. throw new FileLoaderImportException($resource, $sourceResource, null, $e);
  61. }
  62. }
  63. }
  64. }