Location: PHPKode > scripts > PHPIncluder > phpincluder/includer.class.php
<?php
/**
 * @author Gombos Lorand
 * @name PHP Includer
 * @version 1.1
 * @copyright OpenSorce
 */

/**
 * DirIterator, extends the DirectoryIterator class. 
 * Its purpose is to introduce a getExtension method. 
 */
class DirIterator extends DirectoryIterator 
{
    public function getExtension()
    {
        $Filename = $this->getFilename();
        $FileExtension = strrpos($Filename, ".", 1) + 1;
        if ($FileExtension != false)
            return strtolower(substr($Filename, $FileExtension, strlen($Filename) - $FileExtension));
        else
            return "";
    }
}

/**
 * This class is capable to cycle through given catalogue, 
 * to map the inside files, including inner folders using recursivity.
 */
class Includer{
	/**
	 * Define constants for each type of import
	 */
	const I = "include";
	const I_ONCE = "include_once";
	const R = "require";
	const R_ONCE = "require_once"; 
	
	private $subdirList = array();
	private $classList = array();
	private $incfileList = array();
	private $phpfileList = array();
	private $blacklist = array();
	private $whitelist = array();
	private $type = self::I_ONCE;
	private $inSubdir = TRUE;
	private $sortByDepth = FALSE;

	public function __construct(){
    }
    
