PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 6ms app.codeStats 0ms

/library/XenForo/Autoloader.php

https://github.com/hanguyenhuu/DTUI_201105
PHP | 177 lines | 83 code | 22 blank | 72 comment | 13 complexity | cb234e3409a78dfd0bb4849dce9a9ca5 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * A constant that must be made available for any other XenForo class to be included.
  4. *
  5. * @var boolean
  6. */
  7. define('XENFORO_AUTOLOADER_SETUP', true);
  8. /**
  9. * Base XenForo autoloader class. This must be the first class loaded and setup as the
  10. * application/registry depends on it for loading classes.
  11. *
  12. * @package XenForo_Core
  13. */
  14. class XenForo_Autoloader
  15. {
  16. /**
  17. * Instance manager.
  18. *
  19. * @var XenForo_Autoloader
  20. */
  21. private static $_instance;
  22. /**
  23. * Path to directory containing the application's library.
  24. *
  25. * @var string
  26. */
  27. protected $_rootDir = '.';
  28. /**
  29. * Stores whether the autoloader has been setup yet.
  30. *
  31. * @var boolean
  32. */
  33. protected $_setup = false;
  34. /**
  35. * Protected constructor. Use {@link getInstance()} instead.
  36. */
  37. protected function __construct()
  38. {
  39. }
  40. /**
  41. * Setup the autoloader. This causes the environment to be setup as necessary.
  42. *
  43. * @param string Path to application library directory. See {@link $_rootDir}
  44. */
  45. public function setupAutoloader($rootDir)
  46. {
  47. if ($this->_setup)
  48. {
  49. return;
  50. }
  51. $this->_rootDir = $rootDir;
  52. $this->_setupAutoloader();
  53. $this->_setup = true;
  54. }
  55. /**
  56. * Internal method that actually applies the autoloader. See {@link setupAutoloader()}
  57. * for external usage.
  58. */
  59. protected function _setupAutoloader()
  60. {
  61. if (@ini_get('open_basedir'))
  62. {
  63. // many servers don't seem to set include_path correctly with open_basedir, so don't use it
  64. set_include_path($this->_rootDir . PATH_SEPARATOR . '.');
  65. }
  66. else
  67. {
  68. set_include_path($this->_rootDir . PATH_SEPARATOR . '.' . PATH_SEPARATOR . get_include_path());
  69. }
  70. /*require_once('Zend/Loader/Autoloader.php');
  71. $autoloader = Zend_Loader_Autoloader::getInstance();
  72. $autoloader->pushAutoloader(array($this, 'autoload'));*/
  73. spl_autoload_register(array($this, 'autoload'));
  74. }
  75. /**
  76. * Autoload the specified class.
  77. *
  78. * @param string $class Name of class to autoload
  79. *
  80. * @return boolean
  81. */
  82. public function autoload($class)
  83. {
  84. if (class_exists($class, false) || interface_exists($class, false))
  85. {
  86. return true;
  87. }
  88. if ($class == 'utf8_entity_decoder')
  89. {
  90. return true;
  91. }
  92. if (substr($class, 0, 5) == 'XFCP_')
  93. {
  94. throw new XenForo_Exception('Cannot load class using XFCP. Load the class using the correct loader first.');
  95. }
  96. $filename = $this->autoloaderClassToFile($class);
  97. if (!$filename)
  98. {
  99. return false;
  100. }
  101. if (file_exists($filename))
  102. {
  103. include($filename);
  104. return (class_exists($class, false) || interface_exists($class, false));
  105. }
  106. return false;
  107. }
  108. /**
  109. * Resolves a class name to an autoload path.
  110. *
  111. * @param string Name of class to autoload
  112. *
  113. * @return string|false False if the class contains invalid characters.
  114. */
  115. public function autoloaderClassToFile($class)
  116. {
  117. if (preg_match('#[^a-zA-Z0-9_]#', $class))
  118. {
  119. return false;
  120. }
  121. return $this->_rootDir . '/' . str_replace('_', '/', $class) . '.php';
  122. }
  123. /**
  124. * Gets the autoloader's root directory.
  125. *
  126. * @return string
  127. */
  128. public function getRootDir()
  129. {
  130. return $this->_rootDir;
  131. }
  132. /**
  133. * Gets the autoloader instance.
  134. *
  135. * @return XenForo_Autoloader
  136. */
  137. public static final function getInstance()
  138. {
  139. if (!self::$_instance)
  140. {
  141. self::$_instance = new self();
  142. }
  143. return self::$_instance;
  144. }
  145. /**
  146. * Manually sets the autoloader instance. Use this to inject a modified version.
  147. *
  148. * @param XenForo_Autoloader|null
  149. */
  150. public static function setInstance(XenForo_Autoloader $loader = null)
  151. {
  152. self::$_instance = $loader;
  153. }
  154. }