14 * require_once '...path/Autoloader.php';
15 * Smarty_Autoloader::register();
16 * $smarty = new Smarty();
17 * Note: This autoloader is not needed if you use Composer.
18 * Composer will automatically add the classes of the Smarty package to it common autoloader.
20 class Smarty_Autoloader
23 * Filepath to Smarty root
27 public static $SMARTY_DIR = '';
29 * Filepath to Smarty internal plugins
33 public static $SMARTY_SYSPLUGINS_DIR = '';
35 * Array of not existing classes to avoid is_file calls for already tested classes
39 public static $unknown = array();
41 * Array with Smarty core classes and their filename
45 public static $rootClasses = array('Smarty' => 'Smarty.class.php',
46 'SmartyBC' => 'SmartyBC.class.php',
49 private static $syspluginsClasses = array(
50 'smarty_config_source' => true,
51 'smarty_security' => true,
52 'smarty_cacheresource' => true,
53 'smarty_compiledresource' => true,
54 'smarty_cacheresource_custom' => true,
55 'smarty_cacheresource_keyvaluestore' => true,
56 'smarty_resource' => true,
57 'smarty_resource_custom' => true,
58 'smarty_resource_uncompiled' => true,
59 'smarty_resource_recompiled' => true,
60 'smarty_template_source' => true,
61 'smarty_template_compiled' => true,
62 'smarty_template_cached' => true,
63 'smarty_template_config' => true,
64 'smarty_data' => true,
65 'smarty_variable' => true,
66 'smarty_undefined_variable' => true,
67 'smartyexception' => true,
68 'smartycompilerexception' => true,
69 'smarty_internal_data' => true,
70 'smarty_internal_template' => true,
71 'smarty_internal_templatebase' => true,
72 'smarty_internal_resource_file' => true,
73 'smarty_internal_resource_extends' => true,
74 'smarty_internal_resource_eval' => true,
75 'smarty_internal_resource_string' => true,
76 'smarty_internal_resource_registered' => true,
77 'smarty_internal_extension_codeframe' => true,
78 'smarty_internal_extension_config' => true,
79 'smarty_internal_filter_handler' => true,
80 'smarty_internal_function_call_handler' => true,
81 'smarty_internal_cacheresource_file' => true,
82 'smarty_internal_write_file' => true,
86 * Registers Smarty_Autoloader backward compatible to older installations.
88 * @param bool $prepend Whether to prepend the autoloader or not.
90 public static function registerBC($prepend = false)
93 * register the class autoloader
95 if (!defined('SMARTY_SPL_AUTOLOAD')) {
96 define('SMARTY_SPL_AUTOLOAD', 0);
98 if (SMARTY_SPL_AUTOLOAD
&& set_include_path(get_include_path() . PATH_SEPARATOR
. SMARTY_SYSPLUGINS_DIR
) !== false) {
99 $registeredAutoLoadFunctions = spl_autoload_functions();
100 if (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
101 spl_autoload_register();
104 self
::register($prepend);
109 * Registers Smarty_Autoloader as an SPL autoloader.
111 * @param bool $prepend Whether to prepend the autoloader or not.
113 public static function register($prepend = false)
115 self
::$SMARTY_DIR = defined('SMARTY_DIR') ? SMARTY_DIR
: dirname(__FILE__
) . '/';
116 self
::$SMARTY_SYSPLUGINS_DIR = defined('SMARTY_SYSPLUGINS_DIR') ? SMARTY_SYSPLUGINS_DIR
: self
::$SMARTY_DIR . 'sysplugins/';
117 if (version_compare(phpversion(), '5.3.0', '>=')) {
118 spl_autoload_register(array(__CLASS__
, 'autoload'), true, $prepend);
120 spl_autoload_register(array(__CLASS__
, 'autoload'));
125 * Handles autoloading of classes.
127 * @param string $class A class name.
129 public static function autoload($class)
131 // Request for Smarty or already unknown class
132 if (isset(self
::$unknown[$class])) {
135 $_class = strtolower($class);
136 if (isset(self
::$syspluginsClasses[$_class])) {
137 $_class = (self
::$syspluginsClasses[$_class] === true) ?
$_class : self
::$syspluginsClasses[$_class];
138 $file = self
::$SMARTY_SYSPLUGINS_DIR . $_class . '.php';
141 } elseif (0 !== strpos($_class, 'smarty_internal_')) {
142 if (isset(self
::$rootClasses[$class])) {
143 $file = self
::$SMARTY_DIR . self
::$rootClasses[$class];
147 self
::$unknown[$class] = true;
150 $file = self
::$SMARTY_SYSPLUGINS_DIR . $_class . '.php';
151 if (is_file($file)) {
155 self
::$unknown[$class] = true;