    /**
     * Set the $inSubdir value.
     *
     * @param boolean $value
     */
    public function setInSubdir($value){
    	$this->inSubdir = $value;
    }
    /**
     * Set the soertByDepth parameter.
     * This parameter, allow to script to sort the arrays by depth.
     *
     * @param boolean $value
     */
    public function setSortByDepth($value){
    	$this->sortByDepth = $value;
    }
    /**
     * This function is capable to cycle through given catalogue, 
     * to map the inside files, including inner folders using recursivity.
     * 
     * @param string $path
     * @param int $depth
     */
    public function Import($path, $depth=0){
      $dir  = new DirIterator($path);
      	foreach ($dir as $file){
      		if ($file->isDot()){
      			continue;
      		}
      		elseif ($file->isDir()){
      			array_push($this->subdirList,array("path"=>$path."/".$file->getFilename(),"depth"=>$depth));
      			$this->inSubdir?$this->Import($path."/".$file->getFilename(), ++$depth):"";
      		}
      		elseif ($file->getExtension() == "php") {
      			if (substr($file->getFilename(),-9)=="class.php"){
      				$this->putInArray($this->classList, $file->getFilename(), $path, $depth);
      			}
      			elseif (substr($file->getFilename(),-7)=="inc.php"){
      				$this->putInArray($this->incfileList, $file->getFilename(), $path, $depth);
      			}
      			else {
      				$this->putInArray($this->phpfileList, $file->getFilename(), $path, $depth);
      			}
      		}
      	}
      	$this->sortByDepth?$this->sortByDepth():"";
    }
    /**
     * This function verify if the file is in whitelist, or in blacklist
     * and put the files in corresponding array.
     *
     * @param strign $destionationArray
     * @param string $filename
     * @param string $path
     * @param integer $depth
     */
    private function putInArray(&$destionationArray, $filename, $path, $depth){
    	if (count($this->whitelist)){
    		if (in_array($filename, $this->whitelist)){
    			array_push($destionationArray, array("path"=>$path."/".$filename, "depth"=>$depth));
    		}
    	}
    	elseif (count($this->blacklist)){
    	    if (!in_array($filename, $this->blacklist)){
    			array_push($destionationArray, array("path"=>$path."/".$filename, "depth"=>$depth));	
    		}    		
    	}
    	else {
    		array_push($destionationArray, array("path"=>$path."/".$filename, "depth"=>$depth));
    	}
    }
    /**
     * This function sort the classList array and the phpfileList array
     * ascendic by depth
     */
    private function sortByDepth(){
    	$depth = array();
    	foreach ($this->classList as $key => $row){
    		$depth[$key] = $row['depth'];
    	}
		array_multisort($depth, SORT_ASC, $this->classList);
		$depth = array();  
		foreach ($this->phpfileList as $key => $row){
			$depth[$key] = $row['depth'];  	
		}
		array_multisort($depth, SORT_ASC, $this->phpfileList);
    }
	/**
	 * Set the type (methode), for import the files.
	 *
	 * @param string $type
	 */
    public function setType($type){
    	$allowedValues = array(Includer::I, Includer::I_ONCE, Includer::R, Includer::R_ONCE);
    	if (in_array($type, $allowedValues)){
    		$this->type = $type;
    	}
    	else {
    		die("<b>Message:</b> The allowed walues for <b>type</b> is: '<b>Includer::I</b>', '<b>Includer::I_ONCE</b>', '<b>Includer::R</b>', '<b>Includer::R_ONCE</b>'.");
    	}
    }
    /**
     * This function help the user can choose which one suits him better.
     * Use setType function to setting the correct type.
     * The Default type is "include_once". 
     * 
     * @param string $filename
     */
    private function importStrategy($filename){
    	switch ($this->type){
    		case self::I:
    			include ($filename);
    			break;
    		case self::I_ONCE:
    			include_once ($filename);
    			break;
    		case self::R:
    			require ($filename);
    			break;
    		case self::R_ONCE:
    			require_once ($filename);
    			break;
    		default:
    			die("ERROR!");
    			break;
    	}
    }
    /**
     * Import the class files. (*.class.php)
     *
     */
    public function ImportClasses(){
    	foreach ($this->classList as $file){
    		$this->importStrategy($file['path']);
    	}
    }
    /**
     * Import the inc files. (*.inc.php)
     *
     */
    public function ImportIncFiles(){
    	foreach ($this->incfileList as $file){
    		$this->importStrategy($file['path']);
    	}
    }
    /**
     * Import the php files. (*.php)
     *
     */
    public function ImportPhpFiles(){
    	foreach ($this->phpfileList as $file){
    		$this->importStrategy($file["path"]);	
    	}
    }
    /**
     * Import all files (class, inc and php files)
     *
     */
    public function ImportAll(){
    	$this->ImportIncFiles();
    	$this->ImportClasses();
    	$this->ImportPhpFiles();  
    }
    /**
     * Add an array or a value to blacklist array.
     *
     * @param string or array $item
     */
    public function addBlacklist($item){
    	if (is_array($item)) {
    		foreach ($item as $filename){
    			array_push($this->blacklist, $filename);
    		}
    	}
    	else {
    		array_push($this->blacklist, $item);
    	}
    	
    }
    /**
     * Empty the blacklist array.
     *
     */
    public function emptyBlacklist(){
    	$this->blacklist = array();
    }
    /**
     * Add an array or a value to whitelist array
     *
     * @param string or array $item
     */
    public function addWhitelist($item){
    	if (is_array($item)){
    		foreach ($item as $filename){
    			array_push($this->whitelist, $filename);
    		}
    	}
    	else {
    		array_push($this->whitelist, $item);
    	}
    }
    /**
     * Emty the whitelist array.
     *
     */
    public function emptyWhitelist(){
    	$this->whitelist = array();
    }
    /**
     * Test the generated result.
	*/
    public function Test(){
    	print "<pre>PHP Files";
    	print_r ($this->phpfileList);
    	print "</pre>";
    	print "<pre>Subdirs";
    	print_r ($this->subdirList);
    	print "</pre>";
    	print "<pre>Class Files";
    	print_r ($this->classList);
    	print "</pre>";
    	print "<pre>Inc Files";
    	print_r ($this->incfileList);
    	print "</pre>";
    	print "<pre>Blacklist";
    	print_r ($this->blacklist);
    	print "</pre>";
    	print "<pre>Whitelist";
    	print_r ($this->whitelist);
    	print "</pre>";
    }
}
?>
Return current item: PHPIncluder