autoloader.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Elgg autoloader
  4. * Facilities for class/interface/trait autoloading.
  5. *
  6. * @package Elgg.Core
  7. * @subpackage Autoloader
  8. */
  9. /**
  10. * @return \Elgg\Di\ServiceProvider
  11. * @access private
  12. */
  13. function _elgg_services() {
  14. static $provider;
  15. if (null === $provider) {
  16. $provider = _elgg_create_service_provider();
  17. }
  18. return $provider;
  19. }
  20. /**
  21. * Sets up autoloading and creates the service provider (DIC)
  22. *
  23. * Setup global class map and loader instances and add the core classes to the map.
  24. * We can't load this from dataroot because we don't know it yet, and we'll need
  25. * several classes before we can find out!
  26. *
  27. * @throws RuntimeException
  28. * @access private
  29. */
  30. function _elgg_create_service_provider() {
  31. $loader = new \Elgg\ClassLoader(new \Elgg\ClassMap());
  32. // until the cache can be loaded, just setup PSR-0 autoloading
  33. // out of the classes directory. No need to build a full map.
  34. $loader->register();
  35. $manager = new \Elgg\AutoloadManager($loader);
  36. return new \Elgg\Di\ServiceProvider($manager);
  37. }
  38. /**
  39. * Load cached data into the autoload system
  40. *
  41. * Note this has to wait until Elgg's data path is known.
  42. *
  43. * @access private
  44. */
  45. function _elgg_load_autoload_cache() {
  46. $manager = _elgg_services()->autoloadManager;
  47. $manager->setStorage(elgg_get_system_cache());
  48. if (! $manager->loadCache()) {
  49. $manager->addClasses(dirname(dirname(__FILE__)) . '/classes');
  50. }
  51. }
  52. /**
  53. * Save the autoload system cache
  54. *
  55. * @access private
  56. */
  57. function _elgg_save_autoload_cache() {
  58. _elgg_services()->autoloadManager->saveCache();
  59. }
  60. /**
  61. * Delete the autoload system cache
  62. *
  63. * @access private
  64. */
  65. function _elgg_delete_autoload_cache() {
  66. _elgg_services()->autoloadManager->deleteCache();
  67. }
  68. /**
  69. * Get Elgg's class loader
  70. *
  71. * @return \Elgg\ClassLoader
  72. */
  73. function elgg_get_class_loader() {
  74. return _elgg_services()->autoloadManager->getLoader();
  75. }
  76. /**
  77. * Register a directory tree for autoloading classes/interfaces/traits.
  78. *
  79. * For BC with 1.8, all .php files in the top-level directory are scanned
  80. * and added to the class map (only on the first request), then lower-level
  81. * directories are registered for standard PSR-0 autoloading.
  82. *
  83. * @param string $dir The dir to look in
  84. *
  85. * @return void
  86. * @since 1.8.0
  87. */
  88. function elgg_register_classes($dir) {
  89. _elgg_services()->autoloadManager->addClasses($dir);
  90. }
  91. /**
  92. * Register a classname to a file.
  93. *
  94. * @param string $class The name of the class
  95. * @param string $location The location of the file
  96. *
  97. * @return bool true
  98. * @since 1.8.0
  99. */
  100. function elgg_register_class($class, $location) {
  101. _elgg_services()->autoloadManager->setClassPath($class, $location);
  102. return true;
  103. }
  104. return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
  105. $events->registerHandler('shutdown', 'system', '_elgg_save_autoload_cache', 1000);
  106. $events->registerHandler('upgrade', 'all', '_elgg_delete_autoload_cache');
  107. };