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.

Autoloader.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Smarty Autoloader
  4. *
  5. * @package Smarty
  6. */
  7. /**
  8. * Smarty Autoloader
  9. *
  10. * @package Smarty
  11. * @author Uwe Tews
  12. * Usage:
  13. * require_once '...path/Autoloader.php';
  14. * Smarty_Autoloader::register();
  15. * or
  16. * include '...path/bootstrap.php';
  17. *
  18. * $smarty = new Smarty();
  19. */
  20. class Smarty_Autoloader
  21. {
  22. /**
  23. * Filepath to Smarty root
  24. *
  25. * @var string
  26. */
  27. public static $SMARTY_DIR = null;
  28. /**
  29. * Filepath to Smarty internal plugins
  30. *
  31. * @var string
  32. */
  33. public static $SMARTY_SYSPLUGINS_DIR = null;
  34. /**
  35. * Array with Smarty core classes and their filename
  36. *
  37. * @var array
  38. */
  39. public static $rootClasses = array('smarty' => 'Smarty.class.php', 'smartybc' => 'SmartyBC.class.php',);
  40. /**
  41. * Registers Smarty_Autoloader backward compatible to older installations.
  42. *
  43. * @param bool $prepend Whether to prepend the autoloader or not.
  44. */
  45. public static function registerBC($prepend = false)
  46. {
  47. /**
  48. * register the class autoloader
  49. */
  50. if (!defined('SMARTY_SPL_AUTOLOAD')) {
  51. define('SMARTY_SPL_AUTOLOAD', 0);
  52. }
  53. if (SMARTY_SPL_AUTOLOAD &&
  54. set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false
  55. ) {
  56. $registeredAutoLoadFunctions = spl_autoload_functions();
  57. if (!isset($registeredAutoLoadFunctions[ 'spl_autoload' ])) {
  58. spl_autoload_register();
  59. }
  60. } else {
  61. self::register($prepend);
  62. }
  63. }
  64. /**
  65. * Registers Smarty_Autoloader as an SPL autoloader.
  66. *
  67. * @param bool $prepend Whether to prepend the autoloader or not.
  68. */
  69. public static function register($prepend = false)
  70. {
  71. self::$SMARTY_DIR = defined('SMARTY_DIR') ? SMARTY_DIR : dirname(__FILE__) . DIRECTORY_SEPARATOR;
  72. self::$SMARTY_SYSPLUGINS_DIR = defined('SMARTY_SYSPLUGINS_DIR') ? SMARTY_SYSPLUGINS_DIR :
  73. self::$SMARTY_DIR . 'sysplugins' . DIRECTORY_SEPARATOR;
  74. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  75. spl_autoload_register(array(__CLASS__, 'autoload'), true, $prepend);
  76. } else {
  77. spl_autoload_register(array(__CLASS__, 'autoload'));
  78. }
  79. }
  80. /**
  81. * Handles auto loading of classes.
  82. *
  83. * @param string $class A class name.
  84. */
  85. public static function autoload($class)
  86. {
  87. if ($class[ 0 ] !== 'S' && strpos($class, 'Smarty') !== 0) {
  88. return;
  89. }
  90. $_class = strtolower($class);
  91. if (isset(self::$rootClasses[ $_class ])) {
  92. $file = self::$SMARTY_DIR . self::$rootClasses[ $_class ];
  93. if (is_file($file)) {
  94. include $file;
  95. }
  96. } else {
  97. $file = self::$SMARTY_SYSPLUGINS_DIR . $_class . '.php';
  98. if (is_file($file)) {
  99. include $file;
  100. }
  101. }
  102. return;
  103. }
  104. }