Autoloader.php 3.1 KB

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