FileLoader.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Symfony\Components\DependencyInjection\Loader;
  3. /*
  4. * This file is part of the symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * FileLoader is the abstract class used by all built-in loaders that are file based.
  13. *
  14. * @package symfony
  15. * @subpackage dependency_injection
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. */
  18. abstract class FileLoader extends Loader
  19. {
  20. protected $paths;
  21. /**
  22. * Constructor.
  23. *
  24. * @param string|array $paths A path or an array of paths where to look for resources
  25. */
  26. public function __construct($paths = array())
  27. {
  28. if (!is_array($paths))
  29. {
  30. $paths = array($paths);
  31. }
  32. $this->paths = $paths;
  33. }
  34. protected function findFile($file)
  35. {
  36. $path = $this->getAbsolutePath($file);
  37. if (!file_exists($path))
  38. {
  39. throw new \InvalidArgumentException(sprintf('The file "%s" does not exist (in: %s).', $file, implode(', ', $this->paths)));
  40. }
  41. return $path;
  42. }
  43. protected function getAbsolutePath($file, $currentPath = null)
  44. {
  45. if (self::isAbsolutePath($file))
  46. {
  47. return $file;
  48. }
  49. else if (null !== $currentPath && file_exists($currentPath.DIRECTORY_SEPARATOR.$file))
  50. {
  51. return $currentPath.DIRECTORY_SEPARATOR.$file;
  52. }
  53. else
  54. {
  55. foreach ($this->paths as $path)
  56. {
  57. if (file_exists($path.DIRECTORY_SEPARATOR.$file))
  58. {
  59. return $path.DIRECTORY_SEPARATOR.$file;
  60. }
  61. }
  62. }
  63. return $file;
  64. }
  65. static protected function isAbsolutePath($file)
  66. {
  67. if ($file[0] == '/' || $file[0] == '\\' ||
  68. (strlen($file) > 3 && ctype_alpha($file[0]) &&
  69. $file[1] == ':' &&
  70. ($file[2] == '\\' || $file[2] == '/')
  71. )
  72. )
  73. {
  74. return true;
  75. }
  76. return false;
  77. }
  78. }