/vendors/dd-configuration-php/dd_configuration_PathResourceLoader.php

https://github.com/dflydev/substrate-php · PHP · 157 lines · 70 code · 23 blank · 64 comment · 9 complexity · cbfaaf3200af1cd60e45f9490761ea89 MD5 · raw file

  1. <?php
  2. /**
  3. * Path Resource Loader class.
  4. * @package dd_configuration
  5. */
  6. require_once('dd_configuration_IResourceLoader.php');
  7. /**
  8. * Path Resource Loader class.
  9. * @package dd_configuration
  10. */
  11. class dd_configuration_PathResourceLoader implements dd_configuration_IResourceLoader {
  12. /**
  13. * Calling file
  14. *
  15. * This path is used as a starting point for '.' path so that resource
  16. * files that live in the same directory as a library will be loaded
  17. * relatively.
  18. *
  19. * @var string
  20. */
  21. private $callingFile;
  22. /**
  23. * Paths
  24. * @var array
  25. */
  26. private $paths = array();
  27. /**
  28. * Appended paths
  29. * @var array
  30. */
  31. private $appendedPaths = array();
  32. /**
  33. * Prepended paths
  34. * @var array
  35. */
  36. private $prependedPaths = array();
  37. /**
  38. * Constructor
  39. * @param string $callingFile Dot path
  40. */
  41. public function __construct($callingFile = null, $paths = null, $prependedPaths = null, $appendedPaths = null) {
  42. if ( $callingFile !== null ) {
  43. $this->callingFile = $callingFile;
  44. }
  45. if ( $paths !== null ) {
  46. if ( ! is_array($paths) ) {
  47. $paths = array($paths);
  48. }
  49. foreach ( $paths as $path ) {
  50. $this->paths[] = $path;
  51. }
  52. }
  53. if ( $prependedPaths !== null ) {
  54. if ( ! is_array($prependedPaths) ) {
  55. $prependedPaths = array($prependedPaths);
  56. }
  57. foreach ( $prependedPaths as $path ) {
  58. $this->prependedPaths[] = $path;
  59. }
  60. }
  61. if ( $appendedPaths !== null ) {
  62. if ( ! is_array($appendedPaths) ) {
  63. $appendedPaths = array($appendedPaths);
  64. }
  65. foreach ( $appendedPaths as $path ) {
  66. $this->appendedPaths[] = $path;
  67. }
  68. }
  69. }
  70. /**
  71. * Find the path for a location
  72. * @param string $location Location
  73. * @return string
  74. */
  75. public function find($location) {
  76. foreach ( $this->allPaths() as $path ) {
  77. $testLocation = $path . '/' . $location;
  78. // TODO This could possibly be cached eventually.
  79. if ( file_exists($testLocation) ) return $testLocation;
  80. }
  81. // Could not be found.
  82. return null;
  83. }
  84. /**
  85. * Paths to search
  86. * @return array
  87. */
  88. public function allPaths() {
  89. $callingFiles = array();
  90. if ( $this->callingFile ) $callingFiles[] = dirname($this->callingFile);
  91. return array_merge(
  92. $callingFiles,
  93. $this->prependedPaths(),
  94. $this->paths(),
  95. $this->appendedPaths()
  96. );
  97. }
  98. /**
  99. * Paths
  100. * @return array
  101. */
  102. public function paths() {
  103. return $this->paths;
  104. }
  105. /**
  106. * Prepend a path to the classpath
  107. * @param string $path Path
  108. */
  109. public function prependPath($path) {
  110. array_unshift($this->prependedPaths, $path);
  111. }
  112. /**
  113. * Append a path to the classpath
  114. * @param string $path Path
  115. */
  116. public function appendPath($path) {
  117. push($this->appendedPaths, $path);
  118. }
  119. /**
  120. * Prepended paths
  121. * @return array
  122. */
  123. public function prependedPaths() {
  124. return $this->prependedPaths;
  125. }
  126. /**
  127. * Appended paths
  128. * @return array
  129. */
  130. public function appendedPaths() {
  131. return $this->appendedPaths;
  132. }
  133. }
  134. ?>