PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/spout/src/Spout/Autoloader/Psr4Autoloader.php

https://github.com/mackensen/moodle
PHP | 148 lines | 71 code | 16 blank | 61 comment | 6 complexity | 78a92e6af2e9cb0e234f5e9bfcbf7c4d MD5 | raw file
  1. <?php
  2. namespace Box\Spout\Autoloader;
  3. /**
  4. * Class Psr4Autoloader
  5. * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md#class-example
  6. */
  7. class Psr4Autoloader
  8. {
  9. /**
  10. * An associative array where the key is a namespace prefix and the value
  11. * is an array of base directories for classes in that namespace.
  12. *
  13. * @var array
  14. */
  15. protected $prefixes = [];
  16. /**
  17. * Register loader with SPL autoloader stack.
  18. *
  19. * @return void
  20. */
  21. public function register()
  22. {
  23. \spl_autoload_register([$this, 'loadClass']);
  24. }
  25. /**
  26. * Adds a base directory for a namespace prefix.
  27. *
  28. * @param string $prefix The namespace prefix.
  29. * @param string $baseDir A base directory for class files in the
  30. * namespace.
  31. * @param bool $prepend If true, prepend the base directory to the stack
  32. * instead of appending it; this causes it to be searched first rather
  33. * than last.
  34. * @return void
  35. */
  36. public function addNamespace($prefix, $baseDir, $prepend = false)
  37. {
  38. // normalize namespace prefix
  39. $prefix = \trim($prefix, '\\') . '\\';
  40. // normalize the base directory with a trailing separator
  41. $baseDir = \rtrim($baseDir, DIRECTORY_SEPARATOR) . '/';
  42. // initialize the namespace prefix array
  43. if (isset($this->prefixes[$prefix]) === false) {
  44. $this->prefixes[$prefix] = [];
  45. }
  46. // retain the base directory for the namespace prefix
  47. if ($prepend) {
  48. \array_unshift($this->prefixes[$prefix], $baseDir);
  49. } else {
  50. \array_push($this->prefixes[$prefix], $baseDir);
  51. }
  52. }
  53. /**
  54. * Loads the class file for a given class name.
  55. *
  56. * @param string $class The fully-qualified class name.
  57. * @return mixed The mapped file name on success, or boolean false on
  58. * failure.
  59. */
  60. public function loadClass($class)
  61. {
  62. // the current namespace prefix
  63. $prefix = $class;
  64. // work backwards through the namespace names of the fully-qualified
  65. // class name to find a mapped file name
  66. while (($pos = \strrpos($prefix, '\\')) !== false) {
  67. // retain the trailing namespace separator in the prefix
  68. $prefix = \substr($class, 0, $pos + 1);
  69. // the rest is the relative class name
  70. $relativeClass = \substr($class, $pos + 1);
  71. // try to load a mapped file for the prefix and relative class
  72. $mappedFile = $this->loadMappedFile($prefix, $relativeClass);
  73. if ($mappedFile !== false) {
  74. return $mappedFile;
  75. }
  76. // remove the trailing namespace separator for the next iteration
  77. // of strrpos()
  78. $prefix = \rtrim($prefix, '\\');
  79. }
  80. // never found a mapped file
  81. return false;
  82. }
  83. /**
  84. * Load the mapped file for a namespace prefix and relative class.
  85. *
  86. * @param string $prefix The namespace prefix.
  87. * @param string $relativeClass The relative class name.
  88. * @return mixed Boolean false if no mapped file can be loaded, or the
  89. * name of the mapped file that was loaded.
  90. */
  91. protected function loadMappedFile($prefix, $relativeClass)
  92. {
  93. // are there any base directories for this namespace prefix?
  94. if (isset($this->prefixes[$prefix]) === false) {
  95. return false;
  96. }
  97. // look through base directories for this namespace prefix
  98. foreach ($this->prefixes[$prefix] as $baseDir) {
  99. // replace the namespace prefix with the base directory,
  100. // replace namespace separators with directory separators
  101. // in the relative class name, append with .php
  102. $file = $baseDir
  103. . \str_replace('\\', '/', $relativeClass)
  104. . '.php';
  105. // if the mapped file exists, require it
  106. if ($this->requireFile($file)) {
  107. // yes, we're done
  108. return $file;
  109. }
  110. }
  111. // never found it
  112. return false;
  113. }
  114. /**
  115. * If a file exists, require it from the file system.
  116. *
  117. * @param string $file The file to require.
  118. * @return bool True if the file exists, false if not.
  119. */
  120. protected function requireFile($file)
  121. {
  122. if (\file_exists($file)) {
  123. require $file;
  124. return true;
  125. }
  126. return false;
  127. }
  128. }