首先看一下JLoad的setup()方法,这个方法是一个静态的方法。他的实现代码非常的简单
/**
* Method to setup the autoloaders for the Joomla Platform. Since the SPL autoloaders are
* called in a queue we will add our explicit, class-registration based loader first, then
* fall back on the autoloader based on conventions. This will allow people to register a
* class in a specific location and override platform libraries as was previously possible.
*
* @return void
*
* @since 11.3
*/
public static function setup()
{
//注册基本的库文件的路径
// Register the base path for Joomla platform libraries.
self::registerPrefix('J', JPATH_PLATFORM . '/joomla');
// Register the autoloader functions.
//向Zend Engine注册自动状态函数
//主要是为了解决自动加载函数问题
spl_autoload_register(array('JLoader', 'load'));
spl_autoload_register(array('JLoader', '_autoload'));
}
在这里做的事情就是想系统注册两个自动加载的函数,如果我们需要加载一个类,这个类又没有实现。那么系统就会自动调用JLoader的load和_autoload方法。关于spl_autoload_register方法的说明,请参考本站的相关文章。
基本理解了setup的功能之后,我们在来看看两个全局函数jexit和jimport.请注意,这是全局函数,可以在任何地方使用。
//这里导入了全局的应用程序退出
function jexit($message = 0)
{
exit($message);
}
/**
* Intelligent file importer.
*
* @param string $path A dot syntax path.
*
* @return boolean True on success.
*
* @since 11.1
*/
//这两个是全局函数
function jimport($path)
{
return JLoader::import($path);
}
关注一下jexit函数,这个函数有一个参数$message。默认为0.
下面在看一下该类的两个成员变量。
/** * Container for already imported library paths. * * @var array * @since 11.1 */ //这个属性中包含了已经加载的类,这个数组的键是类的名称,值是类的实现文件的全路径 protected static $classes = array(); /** * Container for already imported library paths. * * @var array * @since 11.1 */ //包含已经加载的库文件的路径 protected static $imported = array(); /** * Container for registered library class prefixes and path lookups. * * @var array * @since 12.1 */ //包含注册的类库的前缀和查找的路径 protected static $prefixes = array();
理解这3个成员变量的含义至关重要。
$class 这个属性中包含了已经加载的类,这个数组的键是类的名称,值是类的实现文件的全路径.如果理解了这个,那么你就很容易理解下面的代码了。
/**
* Load the file for a class.
*
* @param string $class The class to be loaded.
*
* @return boolean True on success
*
* @since 11.1
*/
//加载一个类的实现文件
public static function load($class)
{
// Sanitize class name.
$class = strtolower($class);
// If the class already exists do nothing.
if (class_exists($class))
{
return true;
}
// If the class is registered include the file.
if (isset(self::$classes[$class]))
{
include_once self::$classes[$class];
return true;
}
return false;
}
load函数的实现原理就是首先检查类是否存在,如果不存在就在$classes中去查找,如果查找到了,就include进来。
还有一个函数的实现和load函数的实现基本类似。也是基于$classes这个成员变量的。那就是register函数
/**
* Directly register a class to the autoload list.
*
* @param string $class The class name to register.
* @param string $path Full path to the file that holds the class to register.
* @param boolean $force True to overwrite the autoload path value for the class if it already exists.
*
* @return void
*
* @since 11.1
*/
//添加一个类到 autoload list中
public static function register($class, $path, $force = true)
{
// Sanitize class name.
$class = strtolower($class);
// Only attempt to register the class if the name and file exist.
if (!empty($class) && is_file($path))
{
// Register the class with the autoloader if not already registered or the force flag is set.
if (empty(self::$classes[$class]) || $force)
{
self::$classes[$class] = $path;
}
}
}
这个函数的作用就是向自动注册函数的数组中加入一个元素。以类名和路径的键值形式存在。
/**
* Method to discover classes of a given type in a given path.
*
* @param string $classPrefix The class name prefix to use for discovery.
* @param string $parentPath Full path to the parent folder for the classes to discover.
* @param boolean $force True to overwrite the autoload path value for the class if it already exists.
* @param boolean $recurse Recurse through all child directories as well as the parent path.
*
* @return void
*
* @since 11.1
*/
//在指定的路径寻找指定的类
public static function discover($classPrefix, $parentPath, $force = true, $recurse = false)
{
try
{
if ($recurse)
{
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($parentPath),
RecursiveIteratorIterator::SELF_FIRST
);
}
else
{
$iterator = new DirectoryIterator($parentPath);
}
foreach ($iterator as $file)
{
$fileName = $file->getFilename();//我需要确定这地得到是名字还是去路径
//这里输出的是文件名
// Only load for php files.
// Note: DirectoryIterator::getExtension only available PHP >= 5.3.6
if ($file->isFile() && substr($fileName, strrpos($fileName, '.') + 1) == 'php')
{
// Get the class name and full path for each file.
$class = strtolower($classPrefix . preg_replace('#\.php$#', '', $fileName));
// Register the class with the autoloader if not already registered or the force flag is set.
//这里已经确定了$classes中存储的是类名
if (empty(self::$classes[$class]) || $force)
{
self::register($class, $file->getPath() . '/' . $fileName);
}
}
}
}
catch (UnexpectedValueException $e)
{
// Exception will be thrown if the path is not a directory. Ignore it.
}
}
discover函数的实现也是比较简单。同样也是操作$classes这个数组。
另外,我们要谈一下另外一个成员变量$imported数组。我们通常见到 jimport('joomla.application.component.controllerform');这样的操作。那么看一下jimport是如何实现的。
**
* Loads a class from specified directories.
*
* @param string $key The class name to look for (dot notation).
* @param string $base Search this directory for the class.
*
* @return boolean True on success.
*
* @since 11.1
*/
//从指定的目录加载一个类
public static function import($key, $base = null)
{
// Only import the library if not already attempted.
if (!isset(self::$imported[$key]))
{
// Setup some variables.
$success = false;
$parts = explode('.', $key);
// array_pop 将数组的最后一个元素弹出来
// jimport('joomla.application.helper');
$class = array_pop($parts);
$base = (!empty($base)) ? $base : dirname(__FILE__);
//用目录分隔符代替.
$path = str_replace('.', DIRECTORY_SEPARATOR, $key);
// Handle special case for helper classes.
if ($class == 'helper')
{
$class = ucfirst(array_pop($parts)) . ucfirst($class);
}
// Standard class.
else
{
$class = ucfirst($class);
}
// If we are importing a library from the Joomla namespace set the class to autoload.
if (strpos($path, 'joomla') === 0)
{
// Since we are in the Joomla namespace prepend the classname with J.
$class = 'J' . $class;
// Only register the class for autoloading if the file exists.
if (is_file($base . '/' . $path . '.php'))
{
self::$classes[strtolower($class)] = $base . '/' . $path . '.php';
$success = true;
}
}
/*
* If we are not importing a library from the Joomla namespace directly include the
* file since we cannot assert the file/folder naming conventions.
*/
else
{
// If the file exists attempt to include it.
if (is_file($base . '/' . $path . '.php'))
{
$success = (bool) include_once $base . '/' . $path . '.php';
}
}
// Add the import key to the memory cache container.
self::$imported[$key] = $success;
}
return self::$imported[$key];
}
这样一样,我们就懂了$imported这个数组中装的是什么东西了。是joomla.application.component.controllerform作为key的数组,他的值是是对应的类的实现文件否被成功导入。并且在这里着重处理一下helper类。helper类的类名为$class = ucfirst(array_pop($parts)) . ucfirst($class);