aboutsummaryrefslogtreecommitdiffstats
path: root/library/Smarty/libs/Autoloader.php
blob: a24cad6f7e65adf226708eb7ebb81c638b3feaa0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
 * Smarty Autoloader
 *
 * @package    Smarty
 */

/**
 * Smarty Autoloader
 *
 * @package    Smarty
 * @author     Uwe Tews
 *             Usage:
 *             require_once '...path/Autoloader.php';
 *             Smarty_Autoloader::register();
 *             $smarty = new Smarty();
 *             Note:       This autoloader is not needed if you use Composer.
 *             Composer will automatically add the classes of the Smarty package to it common autoloader.
 */
class Smarty_Autoloader
{
    /**
     * Filepath to Smarty root
     *
     * @var string
     */
    public static $SMARTY_DIR = '';
    /**
     * Filepath to Smarty internal plugins
     *
     * @var string
     */
    public static $SMARTY_SYSPLUGINS_DIR = '';
    /**
     * Array of not existing classes to avoid is_file calls for  already tested classes
     *
     * @var array
     */
    public static $unknown = array();
    /**
     * Array with Smarty core classes and their filename
     *
     * @var array
     */
    public static $rootClasses = array('Smarty'   => 'Smarty.class.php',
                                       'SmartyBC' => 'SmartyBC.class.php',
    );

    private static $syspluginsClasses = array(
        'smarty_config_source'                  => true,
        'smarty_security'                       => true,
        'smarty_cacheresource'                  => true,
        'smarty_compiledresource'               => true,
        'smarty_cacheresource_custom'           => true,
        'smarty_cacheresource_keyvaluestore'    => true,
        'smarty_resource'                       => true,
        'smarty_resource_custom'                => true,
        'smarty_resource_uncompiled'            => true,
        'smarty_resource_recompiled'            => true,
        'smarty_template_source'                => true,
        'smarty_template_compiled'              => true,
        'smarty_template_cached'                => true,
        'smarty_template_config'                => true,
        'smarty_data'                           => true,
        'smarty_variable'                       => true,
        'smarty_undefined_variable'             => true,
        'smartyexception'                       => true,
        'smartycompilerexception'               => true,
        'smarty_internal_data'                  => true,
        'smarty_internal_template'              => true,
        'smarty_internal_templatebase'          => true,
        'smarty_internal_resource_file'         => true,
        'smarty_internal_resource_extends'      => true,
        'smarty_internal_resource_eval'         => true,
        'smarty_internal_resource_string'       => true,
        'smarty_internal_resource_registered'   => true,
        'smarty_internal_extension_codeframe'   => true,
        'smarty_internal_extension_config'      => true,
        'smarty_internal_filter_handler'        => true,
        'smarty_internal_function_call_handler' => true,
        'smarty_internal_cacheresource_file'    => true,
        'smarty_internal_write_file'    => true,
    );

    /**
     * Registers Smarty_Autoloader backward compatible to older installations.
     *
     * @param bool $prepend Whether to prepend the autoloader or not.
     */
    public static function registerBC($prepend = false)
    {
        /**
         * register the class autoloader
         */
        if (!defined('SMARTY_SPL_AUTOLOAD')) {
            define('SMARTY_SPL_AUTOLOAD', 0);
        }
        if (SMARTY_SPL_AUTOLOAD && set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false) {
            $registeredAutoLoadFunctions = spl_autoload_functions();
            if (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
                spl_autoload_register();
            }
        } else {
            self::register($prepend);
        }
    }

    /**
     * Registers Smarty_Autoloader as an SPL autoloader.
     *
     * @param bool $prepend Whether to prepend the autoloader or not.
     */
    public static function register($prepend = false)
    {
        self::$SMARTY_DIR = defined('SMARTY_DIR') ? SMARTY_DIR : dirname(__FILE__) . '/';
        self::$SMARTY_SYSPLUGINS_DIR = defined('SMARTY_SYSPLUGINS_DIR') ? SMARTY_SYSPLUGINS_DIR : self::$SMARTY_DIR . 'sysplugins/';
        if (version_compare(phpversion(), '5.3.0', '>=')) {
            spl_autoload_register(array(__CLASS__, 'autoload'), true, $prepend);
        } else {
            spl_autoload_register(array(__CLASS__, 'autoload'));
        }
    }

    /**
     * Handles autoloading of classes.
     *
     * @param string $class A class name.
     */
    public static function autoload($class)
    {
        // Request for Smarty or already unknown class
        if (isset(self::$unknown[$class])) {
            return;
        }
        $_class = strtolower($class);
        if (isset(self::$syspluginsClasses[$_class])) {
            $_class = (self::$syspluginsClasses[$_class] === true) ? $_class : self::$syspluginsClasses[$_class];
            $file = self::$SMARTY_SYSPLUGINS_DIR . $_class . '.php';
            require_once $file;
            return;
        } elseif (0 !== strpos($_class, 'smarty_internal_')) {
            if (isset(self::$rootClasses[$class])) {
                $file = self::$SMARTY_DIR . self::$rootClasses[$class];
                require_once $file;
                return;
            }
            self::$unknown[$class] = true;
            return;
        }
        $file = self::$SMARTY_SYSPLUGINS_DIR . $_class . '.php';
        if (is_file($file)) {
            require_once $file;
            return;
        }
        self::$unknown[$class] = true;
        return;
    }
}