The rare has built-in class auto-mount functionality that is used directly without a require (include) class file when using a class.
This kind of automatic loading function is very independent, if you need, you can directly in other frameworks (any PHP program) to use.
1. First introduce rareAutoLoad.class.php
2. Registration function
- /**
- * Class Automatic loading
- * http://raremvc.googlecode.com
- * http://rare.hongtao3.com
- * @example
- * include ' rareautoload.php ';
- * $option =array (' dirs ' = '/www/lib/share/,/www/lib/api/',//class from those directories
- * ' cache ' = '/tmp/111111.php ',//class path cache file
- * ' suffix ' + '. class.php '//suffix for PHP class files that require class auto-loading
- * "Hand" =>true,//whether to manually update the class path file, False when the cache file is written to the specified cache file,
- *//to TRUE is required to manually allow the autoload.php file
- * );
- * Rareautoload::register ($option);
- *
- * Reference to Automatic loading of symfony classes
- * To provide efficiency, the location of the class is saved to the cache file, and the file directory in the dirs is scanned at the first use
- * File naming requirements for classes that require automatic loading must end with. class.php, such as classes defined in file name a.class.php can be scanned and a.php files are ignored
- * Class name and file name can have no relationship, such as the a.class.php file can be defined class b{}
- *
- * @author Duwei
- *
- */
- Class Rareautoload
- {
- private static $instance =null;
- private static $registered =false;
- Private $cacheFile =null;
- Private $classes =array ();//corresponds to class name and corresponding file path
- Private $option;
- Private $hand =false;//Whether to run the script manually for class path scanning,
- Private $reloadCount number of =0;//reload operations
- /**
- * @param array $option requires parameter dirs: Scan directory cache: Cached file
- */
- 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 Dautoload Single-instance objects
- * @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 (The sprintf (' Unable to register%s::autoload as a autoloading method. ', Get_class (Self::getinstance ())));
- }
- Self:: $registered = true;
- }
- /**
- * Spl_autoload_call Call Load Class
- * If the path to the class in the cache file is incorrect, try Reload once
- * Record its key in the class cache that does not exist after reload, mark it as false to avoid the cache file multiple invalid updates
- * AutoLoad action is performed by default when using Class_exists
- * @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 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 ();
- }
- /**
- * Re-scan once
- * and save the location information of the class name in the 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 $this->option[' suffix ') will be scanned to
- * @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*: abstract\s+|final\s+)? (?: 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;
- }
- }
Copy Code |