Autoloader.php 1002 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace Workerman;
  3. if(!defined('WORKERMAN_ROOT_DIR'))
  4. {
  5. define('WORKERMAN_ROOT_DIR', realpath(__DIR__ . '/../'));
  6. }
  7. require_once WORKERMAN_ROOT_DIR.'/Workerman/Lib/Constants.php';
  8. class Autoloader
  9. {
  10. protected static $_appInitPath = '';
  11. public static function setRootPath($root_path)
  12. {
  13. self::$_appInitPath = $root_path;
  14. spl_autoload_register('\GatewayWorker\Lib\Autoloader::loadByNamespace');
  15. }
  16. public static function loadByNamespace($name)
  17. {
  18. $class_path = str_replace('\\', DIRECTORY_SEPARATOR ,$name);
  19. $class_file = self::$_appInitPath . '/' . $class_path.'.php';
  20. if(is_file($class_file))
  21. {
  22. $class_file = WORKERMAN_ROOT_DIR . DIRECTORY_SEPARATOR . "$class_path.php";
  23. }
  24. if(is_file($class_file))
  25. {
  26. require_once($class_file);
  27. if(class_exists($name, false))
  28. {
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. }