- <?php
- /**
- * @version $Id: loader.php 10381 2008-06-01 03:35:53Z pasamio $
- * @package Joomla.Framework
- * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
- * @license GNU/GPL, see LICENSE.php
- * Joomla! is free software. This version may have been modified pursuant
- * to the GNU General Public License, and as distributed it includes or
- * is derivative of works licensed under the GNU General Public License or
- * other free or open source software licenses.
- * See COPYRIGHT.php for copyright notices and details.
- */
- //定義分隔符
- if(!defined('DS')) {
- define( 'DS', DIRECTORY_SEPARATOR );
- }
- /**
- * @package Joomla.Framework
- */
- class JLoader
- {
- /**
- * Loads a class from specified directories.
- *
- * @param string $name The class name to look for ( dot notation ).
- * @param string $base Search this directory for the class.
- * @param string $key String used as a prefix to denote the full path of the file ( dot notation ).
- * @return void
- * @since 1.5
- */
- function import( $filePath, $base = null, $key = 'libraries.' )
- {
- static $paths;//保存 $paths[***.***.**.**] = bool 或者 include...
- if (!isset($paths)) { //頭一回
- $paths = array();//設置爲空數組
- }
- //在原路徑中加上前綴,$keypath是作爲$paths的鍵名,靜態保存
- $keyPath = $key ? $key . $filePath : $filePath;
- //如果沒設置這個鍵名
- if (!isset($paths[$keyPath]))
- {
- if ( ! $base ) {//基本路徑爲空
- $base = dirname( __FILE__ );//其實就是JPATH_LIBRARIES
- }
- //把原路徑依.分開
- $parts = explode( '.', $filePath );
- //取出最後一個,就是類名
- $classname = array_pop( $parts );
- switch($classname)//判斷下類名
- {
- case 'helper' ://如果是'helper'
- //再取一個雙大寫組合
- $classname = ucfirst(array_pop( $parts )).ucfirst($classname);
- break;
- default ://默認的話就類名開頭大寫
- $classname = ucfirst($classname);
- break;
- }
- //路徑更改, .變DS
- $path = str_replace( '.', DS, $filePath );
- //joomla開頭
- if (strpos($filePath, 'joomla') === 0)
- {
- /*
- * If we are loading a joomla class prepend the classname with a
- * capital J.
- */
- $classname = 'J'.$classname;//J+類名
- //註冊一下
- $classes = JLoader::register($classname, $base.DS.$path.'.php');
- //是否設置了
- $rs = isset($classes[strtolower($classname)]);
- }
- else
- {
- /*
- * If it is not in the joomla namespace then we have no idea if
- * it uses our pattern for class names/files so just include.
- */
- //直接導入
- $rs = include($base.DS.$path.'.php');
- }
- $paths[$keyPath] = $rs;
- }
- //返回的可以是bool或是導入的內容
- return $paths[$keyPath];
- }
- /**
- * Add a class to autoload
- *
- * @param string $classname The class name
- * @param string $file Full path to the file that holds the class
- * @return array|boolean Array of classes
- * @since 1.5
- */
- /**
- * 配合php5的自動讀取
- *
- * @param string $classname 類名
- * @param string $file 絕對路徑
- * @return array|boolean Array of classes
- * @since 1.5
- */
- function & register ($class = null, $file = null)
- {
- static $classes;//classes[(小寫)類名] = 路徑
- if(!isset($classes)) {//如果頭一回
- $classes = array();//爲空數組
- }
- if($class && is_file($file))//if都沒問題
- {
- // Force to lower case.
- $class = strtolower($class);//強制小寫
- $classes[$class] = $file; //classes[(小寫)類名] = 路徑
- // In php4 we load the class immediately.
- if((version_compare( phpversion(), '5.0' ) < 0)) {//PHP就直接讀取class
- JLoader::load($class);
- }
- }
- //return classes
- return $classes;
- }
- /**
- * Load the file for a class
- *
- * @access public
- * @param string $class The class that will be loaded
- * @return boolean True on success
- * @since 1.5
- */
- /**
- * 從註冊函數中返回classes,$class是classes的鍵值就包括進來
- * @access public
- * @param string $class
- * @return boolean True on success
- */
- function load( $class )
- {
- $class = strtolower($class); //強制小寫
- if (class_exists($class)) {//最在返回空
- return;
- }
- $classes = JLoader::register();//取得所有註冊的類
- if(array_key_exists( strtolower($class), $classes)) {
- //如果存在,包括進來
- include($classes[$class]);
- return true;
- }
- //否則返回 false
- return false;
- }
- }
- /**
- * When calling a class that hasn't been defined, __autoload will attempt to
- * include the correct file for that class.
- *
- * This function get's called by PHP. Never call this function yourself.
- *
- * @param string $class
- * @access public
- * @return boolean
- * @since 1.5
- */
- //PHP5的新特性,包括已經註冊的類
- function __autoload($class)
- {
- if(JLoader::load($class)) {
- return true;
- }
- return false;
- }
- /**
- * Global application exit.全局的退出函數
- *
- * This function provides a single exit point for the framework.
- *
- * @param mixed Exit code or string. Defaults to zero.
- */
- function jexit($message = 0) {
- exit($message);
- }
- /**
- * Intelligent file importer一個系統內部的導入,因爲是用libraries.開始
- *
- * @access public
- * @param string $path A dot syntax path
- * @since 1.5
- */
- function jimport( $path ) {
- return JLoader::import($path);
- }
joomla1.56 loader.php
分享一下,有錯誤請寫出來,謝謝啦
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.