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.

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