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