You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.7 KiB

11 years ago
11 years ago
11 years ago
  1. <?php namespace Majestic;
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage Load
  7. * @since 2010-02-24
  8. */
  9. class Load
  10. {
  11. static protected $file;
  12. static protected $autoload;
  13. static protected $exclude = array();
  14. static protected $builder = null;
  15. /**
  16. * @var \Composer\Autoload\ClassLoader
  17. */
  18. static protected $class_loader;
  19. static public function setClassLoader($class_loader) {
  20. self::$class_loader = $class_loader;
  21. }
  22. /**
  23. * Add exclude path for autoload. Should be called before setAutoloadFrom
  24. * @static
  25. * @param array $exclude list of relative path (from project root)
  26. * @return void
  27. */
  28. static public function setExclude($exclude = array())
  29. {
  30. if (!is_array($exclude)) {
  31. $exclude = array($exclude);
  32. }
  33. self::$exclude = array_merge(self::$exclude, $exclude);
  34. }
  35. static public function setAutoloadFrom($file)
  36. {
  37. self::$file = $file;
  38. if (!file_exists(self::$file)) {
  39. self::buildAutoload();
  40. }
  41. self::$autoload = require(self::$file);
  42. spl_autoload_register(array(
  43. __CLASS__,
  44. 'autoload'));
  45. }
  46. static public function autoload($class)
  47. {
  48. if (isset(self::$autoload[$class])) {
  49. require(PATH . self::$autoload[$class]);
  50. return;
  51. }
  52. if (Config::get('DEBUG')) {
  53. if (!isset(self::$autoload[$class])) {
  54. self::buildAutoload();
  55. }
  56. if (isset(self::$autoload[$class])) {
  57. require(PATH . self::$autoload[$class]);
  58. }
  59. }
  60. }
  61. static public function getFilePath($class)
  62. {
  63. if (self::$class_loader) {
  64. return self::$class_loader->findFile($class);
  65. } else {
  66. return self::$autoload[$class];
  67. }
  68. }
  69. static protected function buildAutoload()
  70. {
  71. ignore_user_abort(true);
  72. $dir = dirname(self::$file);
  73. if (!file_exists($dir) && !mkdir($dir)) {
  74. trigger_error('Can\'t create directory: "' . $dir . '"', E_USER_ERROR);
  75. }
  76. $scan = array(PATH . '/' . APP . '/src', PATH . '/lib', PATH . '/vendor');
  77. $exclude = array_merge(self::$exclude, array(PATH . '/.git', PATH . '/lib/core/tests', PATH . '/lib/core/.git'));
  78. if (!self::$builder) {
  79. require_once(PATH . '/lib/core/util/AutoloadBuilder.php');
  80. require_once(PATH . '/lib/core/util/AsciiSortedIterator.php');
  81. self::$builder = new AutoloadBuilder(self::$file, $scan, $exclude);
  82. }
  83. self::$builder->build();
  84. ignore_user_abort(false);
  85. }
  86. }