move autoload code into Autoloader.php.

Use Composer autoloader when possible
This commit is contained in:
Uwe Tews
2014-11-13 19:42:01 +01:00
parent 0b21213c55
commit 0b6af0add7
4 changed files with 130 additions and 50 deletions

View File

@@ -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

View File

@@ -31,7 +31,7 @@
"classmap": [
"libs/Smarty.class.php",
"libs/SmartyBC.class.php",
"libs/sysplugins/smarty_security.php"
"libs/sysplugins/"
]
},
"extra": {

112
libs/Autoloader.php Normal file
View File

@@ -0,0 +1,112 @@
<?php
/**
* Smarty Autoloader
*
* @package Smarty
*/
/**
* Smarty Autoloader
*
* @package Smarty
* @author Uwe Tews
*
* Usage:
* include '...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',
);
/**
* 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;
}
}
}

View File

@@ -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';
}
}