rare has a built-in class automatic loading function. When using a class, it can be used directly without requiring (include) the class file. This type of automatic loading function is very independent. If you need it, you can use it directly in other frameworks (any PHP program). 1. First introduce rareAutoLoad.class.php 2.Registration function
- /**
- * Class autoloading
- * http://raremvc.googlecode.com
- * http://rare.hongtao3.com
- * @example
- * include 'rareAutoLoad.php';
- * $option=array('dirs' =>'/www/lib/share/,/www/lib/api/',//class Search from those directories
- * 'cache'=>'/tmp/111111.php',//class path Cache file
- * 'suffix'=>'.class.php' //The suffix of the PHP class file that requires automatic class loading
- * "hand"=>true, //Whether to manually update the class path file, when it is false The cache file is written to the specified cache file,
- * //If true, you need to manually allow the autoLoad.php file
- * );
- * rareAutoLoad::register($option);
- *
- * Refers to the symfony class Automatic loading
- * In order to provide efficiency, the location of the class is saved in the cache file, and the file directory in dirs will be scanned when used for the first time
- * The file naming requirement for classes that need to be automatically loaded must be .class. php ends. For example, the class defined in the file name a.class.php can be scanned and the file a.php will be ignored
- * There is no relationship between the class name and the file naming. For example, class b can be defined in the a.class.php file. {}
- *
- * @author duwei
- *
- */
- class rareAutoLoad
- {
- private static $instance=null;
- private static $registered=false;
-
- private $cacheFile=null;
- private $classes =array();//Corresponding class class name and corresponding file path
- private $option;
-
- private $hand=false;//Whether to manually run the script to scan the class path,
-
- private $reloadCount=0;//reload Number of operations
- /**
- * @param array $option requires parameters dirs: scan directory cache: cache files
- */
- public function __construct($option){
-
- if(!isset($option['suffix'])) $option['suffix']=".class.php ";//File suffix
- $this->option=$option;
- if(!isset($option['cache'])){
- $trac=debug_backtrace(false);
- $calFile=$trac[2 ]['file'];
- $option['cache']="/tmp/rareautoLoad_".md5($calFile)."_".filemtime($calFile);
- @unlink($option['cache'] );
- }
- if(isset($option['hand']))$this->hand=(boolean)$option['hand'];
- $this->cacheFile=$option['cache' ].".php";
- $this->getClasses();
- }
-
- /**
- * Get the single instance object of DAutoLoad
- * @param array $option
- * @return DAutoLoad
- */
- private static function getInstance($option){
- if (!isset(self::$instance )){
- self::$instance = new rareAutoLoad($option);
- }
- return self::$instance;
- }
-
- /**
- * Register for automatic loading
- * @param array $option array('dirs'=>'/www/lib/share/,/www/lib/api/','cache'=>'/tmp/111111. php');
- * @throws Exception
- */
- public static function register($option) {
- if (self::$registered)return;
- // ini_set('unserialize_callback_func', 'spl_autoload_call');
- if (false === spl_autoload_register(array(self::getInstance($option), 'autoload'))) {
- die(sprintf('Unable to register %s::autoload as an autoloading method.', get_class(self::getInstance())));
- }
- self::$registered = true;
- }
-
- / **
- * spl_autoload_call calls load class
- * If the path of the class in the cache file is incorrect, it will try to reload once
- * Record the key of the class that does not exist after reloading, and mark it as false to avoid multiple invalidation of the cache file. Update
- * When using class_exists for judgment, the autoload operation will be performed by default
- * @param $class
- * @return
- */
- public function autoload($class){
- if(class_exists($class, false) || interface_exists($class, false)) return true;
- if ($this->classes && isset ($this->classes[$class]) ){
- $file=$this->classes[$class];
- if(!$file)return false;
- if(!file_exists($file) && ! $this->hand){
- $this->reload();
- return $this->autoload($class);
- }
- require($file);
- return true;
- }{
- $this ->reload();
- if(isset($this->classes[$class])){
- $file=$this->classes[$class];
- if(!$file)return false;
- require($file);
- return true;
- }else{
- $this->classes[$class]=false;
- $this->saveCache();
- }
- }
- return false;
- }
- /**
- * Get the list of class names
- * @return
- */
- private function getClasses(){
- if(file_exists($this->cacheFile)){
- $this->classes=require($this->cacheFile);
- if(is_array($this->classes))return true;
- }
- $this->classes=array();
- $this->reload();
- }
-
- /**
- * Rescan again
- * and save the location information of the class name to cache
- * @return
- */
- private function reload(){
- $this->reloadCount++;
- if($this->hand)return;
- $cachedir=dirname($this->cacheFile);
- $this->directory($cachedir);
- if(!is_writable($cachedir)) die('can not write cache!');
-
- settype($this->classes, 'array');
-
- $dirs=$this->option['dirs'];
- if(!is_array($dirs)) $dirs=explode(",", $dirs);
-
- $dirs=array_unique($dirs);
- foreach($dirs as $dir){
- if(!$dir || !file_exists($dir))continue;
- $this->scanDir($dir);
- }
- $this->saveCache();
- }
-
- private function saveCache(){
- if($this->hand)return;
- $phpData=" if(!is_array($this->classes))$this->classes=array();
- ksort($this->classes);
- $phpData.="return ".var_export($this->classes,true).";";
- file_put_contents($this->cacheFile, $phpData,LOCK_EX);
- clearstatcache();
- }
-
- /**
- * Scan folders and files
- * Only files named by $this->option['suffix'] will be scanned
- * @param $dir
- * @return
- */
- private function scanDir($dir){
- $files=scandir($dir,1);
- foreach($files as $fileName){
- $file=rtrim($dir,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$fileName;
- if(is_dir($file) && strpos($fileName,'.')!==0){
- $this->scanDir($file);
- }else{
- if($this->str_endWith($fileName,$this->option['suffix'])){
- preg_match_all('~^s*(?:abstracts+|finals+)?(?:class|interface)s+(w+)~mi', file_get_contents($file), $classes);
- foreach ($classes[1] as $class){
- $this->classes[$class] = $file;
- }
- }
- }
- }
- }
-
- private function directory($dir){
- return is_dir($dir) or ($this->directory(dirname($dir)) and mkdir($dir, 0777));
- }
-
- function str_endWith($str,$subStr){
- return substr($str, -(strlen($subStr)))==$subStr;
- }
- }
-
复制代码
|