diff --git a/change_log.txt b/change_log.txt index e8d514ba..d750c441 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,7 @@ ===== 3.1.22-dev ===== (xx.xx.2014) + 13.11.2014 + - improvement move autoload code into Autoloader.php. Use Composer autoloader when possible + 12.11.2014 - new feature added support of namespaces to template code diff --git a/composer.json b/composer.json index c84b5951..f744cbc7 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "classmap": [ "libs/Smarty.class.php", "libs/SmartyBC.class.php", - "libs/sysplugins/smarty_security.php" + "libs/sysplugins/" ] }, "extra": { diff --git a/libs/Autoloader.php b/libs/Autoloader.php new file mode 100644 index 00000000..6479681e --- /dev/null +++ b/libs/Autoloader.php @@ -0,0 +1,112 @@ + 'Smarty.class.php', + 'SmartyBC' => 'SmartyBC.class.php', + ); + + /** + * 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 (0 !== strpos($class, 'Smarty') || isset(self::$unknown[$class])) { + return; + } + if (isset(self::$rootClasses[$class]) && is_file($file = self::$SMARTY_DIR . self::$rootClasses[$class])) { + require $file; + return; + } + $_class = strtolower($class); + if (is_file($file = self::$SMARTY_SYSPLUGINS_DIR . $_class . '.php')) { + require $file; + } else { + self::$unknown[$class] = true; + } + } +} diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 335e50f2..4aa9e5f8 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -2,7 +2,7 @@ /** * Project: Smarty: the PHP compiling template engine * File: Smarty.class.php - * SVN: $Id: Smarty.class.php 4897 2014-10-14 22:29:58Z Uwe.Tews@googlemail.com $ + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either @@ -70,33 +70,25 @@ if (!defined('SMARTY_RESOURCE_DATE_FORMAT')) { } /** - * register the class autoloader + * Try loading the Smmarty_Internal_Data class + * + * If we fail we must load Smarty's autoloader. + * Otherwise we may have a global autoloader like Composer */ -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 { - spl_autoload_register('smartyAutoload'); +if (!class_exists('Smarty_Internal_Data', true)) { + require 'Autoloader.php'; + Smarty_Autoloader::registerBC(); + require SMARTY_SYSPLUGINS_DIR . 'smarty_internal_data.php'; } /** * Load always needed external class files */ -/** -include_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_data.php'; -include_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_templatebase.php'; -include_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_template.php'; -include_once SMARTY_SYSPLUGINS_DIR . 'smarty_resource.php'; -include_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_resource_file.php'; -include_once SMARTY_SYSPLUGINS_DIR . 'smarty_cacheresource.php'; -include_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_cacheresource_file.php'; - * / +require SMARTY_SYSPLUGINS_DIR . 'smarty_internal_templatebase.php'; +require SMARTY_SYSPLUGINS_DIR . 'smarty_internal_template.php'; +require SMARTY_SYSPLUGINS_DIR . 'smarty_resource.php'; +require SMARTY_SYSPLUGINS_DIR . 'smarty_internal_resource_file.php'; + /** * This is the main Smarty class @@ -1595,30 +1587,3 @@ class Smarty extends Smarty_Internal_TemplateBase restore_error_handler(); } } - - -/** - * Autoloader - */ -function smartyAutoload($class) -{ - $_class = strtolower($class); - static $_classes = array( - 'smarty_config_source' => true, - 'smarty_config_compiled' => true, - 'smarty_security' => true, - 'smarty_cacheresource' => true, - 'smarty_cacheresource_custom' => true, - 'smarty_cacheresource_keyvaluestore' => true, - 'smarty_resource' => true, - 'smarty_resource_custom' => true, - 'smarty_resource_uncompiled' => true, - 'smarty_resource_recompiled' => true, - 'smartyexception' => true, - 'smartycompilerexception' => true, - ); - - if (!strncmp($_class, 'smarty_internal_', 16) || isset($_classes[$_class])) { - include SMARTY_SYSPLUGINS_DIR . $_class . '.php'; - } -}