Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Autoloader.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. $file = self::$SMARTY_SYSPLUGINS_DIR . $_class . '.php';
  88. if (strpos($_class, 'smarty_internal_') === 0) {
  89. if (strpos($_class, 'smarty_internal_compile_') === 0) {
  90. if (is_file($file)) {
  91. require $file;
  92. }
  93. return;
  94. }
  95. @include $file;
  96. return;
  97. }
  98. if (preg_match('/^(smarty_(((template_(source|config|cache|compiled|resource_base))|((cached|compiled)?resource)|(variable|security)))|(smarty(bc)?)$)/',
  99. $_class, $match)) {
  100. if (!empty($match[3])) {
  101. @include $file;
  102. return;
  103. } elseif (!empty($match[9]) && isset(self::$rootClasses[$_class])) {
  104. $file = self::$rootClasses[$_class];
  105. require $file;
  106. return;
  107. }
  108. }
  109. if (0 !== strpos($_class, 'smarty')) {
  110. return;
  111. }
  112. if (is_file($file)) {
  113. require $file;
  114. return;
  115. }
  116. return;
  117. }
  118. }