file_map)){ $this->file_map = new FileMap(); } if(!is_object($this->file_store)){ $this->file_store = new FileStore(); } spl_autoload_register(array($this, "loadFile")); } public function autoload(){ $autoload_path = $this->cleanPath($this->autoload_path); foreach($autoload_path as $index=>$path) { if (!is_readable($path)) { unset($autoload_path[$index]); } } //保存到本地 if($this->open_local_storage){ if( !$this->file_map->hasMap()){ $this->scanDirs($autoload_path); $store = json_encode($this->file_store->getAll()); //保存为json $this->file_map->saveMap($store); }else if($obdt = $this->file_map->loadMap()){ $file_map = json_decode($obdt); foreach($file_map as $crypt => $items){ foreach($items->name as $name){ $this->addFileStore($items->dir."/".$name.".php"); } } }else{ trigger_error("Error:map.json file has no content ",E_USER_ERROR); } }else{ $this->scanDirs($autoload_path); } } protected function scanDirs($dirs){ $i = 0; while(isset($dirs[$i])){ $dir = $dirs[$i]; $files = scandir($dir); foreach ($files as $file) { if (in_array($file, array(".", ".."))) { continue; } $currentFile = $dir . "/" . $file; if (is_php($file) && is_file($currentFile)) { $this->addFileStore($currentFile); } else if (is_dir($currentFile)) { $dirs[] = $currentFile; } } $i++; } } protected function getClassName($path){ if(preg_match("/\\/([^\\/]+?)\\.(?:php)$/",$path, $matches)){ return $matches[1]; } return false; } protected function cleanPath($path){ $res = array(); if(!is_array($path)){ $path = array($path); } foreach($path as $no => $file_path){ $file_path = str_replace("\\","/",trim($file_path)); $res[] = $file_path; } return $res; } protected function addFileStore($file){ $className = $this->getClassName($file); if(!$this->file_store->get($className)){ $this->file_store->add($className,$file); }else{ $this->file_store->update($className,$file); } } protected function loadFile($class){ if($file_path = $this->file_store->get($class)){ include($file_path); } } }