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.

87 lines
2.3 KiB

  1. <?php
  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. * Add exclude path for autoload. Should be called before setAutoloadFrom
  17. * @static
  18. * @param array $exclude list of relative path (from project root)
  19. * @return void
  20. */
  21. static public function setExclude($exclude = array())
  22. {
  23. if (!is_array($exclude)) {
  24. $exclude = array($exclude);
  25. }
  26. self::$exclude = array_merge(self::$exclude, $exclude);
  27. }
  28. static public function setAutoloadFrom($file)
  29. {
  30. self::$file = $file;
  31. if (!file_exists(self::$file)) {
  32. self::buildAutoload();
  33. }
  34. self::$autoload = require(self::$file);
  35. spl_autoload_register(array(
  36. __CLASS__,
  37. 'autoload'));
  38. }
  39. static public function autoload($class)
  40. {
  41. if (isset(self::$autoload[$class])) {
  42. require(PATH . self::$autoload[$class]);
  43. return;
  44. }
  45. if (Config::get('DEBUG')) {
  46. if (!isset(self::$autoload[$class])) {
  47. self::buildAutoload();
  48. }
  49. if (isset(self::$autoload[$class])) {
  50. require(PATH . self::$autoload[$class]);
  51. }
  52. }
  53. }
  54. static public function getFilePath($class)
  55. {
  56. return self::$autoload[$class];
  57. }
  58. static protected function buildAutoload()
  59. {
  60. ignore_user_abort(true);
  61. $dir = dirname(self::$file);
  62. if (!file_exists($dir) && !mkdir($dir)) {
  63. trigger_error('Can\'t create directory: "' . $dir . '"', E_USER_ERROR);
  64. }
  65. $scan = array(PATH . '/' . APP . '/src', PATH . '/lib');
  66. $exclude = array_merge(self::$exclude, array(PATH . '/.git', PATH . '/lib/core/tests', PATH . '/lib/core/.git'));
  67. if (!self::$builder) {
  68. require_once(PATH . '/lib/core/util/AutoloadBuilder.php');
  69. require_once(PATH . '/lib/core/util/AsciiSortedIterator.php');
  70. self::$builder = new AutoloadBuilder(self::$file, $scan, $exclude);
  71. }
  72. self::$builder->build();
  73. ignore_user_abort(false);
  74. }
  75. }