joomla1.56 loader.php

来源:互联网 发布:软件后端开发是什么 编辑:程序博客网 时间:2024/05/01 14:38
分享一下,有错误请写出来,谢谢啦
  1. <?php
  2. /**
  3. * @version $Id: loader.php 10381 2008-06-01 03:35:53Z pasamio $
  4. * @package      Joomla.Framework
  5. * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  6. * @license      GNU/GPL, see LICENSE.php
  7. * Joomla! is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13. //定义分隔符
  14. if(!defined('DS')) {
  15.     define( 'DS', DIRECTORY_SEPARATOR );
  16. }
  17. /**
  18.  * @package     Joomla.Framework
  19.  */
  20. class JLoader
  21. {
  22.      /**
  23.      * Loads a class from specified directories.
  24.      *
  25.      * @param string $name  The class name to look for ( dot notation ).
  26.      * @param string $base  Search this directory for the class.
  27.      * @param string $key   String used as a prefix to denote the full path of the file ( dot notation ).
  28.      * @return void
  29.      * @since 1.5
  30.      */
  31.     function import( $filePath$base = null, $key = 'libraries.' )
  32.     {
  33.         static $paths;//保存 $paths[***.***.**.**] = bool 或者 include...
  34.         if (!isset($paths)) { //头一回
  35.             $paths = array();//设置为空数组
  36.         }
  37.         //在原路径中加上前缀,$keypath是作为$paths的键名,静态保存   
  38.         $keyPath = $key ? $key . $filePath : $filePath;
  39.         //如果没设置这个键名
  40.         if (!isset($paths[$keyPath]))
  41.         {
  42.             if ( ! $base ) {//基本路径为空
  43.                 $base =  dirname( __FILE__ );//其实就是JPATH_LIBRARIES
  44.             }
  45.             //把原路径依.分开
  46.             $parts = explode'.'$filePath );
  47.             //取出最后一个,就是类名
  48.             $classname = array_pop$parts );
  49.             switch($classname)//判断下类名
  50.             {
  51.                 case 'helper' ://如果是'helper'
  52.                     //再取一个双大写组合
  53.                     $classname = ucfirst(array_pop$parts )).ucfirst($classname);
  54.                     break;
  55.                 default ://默认的话就类名开头大写
  56.                     $classname = ucfirst($classname);
  57.                     break;
  58.             }
  59.             //路径更改, .变DS
  60.             $path  = str_replace'.', DS, $filePath );
  61.             //joomla开头
  62.             if (strpos($filePath'joomla') === 0)
  63.             {
  64.                 /*
  65.                  * If we are loading a joomla class prepend the classname with a
  66.                  * capital J.
  67.                  */
  68.                 $classname  = 'J'.$classname;//J+类名
  69.                 //注册一下
  70.                 $classes    = JLoader::register($classname$base.DS.$path.'.php');
  71.                 //是否设置了
  72.                 $rs         = isset($classes[strtolower($classname)]);
  73.             }
  74.             else
  75.             {
  76.                 /*
  77.                  * If it is not in the joomla namespace then we have no idea if
  78.                  * it uses our pattern for class names/files so just include.
  79.                  */
  80.                 //直接导入
  81.                 $rs   = include($base.DS.$path.'.php');
  82.             }
  83.             $paths[$keyPath] = $rs;
  84.         }
  85.         //返回的可以是bool或是导入的内容
  86.         return $paths[$keyPath];
  87.     }
  88.     /**
  89.      * Add a class to autoload
  90.      *
  91.      * @param   string $classname   The class name
  92.      * @param   string $file        Full path to the file that holds the class
  93.      * @return  array|boolean       Array of classes
  94.      * @since   1.5
  95.      */
  96.     /**
  97.      * 配合php5的自动读取
  98.      *
  99.      * @param   string $classname   类名
  100.      * @param   string $file        绝对路径
  101.      * @return  array|boolean       Array of classes
  102.      * @since   1.5
  103.      */
  104.     function & register ($class = null, $file = null)
  105.     {
  106.         static $classes;//classes[(小写)类名] = 路径
  107.         if(!isset($classes)) {//如果头一回
  108.             $classes    = array();//为空数组
  109.         }
  110.         if($class && is_file($file))//if都没问题
  111.         {
  112.             // Force to lower case.
  113.             $class = strtolower($class);//强制小写
  114.             $classes[$class] = $file//classes[(小写)类名] = 路径
  115.             // In php4 we load the class immediately.
  116.             if((version_compare( phpversion(), '5.0' ) < 0)) {//PHP就直接读取class
  117.                 JLoader::load($class);
  118.             }
  119.         }
  120.         //return classes
  121.         return $classes;
  122.     }
  123.     /**
  124.      * Load the file for a class
  125.      *
  126.      * @access  public
  127.      * @param   string  $class  The class that will be loaded
  128.      * @return  boolean True on success
  129.      * @since   1.5
  130.      */
  131.     /**
  132.      * 从注册函数中返回classes,$class是classes的键值就包括进来
  133.      * @access public
  134.      * @param  string $class
  135.      * @return boolean True on success
  136.      */
  137.     function load( $class )
  138.     {
  139.         $class = strtolower($class); //强制小写
  140.         if (class_exists($class)) {//最在返回空
  141.               return;
  142.         }
  143.         $classes = JLoader::register();//取得所有注册的类
  144.         if(array_key_existsstrtolower($class), $classes)) {
  145.             //如果存在,包括进来
  146.             include($classes[$class]);
  147.             return true;
  148.         }
  149.         //否则返回 false
  150.         return false;
  151.     }
  152. }
  153. /**
  154.  * When calling a class that hasn't been defined, __autoload will attempt to
  155.  * include the correct file for that class.
  156.  *
  157.  * This function get's called by PHP. Never call this function yourself.
  158.  *
  159.  * @param   string  $class
  160.  * @access  public
  161.  * @return  boolean
  162.  * @since   1.5
  163.  */
  164. //PHP5的新特性,包括已经注册的类
  165. function __autoload($class)
  166. {
  167.     if(JLoader::load($class)) {
  168.         return true;
  169.     }
  170.     return false;
  171. }
  172. /**
  173.  * Global application exit.全局的退出函数
  174.  *
  175.  * This function provides a single exit point for the framework.
  176.  *
  177.  * @param mixed Exit code or string. Defaults to zero.
  178.  */
  179. function jexit($message = 0) {
  180.     exit($message);
  181. }
  182. /**
  183.  * Intelligent file importer一个系统内部的导入,因为是用libraries.开始
  184.  * 
  185.  * @access public
  186.  * @param string $path A dot syntax path
  187.  * @since 1.5
  188.  */
  189. function jimport( $path ) {
  190.     return JLoader::import($path);
  191. }

原创粉丝点击