note 91312 deleted from language.oop5.autoload by danbrown

From: Date: Fri, 05 Jun 2009 13:51:47 +0000
Subject: note 91312 deleted from language.oop5.autoload by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156387@lists.php.net to get a copy of this message
Note Submitter: jandosul at gmail dot com ---- >=5.3.0 Autoloader from my project im storing all classess in *.phar files /** * * This class used to filter all phar files in directory */ class AllPharFilesFilter extends FilterIterator{ public function accept(){ if(!$this->isDot()){ if(preg_match('/\.phar$/', $this->current())){ return true; }// end if }// end if return false; } } function __autoload($classname){ // namespaced classes in win is double slashed $classname = explode('\\', $classname); // getting last element $classname = $classname[(count($classname)-1)]; $cDir = ''; $projectDirectory = getenv("DOCUMENT_ROOT")."/yourdir"; $it = new AllPharFilesFilter(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($projectDirectory))); // needed file $index = $classname.'.php'; $it->rewind(); while($it->valid()){ $subPathName = explode('\\', $it->getSubPathname()); $subPathName = $subPathName[(count($subPathName)-1)]; $p = new Phar($it->getRealPath(), 0, $subPathName); // include if found if(isSet($p[$index])){ require_once('phar://'.$subPathName.'/'.$index); }// end if $it->next(); }// end while }// Class loader and utility makephars.php file which creates *.phar files in urdir ini_set("phar.readonly", 0); // all *.php$ files class AllPhpFilesFilter extends FilterIterator{ public function accept(){ if(!$this->isDot()){ if(preg_match('/\.php$/', $this->current())){ return true; }// end if }// end if return false; } } // all *.tar.gz$ files class AllTarGzPharFilesFilter extends FilterIterator{ public function accept(){ if(!$this->isDot()){ if(preg_match('/\.tar.gz$/', $this->current()) || preg_match('/\.phar$/', $this->current())){ return true; }// end if }// end if return false; } } $projectDirectory = getenv("DOCUMENT_ROOT")."/symp"; $it = new AllTarGzPharFilesFilter(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($projectDirectory))); $it->rewind(); while($it->valid()){ try{ @unlink($it->current()); }catch(Exception $ex){ echo $ex->getMessage().'<br/>'; }// end try $it->next(); }// end while $it = new AllPhpFilesFilter(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($projectDirectory))); $its = new RecursiveDirectoryIterator($projectDirectory); $cDir = ''; $it->rewind(); while($it->valid()){ try{ $tmp = ''; while($tmp==''){ $tmp = explode('\\', $it->getSubPath()); $subPath = join('/', $tmp); $tmp = $tmp[(count($tmp)-1)]; if($tmp=='') $it->next(); }// end while if($cDir!=$tmp && $p instanceof Phar){ if(!file_exists($newpharfile.'.tar.gz')) $p->convertToExecutable(Phar::TAR, Phar::GZ); }// end if if($cDir!=$tmp){ $cDir = $tmp; $newpharfile = $projectDirectory.'/'.$it->getSubPath().'/'.$cDir.'.phar'; $newpharfile = str_replace('\\', '/', $newpharfile); if($p = new Phar($newpharfile, 0, $cDir.'.phar')) echo 'File "'.$newpharfile.'" created <br/>'; } if($tmp!=''){ echo '----->'.$it->getFileName().'<br/>'; $p[$it->getFileName()] = file_get_contents($it->getRealPath()); }// end if }catch(Exception $ex){ Page::addError($ex); echo $ex->getMessage().'<br/>'; }// end try $it->next(); }// end while

« previous php.notes (#156387) next »