library/smarty/libs/Autoloader.php
changeset 0 4869aea77e21
child 7 3ed6f90e97d9
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     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  *             $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.
       
    19  */
       
    20 class Smarty_Autoloader {
       
    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 of not existing classes to avoid is_file calls for  already tested classes
       
    35      *
       
    36      * @var array
       
    37      */
       
    38     public static $unknown = array();
       
    39     /**
       
    40      * Array with Smarty core classes and their filename
       
    41      *
       
    42      * @var array
       
    43      */
       
    44     public static $rootClasses = array('Smarty' => 'Smarty.class.php',
       
    45         'SmartyBC' => 'SmartyBC.class.php',
       
    46     );
       
    47 
       
    48     private static $syspluginsClasses = array(
       
    49         'smarty_config_source' => true,
       
    50         'smarty_security' => true,
       
    51         'smarty_cacheresource' => true,
       
    52         'smarty_compiledresource' => true,
       
    53         'smarty_cacheresource_custom' => true,
       
    54         'smarty_cacheresource_keyvaluestore' => true,
       
    55         'smarty_resource' => true,
       
    56         'smarty_resource_custom' => true,
       
    57         'smarty_resource_uncompiled' => true,
       
    58         'smarty_resource_recompiled' => true,
       
    59         'smarty_template_source' => true,
       
    60         'smarty_template_compiled' => true,
       
    61         'smarty_template_cached' => true,
       
    62         'smarty_template_config' => true,
       
    63         'smarty_data' => true,
       
    64         'smarty_variable' => true,
       
    65         'smarty_undefined_variable' => true,
       
    66         'smartyexception' => true,
       
    67         'smartycompilerexception' => true,
       
    68         'smarty_internal_data' => true,
       
    69         'smarty_internal_template' => true,
       
    70         'smarty_internal_templatebase' => true,
       
    71         'smarty_internal_resource_file' => true,
       
    72         'smarty_internal_resource_extends' => true,
       
    73         'smarty_internal_resource_eval' => true,
       
    74         'smarty_internal_resource_string' => true,
       
    75         'smarty_internal_resource_registered' => true,
       
    76         'smarty_internal_extension_codeframe' => true,
       
    77         'smarty_internal_extension_config' => true,
       
    78         'smarty_internal_filter_handler' => true,
       
    79         'smarty_internal_function_call_handler' => true,
       
    80         'smarty_internal_cacheresource_file' => true,
       
    81         'smarty_internal_write_file' => true,
       
    82     );
       
    83 
       
    84     /**
       
    85      * Registers Smarty_Autoloader backward compatible to older installations.
       
    86      *
       
    87      * @param bool $prepend Whether to prepend the autoloader or not.
       
    88      */
       
    89     public static function registerBC($prepend = false) {
       
    90         /**
       
    91          * register the class autoloader
       
    92          */
       
    93         if (!defined('SMARTY_SPL_AUTOLOAD')) {
       
    94             define('SMARTY_SPL_AUTOLOAD', 0);
       
    95         }
       
    96         if (SMARTY_SPL_AUTOLOAD && set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false) {
       
    97             $registeredAutoLoadFunctions = spl_autoload_functions();
       
    98             if (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
       
    99                 spl_autoload_register();
       
   100             }
       
   101         } else {
       
   102             self::register($prepend);
       
   103         }
       
   104     }
       
   105 
       
   106     /**
       
   107      * Registers Smarty_Autoloader as an SPL autoloader.
       
   108      *
       
   109      * @param bool $prepend Whether to prepend the autoloader or not.
       
   110      */
       
   111     public static function register($prepend = false) {
       
   112         self::$SMARTY_DIR = defined('SMARTY_DIR') ? SMARTY_DIR : dirname(__FILE__) . '/';
       
   113         self::$SMARTY_SYSPLUGINS_DIR = defined('SMARTY_SYSPLUGINS_DIR') ? SMARTY_SYSPLUGINS_DIR : self::$SMARTY_DIR . 'sysplugins/';
       
   114         if (version_compare(phpversion(), '5.3.0', '>=')) {
       
   115             spl_autoload_register(array(__CLASS__, 'autoload'), true, $prepend);
       
   116         } else {
       
   117             spl_autoload_register(array(__CLASS__, 'autoload'));
       
   118         }
       
   119     }
       
   120 
       
   121     /**
       
   122      * Handles autoloading of classes.
       
   123      *
       
   124      * @param string $class A class name.
       
   125      */
       
   126     public static function autoload($class) {
       
   127         // Request for Smarty or already unknown class
       
   128         if (isset(self::$unknown[$class])) {
       
   129             return;
       
   130         }
       
   131         $_class = strtolower($class);
       
   132         if (isset(self::$syspluginsClasses[$_class])) {
       
   133             $_class = (self::$syspluginsClasses[$_class] === true) ? $_class : self::$syspluginsClasses[$_class];
       
   134             $file = self::$SMARTY_SYSPLUGINS_DIR . $_class . '.php';
       
   135             require_once $file;
       
   136             return;
       
   137         } elseif (0 !== strpos($_class, 'smarty_internal_')) {
       
   138             if (isset(self::$rootClasses[$class])) {
       
   139                 $file = self::$SMARTY_DIR . self::$rootClasses[$class];
       
   140                 require_once $file;
       
   141                 return;
       
   142             }
       
   143             self::$unknown[$class] = true;
       
   144             return;
       
   145         }
       
   146         $file = self::$SMARTY_SYSPLUGINS_DIR . $_class . '.php';
       
   147         if (is_file($file)) {
       
   148             require_once $file;
       
   149             return;
       
   150         }
       
   151         self::$unknown[$class] = true;
       
   152         return;
       
   153     }
       
   154 }