function doSearch($search){
    $dir    = rtrim($search, '/').'/';
    $items  = glob($dir.'*');
    if(empty($items)){
        continue;
    }
    $path   = array();
    foreach ($items as $key => $item) {
        
        if(is_dir($item)){
            $path   = array_merge($path, doSearch($item));
        }
        
        $fp = file_get_contents($item);
        preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', $fp, $classes);
        preg_match_all('~namespace(.*?);~mi', $fp, $spaces);
        
        if(!isset($classes[1]) || empty($classes[1])){
            continue;
        }        foreach ($classes[1] as $k => $class) {
            $pre    = null;
            if(isset($spaces[1][$k])){
                $pre    = trim($spaces[1][$k]).'\\';
            }
            $path[md5($pre . $class)]    = $item;
        }
    }
    return $path;
}
返回结果:
Array
(
    [7c3a03bae648f5c37293db6ac6a1f900] => /home/www/default/classes/MysqlApi.class.php
    [e28b121f5bebc4e57f63cb35f19fb778] => /home/www/default/classes/MysqlApi.class.php
    [48941ce7b9e8d072889203cd0f0e7fd3] => /home/www/default/classes/final/PdoMysql.class.php
)

解决方案 »

  1.   

    返回结果:
    Array
    (
        [7c3a03bae648f5c37293db6ac6a1f900] => /home/www/default/classes/MysqlApi.class.php
        [e28b121f5bebc4e57f63cb35f19fb778] => /home/www/default/classes/MysqlApi.class.php
        [48941ce7b9e8d072889203cd0f0e7fd3] => /home/www/default/classes/final/PdoMysql.class.php
    )
      

  2.   

    例子:
    $dir        = __DIR__.'/classes/';FileSearch::addPath($dir);$cache  = new a\b\d\PdoApi();function __autoload($className){
        $classes   = FileSearch::get($className);
        if($classes){
            include_once $classes;
        }
    }class FileSearch {
        
        protected static $files     = array();
        
        protected static $path      = array();
        
        protected static $memcache  = array();
        
        protected static $memcached = array();    static public function get($className){
            $files      = self::load();
            $className  = md5($className);
            if(!array_key_exists($className, $files)){
                return false;
            }
            return $files[$className];
        }
        
        static function getFiles(){
            return self::$files;
        }    static public function setPath(array $path){
            self::$path = $path;
        }
        
        /**
         * 
         * @param array $path array('host'=>'',)
         */
        static public function addPath($path){
            self::$path[]   = $path;
        }
        
        static public function addMemcache(array $config){
            self::$memcache[]   = $config;
        }
        
        static public function addMemcached(array $config){
            self::$memcached[]  = $config;
        }    static protected function load(){
            
            self::doSearchByMemcache();
            
            self::doSearchByFile();
            
            if(empty(self::$files)){
                foreach (self::$path as $k => $path) {
                    self::$files    = array_merge(self::$files, self::doSearch($path));
                }
            }
            return self::$files;
        }
        
        static protected function doSearchByMemcache () {
            if(!empty(self::$files)){
                return true;
            }
        }
        
        static protected function doSearchByFile () {
            if(!empty(self::$files)){
                return true;
            }        
        }    static protected function doSearch($search){
            
            $dir    = rtrim($search, '/').'/';
            $items  = glob($dir.'*');
            if(empty($items)){
                continue;
            }
            $path   = array();
            foreach ($items as $key => $item) {            if(is_dir($item)){
                    $path   = array_merge($path, self::doSearch($item));
                }            $fp = file_get_contents($item);
                preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', $fp, $classes);
                preg_match_all('~namespace(.*?);~mi', $fp, $spaces);            if(!isset($classes[1]) || empty($classes[1])){
                    continue;
                }            foreach ($classes[1] as $k => $class) {
                    $pre    = null;
                    if(isset($spaces[1][$k])){
                        $pre    = trim($spaces[1][$k]).'\\';
                    }
                    $path[md5($pre . $class)]    = $item;
                }
            }
            return $path;
        }
    }
      

  3.   


    <?php$dir        = __DIR__.'/classes/';FileSearch::addPath($dir);FileSearch::addMemcached(array('host'=>'127.0.0.1','port'=>'11211'));new a\b\d\PdoApi();new a\b\c\MysqlApi();print_r(FileSearch::getFiles());function __autoload($className){
        $classes   = FileSearch::__autoload($className);
        if($classes){
            include_once $classes;
        }
    }class FileSearch {
        
        protected static $files     = array();
        
        protected static $path      = array();
        
        protected static $memcache  = array();
        
        protected static $memcached = array();
        
        protected static $cache     = array();
        
        static public function get($className){
            
            if(empty(self::$path)){
                throw new Exception('path need');
            }
            
            if(empty(self::$memcached) && empty(self::$memcache) && empty(self::$path)){
                throw new Exception('memcache or memcache or file cache save path need.');
            }
            
            $files      = self::init();
            
            $className  = md5($className);
            if(!array_key_exists($className, $files)){
                return false;
            }
            return $files[$className];
        }
        
        static public function __autoload($className){
            
            $file   = self::get($className);
            
            if($file){
                include $file;
            }
        }    static public function getFiles(){
            return self::$files;
        }    static public function getCache(){
            $cache  = self::getMemcached();
            if($cache){
                return $cache;
            }
            
            $cache  = self::getMemcache();
            if($cache){
                return $cache;
            }
            return false;
        }
        
        static public function getCacheKey(){
            return md5($_SERVER['HTTP_HOST'].'_'.$_SERVER['SERVER_ADDR']);
        }    static protected function getMemcache(){
            
            if(isset(self::$cache['memcache'])){
                return self::$cache['memcache'];
            }
            
            if(!empty(self::$memcache)){
                self::$cache['memcache']    = new Memcache();
                foreach (self::$memcache as $k => $v) {
                    self::getMemcache()->addServer($v['host'], $v['port']);
                }
                return self::$cache['memcache'];
            }
            
            return false;
        }
        
        static protected function getMemcached(){
            if(isset(self::$cache['memcached'])){
                return self::$cache['memcached'];
            }
            
            if(!empty(self::$memcached)){
                self::$cache['memcached']    = new Memcached();
                foreach (self::$memcached as $k => $v) {
                    self::getMemcached()->addServer($v['host'], $v['port']);
                }
                return self::$cache['memcached'];
            }
            
            return false;        
        }    static public function setPath(array $path){
            self::$path = $path;
        }
        
        /**
         * 
         * @param array $path array('host'=>'127.0.0.1','port'=>'11211')
         */
        static public function addPath($path){
            self::$path[]   = $path;
        }
        
        /**
         * 
         * @param array $path array('host'=>'127.0.0.1','port'=>'11211')
         */
        static public function addMemcache(array $config){
            self::$memcache[]   = $config;
        }
        
        static public function addMemcached(array $config){
            self::$memcached[]  = $config;
        }    static protected function init(){
            
            self::doSearchByMemcache();
            
            self::doSearchByFile();        return self::$files;
        }
        
        static protected function doSearchByMemcache () {
            
            if(!empty(self::$files)){
                return self::$files;
            }
            
            $cache  = self::getCache();
            $data   = $cache->get(self::getCacheKey());
            if($data){
                self::$files    = $data;
            }
            
            self::$files        = self::doSearchRun();
            if(self::$files){
                $cache->set(self::getCacheKey(), self::$files);
            }
            
            return self::$files;
        }
        
        static protected function doSearchByFile () {
            
            if(!empty(self::$files)){
                return self::$files;
            }
            
            self::$files        = self::doSearchRun();
            
        }
        
        static protected function doSearchRun(){
            if(empty(self::$files)){
                foreach (self::$path as $k => $path) {
                    self::$files    = array_merge(self::$files, self::doSearch($path));
                }
            }
            return self::$files;
        }    static protected function doSearch($search){
            
            $dir    = rtrim($search, '/').'/';
            $items  = glob($dir.'*');
            if(empty($items)){
                continue;
            }
            $path   = array();
            foreach ($items as $key => $item) {            if(is_dir($item)){
                    $path   = array_merge($path, self::doSearch($item));
                }            $fp = file_get_contents($item);
                preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', $fp, $classes);
                preg_match_all('~namespace(.*?);~mi', $fp, $spaces);            if(!isset($classes[1]) || empty($classes[1])){
                    continue;
                }            foreach ($classes[1] as $k => $class) {
                    $pre    = null;
                    if(isset($spaces[1][$k])){
                        $pre    = trim($spaces[1][$k]).'\\';
                    }
                    $path[md5($pre . $class)]    = $item;
                }
            }
            return $path;
        }
    }?>
      

  4.   

    最终版本,兼容memcache、memcached、file cache<?php$dir        = __DIR__.'/classes/';FileSearch::addPath($dir);FileSearch::setCacheDir(__DIR__.'/cache/');//FileSearch::addMemcached(array('host'=>'127.0.0.1','port'=>'11211'));new a\b\d\PdoApi();new a\b\c\MysqlApi();print_r(FileSearch::getFiles());
    function __autoload($className){
        $classes   = FileSearch::__autoload($className);
        if($classes){
            include_once $classes;
        }
    }class FileSearch {
        
        protected static $files     = array();
        
        protected static $path      = array();
        
        protected static $memcache  = array();
        
        protected static $memcached = array();
        
        protected static $cache     = array();
        
        protected static $cacheDir  = null;
        
        protected static $cacheFile = '~run.php';    static public function get($className){
            
            if(empty(self::$path)){
                throw new Exception('path need');
            }
            
            if(empty(self::$memcached) && empty(self::$memcache) && empty(self::$cacheDir)){
                throw new Exception('memcache or memcache or file cache save path need.');
            }
            
            if(empty(self::$path)){
                throw new Exception('file search path not set.');
            }
            
            $files      = self::init();
            
            $className  = md5($className);
            if(!array_key_exists($className, $files)){
                return false;
            }
            return $files[$className];
        }
        
        static public function __autoload($className){
            
            $file   = self::get($className);
            
            if($file){
                include $file;
            }
        }    static public function getFiles(){
            return self::$files;
        }    static public function getCache(){
            $cache  = self::getMemcached();
            if($cache){
                return $cache;
            }
            
            $cache  = self::getMemcache();
            if($cache){
                return $cache;
            }
            return false;
        }
        
        static public function getCacheKey(){
            return md5($_SERVER['HTTP_HOST'].'_'.$_SERVER['SERVER_ADDR']);
        }    static protected function getMemcache(){
            
            if(isset(self::$cache['memcache'])){
                return self::$cache['memcache'];
            }
            
            if(!empty(self::$memcache)){
                self::$cache['memcache']    = new Memcache();
                foreach (self::$memcache as $k => $v) {
                    self::getMemcache()->addServer($v['host'], $v['port']);
                }
                return self::$cache['memcache'];
            }
            
            return false;
        }
        
        static protected function getMemcached(){
            if(isset(self::$cache['memcached'])){
                return self::$cache['memcached'];
            }
            
            if(!empty(self::$memcached)){
                self::$cache['memcached']    = new Memcached();
                foreach (self::$memcached as $k => $v) {
                    self::getMemcached()->addServer($v['host'], $v['port']);
                }
                return self::$cache['memcached'];
            }
            
            return false;        
        }    static public function setPath(array $path){
            self::$path = $path;
        }
        
        static public function setCacheDir($dir){
            self::$cacheDir = $dir;
            if(!is_dir(self::$cacheDir)){
                mkdir(self::$cacheDir, 777, true);
            }
        }
        
        static public function setCacheFile($file){
            self::$cacheFile    = $file;
        }    /**
         * 
         * @param array $path array('host'=>'127.0.0.1','port'=>'11211')
         */
        static public function addPath($path){
            self::$path[]   = $path;
        }
        
        /**
         * 
         * @param array $path array('host'=>'127.0.0.1','port'=>'11211')
         */
        static public function addMemcache(array $config){
            self::$memcache[]   = $config;
        }
        
        static public function addMemcached(array $config){
            self::$memcached[]  = $config;
        }    static protected function init(){
            
            self::doSearchByMemcache();        self::doSearchByFile();        return self::$files;
        }
        
        static protected function doSearchByMemcache () {
            
            if(!empty(self::$files)){
                return self::$files;
            }
            
            $cache  = self::getCache();
            if(!$cache){
                return false;
            }
            
            $data   = $cache->get(self::getCacheKey());
            if($data){
                self::$files    = $data;
            }
            
            self::$files        = self::doSearchRun();
            if(self::$files){
                $cache->set(self::getCacheKey(), self::$files);
            }
            
            return self::$files;
        }
        
        static protected function doSearchByFile () {
            
            if(!empty(self::$files)){
                return self::$files;
            }
            
            
            $save   = rtrim(self::$cacheDir, '/') .'/'. self::$cacheFile;
            if(!file_exists($save)){
                self::$files        = self::doSearchRun();
                $file   = "<?php\r\nreturn\t".var_export(self::$files, true).";\r\n?>";
                file_put_contents($save, $file);
            }
            
            self::$files    = include $save;
        }
        
        static protected function doSearchRun(){
            if(empty(self::$files)){
                foreach (self::$path as $k => $path) {
                    self::$files    = array_merge(self::$files, self::doSearch($path));
                }
            }
            return self::$files;
        }    static protected function doSearch($search){
            
            $dir    = rtrim($search, '/').'/';
            $items  = glob($dir.'*');
            if(empty($items)){
                continue;
            }
            $path   = array();
            foreach ($items as $key => $item) {            if(is_dir($item)){
                    $path   = array_merge($path, self::doSearch($item));
                }            $fp = file_get_contents($item);
                preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', $fp, $classes);
                preg_match_all('~namespace(.*?);~mi', $fp, $spaces);            if(!isset($classes[1]) || empty($classes[1])){
                    continue;
                }            foreach ($classes[1] as $k => $class) {
                    $pre    = null;
                    if(isset($spaces[1][$k])){
                        $pre    = trim($spaces[1][$k]).'\\';
                    }
                    $path[md5($pre . $class)]    = $item;
                }
            }
            return $path;
        }
    }?>