timesheet source code
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

187 lines
5.7KB

  1. <?php
  2. /**
  3. * An example of a general-purpose implementation that includes the optional
  4. * functionality of allowing multiple base directories for a single namespace
  5. * prefix.
  6. *
  7. * Given a foo-bar package of classes in the file system at the following
  8. * paths ...
  9. *
  10. * /path/to/packages/foo-bar/
  11. * src/
  12. * Baz.php # Foo\Bar\Baz
  13. * Qux/
  14. * Quux.php # Foo\Bar\Qux\Quux
  15. * tests/
  16. * BazTest.php # Foo\Bar\BazTest
  17. * Qux/
  18. * QuuxTest.php # Foo\Bar\Qux\QuuxTest
  19. *
  20. * ... add the path to the class files for the \Foo\Bar\ namespace prefix
  21. * as follows:
  22. *
  23. * <?php
  24. * // instantiate the loader
  25. * $loader = new \Example\Psr4AutoloaderClass;
  26. *
  27. * // register the autoloader
  28. * $loader->register();
  29. *
  30. * // register the base directories for the namespace prefix
  31. * $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/src');
  32. * $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/tests');
  33. *
  34. * The following line would cause the autoloader to attempt to load the
  35. * \Foo\Bar\Qux\Quux class from /path/to/packages/foo-bar/src/Qux/Quux.php:
  36. *
  37. * <?php
  38. * new \Foo\Bar\Qux\Quux;
  39. *
  40. * The following line would cause the autoloader to attempt to load the
  41. * \Foo\Bar\Qux\QuuxTest class from /path/to/packages/foo-bar/tests/Qux/QuuxTest.php:
  42. *
  43. * <?php
  44. * new \Foo\Bar\Qux\QuuxTest;
  45. */
  46. class Psr4AutoloaderClass
  47. {
  48. /**
  49. * An associative array where the key is a namespace prefix and the value
  50. * is an array of base directories for classes in that namespace.
  51. *
  52. * @var array
  53. */
  54. protected $prefixes = array();
  55. /**
  56. * Register loader with SPL autoloader stack.
  57. *
  58. * @return void
  59. */
  60. public function register()
  61. {
  62. spl_autoload_register(array($this, 'loadClass'));
  63. }
  64. /**
  65. * Adds a base directory for a namespace prefix.
  66. *
  67. * @param string $prefix The namespace prefix.
  68. * @param string $base_dir A base directory for class files in the
  69. * namespace.
  70. * @param bool $prepend If true, prepend the base directory to the stack
  71. * instead of appending it; this causes it to be searched first rather
  72. * than last.
  73. * @return void
  74. */
  75. public function addNamespace($prefix, $base_dir, $prepend = false)
  76. {
  77. // normalize namespace prefix
  78. $prefix = trim($prefix, '\\') . '\\';
  79. // normalize the base directory with a trailing separator
  80. $base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/';
  81. // initialize the namespace prefix array
  82. if (isset($this->prefixes[$prefix]) === false) {
  83. $this->prefixes[$prefix] = array();
  84. }
  85. // retain the base directory for the namespace prefix
  86. if ($prepend) {
  87. array_unshift($this->prefixes[$prefix], $base_dir);
  88. } else {
  89. array_push($this->prefixes[$prefix], $base_dir);
  90. }
  91. }
  92. /**
  93. * Loads the class file for a given class name.
  94. *
  95. * @param string $class The fully-qualified class name.
  96. * @return mixed The mapped file name on success, or boolean false on
  97. * failure.
  98. */
  99. public function loadClass($class)
  100. {
  101. // the current namespace prefix
  102. $prefix = $class;
  103. // work backwards through the namespace names of the fully-qualified
  104. // class name to find a mapped file name
  105. while (false !== $pos = strrpos($prefix, '\\')) {
  106. // retain the trailing namespace separator in the prefix
  107. $prefix = substr($class, 0, $pos + 1);
  108. // the rest is the relative class name
  109. $relative_class = substr($class, $pos + 1);
  110. // try to load a mapped file for the prefix and relative class
  111. $mapped_file = $this->loadMappedFile($prefix, $relative_class);
  112. if ($mapped_file) {
  113. return $mapped_file;
  114. }
  115. // remove the trailing namespace separator for the next iteration
  116. // of strrpos()
  117. $prefix = rtrim($prefix, '\\');
  118. }
  119. // never found a mapped file
  120. return false;
  121. }
  122. /**
  123. * Load the mapped file for a namespace prefix and relative class.
  124. *
  125. * @param string $prefix The namespace prefix.
  126. * @param string $relative_class The relative class name.
  127. * @return mixed Boolean false if no mapped file can be loaded, or the
  128. * name of the mapped file that was loaded.
  129. */
  130. protected function loadMappedFile($prefix, $relative_class)
  131. {
  132. // are there any base directories for this namespace prefix?
  133. if (isset($this->prefixes[$prefix]) === false) {
  134. return false;
  135. }
  136. // look through base directories for this namespace prefix
  137. foreach ($this->prefixes[$prefix] as $base_dir) {
  138. // replace the namespace prefix with the base directory,
  139. // replace namespace separators with directory separators
  140. // in the relative class name, append with .php
  141. $file = $base_dir
  142. . str_replace('\\', '/', $relative_class)
  143. . '.php';
  144. // if the mapped file exists, require it
  145. if ($this->requireFile($file)) {
  146. // yes, we're done
  147. return $file;
  148. }
  149. }
  150. // never found it
  151. return false;
  152. }
  153. /**
  154. * If a file exists, require it from the file system.
  155. *
  156. * @param string $file The file to require.
  157. * @return bool True if the file exists, false if not.
  158. */
  159. protected function requireFile($file)
  160. {
  161. if (file_exists($file)) {
  162. require $file;
  163. return true;
  164. }
  165. return false;
  166. }
  167. }