PageRenderTime 24ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/system/autoload.php

https://github.com/HabariMag/habarimag-old
PHP | 71 lines | 42 code | 8 blank | 21 comment | 11 complexity | b4caaed9bf314d955f969e45743389f0 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * Attempt to load the class before PHP fails with an error.
  4. * This method is called automatically in case you are trying to use a class which hasn't been defined yet.
  5. *
  6. * We look for the undefined class in the following folders:
  7. * - /system/classes/*.php
  8. * - /system/handlers/*.php
  9. * - /user/classes/*.php
  10. * - /user/handlers/*.php
  11. * - /user/sites/x.y.z/classes/*.php
  12. * - /user/sites/x.y.z/handlers/*.php
  13. *
  14. * @param string $class_name Class called by the user
  15. */
  16. function habari_autoload( $class_name )
  17. {
  18. static $files = null;
  19. $success = false;
  20. $class_file = strtolower( $class_name ) . '.php';
  21. if ( empty( $files ) ) {
  22. $files = array();
  23. $dirs = array(
  24. HABARI_PATH . '/system/classes',
  25. HABARI_PATH . '/system/handlers',
  26. HABARI_PATH . '/user/classes',
  27. HABARI_PATH . '/user/handlers',
  28. );
  29. // For each directory, save the available files in the $files array.
  30. foreach ( $dirs as $dir ) {
  31. $glob = glob( $dir . '/*.php' );
  32. if ( $glob === false || empty( $glob ) ) continue;
  33. $fnames = array_map( create_function( '$a', 'return strtolower(basename($a));' ), $glob );
  34. $files = array_merge( $files, array_combine( $fnames, $glob ) );
  35. }
  36. // Load the Site class, a requirement to get files from a multisite directory.
  37. if ( isset( $files['site.php'] ) ) {
  38. require( $files['site.php'] );
  39. }
  40. // Verify if this Habari instance is a multisite.
  41. if ( ( $site_user_dir = Site::get_dir( 'user' ) ) != HABARI_PATH . '/user' ) {
  42. // We are dealing with a site defined in /user/sites/x.y.z
  43. // Add the available files in that directory in the $files array.
  44. $glob_classes = glob( $site_user_dir . '/classes/*.php' );
  45. $glob_handlers = glob( $site_user_dir . '/handlers/*.php' );
  46. $glob = array_merge( $glob_classes, $glob_handlers );
  47. if ( $glob !== false && !empty( $glob ) ) {
  48. $fnames = array_map( create_function( '$a', 'return strtolower(basename($a));' ), $glob );
  49. $files = array_merge( $files, array_combine( $fnames, $glob ) );
  50. }
  51. }
  52. }
  53. // Search in the available files for the undefined class file.
  54. if ( isset( $files[$class_file] ) ) {
  55. require( $files[$class_file] );
  56. // If the class has a static method named __static(), execute it now, on initial load.
  57. if ( class_exists( $class_name, false ) && method_exists( $class_name, '__static' ) ) {
  58. call_user_func( array( $class_name, '__static' ) );
  59. }
  60. $success = true;
  61. }
  62. }
  63. ?>