/autoloader/Autoloader.php

https://github.com/ThaDeanesta/Hydrogen · PHP · 120 lines · 49 code · 10 blank · 61 comment · 5 complexity · a8624f6a23cc5419e24315eba3a09126 MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright (c) 2009 - 2011, Frosted Design
  4. * All rights reserved.
  5. */
  6. namespace hydrogen\autoloader;
  7. use hydrogen\config\Config;
  8. /**
  9. * The Autoloader class extends Hydrogen's class autoloading capabilities to
  10. * any other namespace within a PHP app.
  11. */
  12. class Autoloader {
  13. protected static $namespaces = array();
  14. protected static $registered = false;
  15. /**
  16. * Load a specified class from its PHP file. This function should only
  17. * ever be called by the PHP autoloading system, and registered through
  18. * the {@link #register} function.
  19. *
  20. * @param string $class The full namespace/class string of the class
  21. * to be loaded.
  22. * @return boolean true if the class was loaded; false otherwise.
  23. */
  24. public static function loadClass($class) {
  25. $class = ltrim($class, '\\');
  26. $rootNamespace = '';
  27. if ($firstSlash = strpos($class, '\\'))
  28. $rootNamespace = substr($class, 0, $firstSlash);
  29. if (isset(static::$namespaces[$rootNamespace])) {
  30. $subNamespace = '';
  31. if ($lastSlash = strrpos($class, '\\')) {
  32. $fileName = substr($class, $lastSlash + 1);
  33. if ($firstSlash < $lastSlash) {
  34. $subNamespace = substr($class, $firstSlash + 1,
  35. $lastSlash - $firstSlash);
  36. }
  37. }
  38. else
  39. $fileName = $class;
  40. $path = static::$namespaces[$rootNamespace][0];
  41. $path .= DIRECTORY_SEPARATOR;
  42. $path .= str_replace('\\', DIRECTORY_SEPARATOR, $subNamespace);
  43. $path .= (static::$namespaces[$rootNamespace][1] ?
  44. str_replace('_', DIRECTORY_SEPARATOR, $fileName) :
  45. $fileName) . '.php';
  46. return !!include($path);
  47. }
  48. return false;
  49. }
  50. /**
  51. * Registers this autoloader and makes it active. This should only
  52. * need to be called in hydrogen.inc.php.
  53. *
  54. * @return boolean true if the autoloader was successfully registered;
  55. * false if it was not, or if it has already been registered in the
  56. * past.
  57. */
  58. public static function register() {
  59. if (static::$registered)
  60. return false;
  61. static::$registered = spl_autoload_register(
  62. '\hydrogen\autoloader\Autoloader::loadClass');
  63. return static::$registered;
  64. }
  65. /**
  66. * Registers a new namespace with the autoloader. As soon as this
  67. * function is called, classes within that namespace (or within
  68. * child namespaces within that namespace) will be autoloaded.
  69. *
  70. * Note that it's expected that the namespace being added will follow the
  71. * proper convention for file organization: the folder structure should
  72. * match the namespace exactly, so that a class named something like
  73. * myapp\models\exceptions\UserNotFoundException would be found in
  74. * models/exceptions/UserNotFoundException.php within the root folder
  75. * for the 'myapp' namespace.
  76. *
  77. * The Autoloader also supports replacing underscores in a filename with
  78. * directory separators, which is useful for compatibility with old code
  79. * written with a Zend-style autoloader. This feature is optional on a
  80. * per-namespace basis.
  81. *
  82. * The root folder supplied to this function should point to the first
  83. * folder of the namespace itself. For example, if the above PHP file were
  84. * located at this path:
  85. * [webroot]/lib/myapp/models/exceptions/UserNotFoundException.php
  86. * then the namespace would be 'myapp' and the root folder would be
  87. * 'lib/myapp'.
  88. *
  89. * @param string $namespace The root namespace for which classes should be
  90. * autoloaded.
  91. * @param string $rootFolder The folder that the namespace's files reside
  92. * in. Relative paths will be evaluated relative to the base path set
  93. * in the autoconfig.
  94. * @param boolean $replaceUnderscores true to replace any underscores in
  95. * the class name with directory separators when loading; false to
  96. * treat them as part of the file name.
  97. */
  98. public static function registerNamespace($namespace, $rootFolder,
  99. $replaceUnderscores=true) {
  100. $rootFolder = Config::getAbsolutePath($rootFolder);
  101. $rootFolder = rtrim($rootFolder, DIRECTORY_SEPARATOR);
  102. static::$namespaces[$namespace] = array($rootFolder,
  103. $replaceUnderscores);
  104. }
  105. /**
  106. * The Autoloader should not be instantiated.
  107. */
  108. protected function __construct() {}
  109. }
  110. ?>