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.

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