Location: PHPKode > scripts > ubinclude > ubinclude/include.php
<?php
class ubInclude
{
  
	private $list_class = array();
	
	private $list_file = array();
	
	private $path = array();
	
	private $dir_cache = ".";
	
	private $file_cache = ".";
	
	/**
	 * @return array
	 */
	public function get_list_class()
	{
		return $this->list_class;
	}
	
	/**
	 * @return array
	 */
	public function get_list_file()
	{
		return $this->list_file;
	}
	
	/**
	 * @param string $value
	 */
	public function set_file_cache($value)
	{
		$this->file_cache = $value;
	}
	
	/**
	 * @return string
	 */
	public function get_file_cache()
	{
		return $this->file_cache;
	}
	
	/**
	 * @param string $value
	 */
	public function set_dir_cache($value)
	{
		$this->dir_cache = $value;
	}

	/**
	 * @return string
	 */
	public function get_dir_cache()
	{
		return $this->dir_cache;
	}
	
	/**
	 * @return string
	 */
	public function get_dir()
	{
		return 	$this->dir_cache.DIRECTORY_SEPARATOR.$this->file_cache;	
	}
	
	/**
	* Constructor
	*
	* @param string or array $path
	*/
	public function __construct($dir_cache = ".",$file_cache = "file_cache")
	{
		$this->dir_cache = $dir_cache;
		$this->file_cache = $file_cache;
	}
	
  	/**
	 * Permite salvar la información en un fichero para evitar el mapeo constante de direcciones
	 */
	public function save_cache()
	{
		$dir = $this->get_dir();
		
		$var_save = array(
			'list_class' 	=> $this->list_class,
			'list_file' 	=> $this->list_file,
			'path' 			=> $this->path
		);
		
		if (($fd = @fopen($dir, 'w+')))
		{
	        fwrite($fd,serialize($var_save));
			fclose($fd);
		}
	}
	  
	/**
	 * Lee un fichero cache con la información deseada
	 *
	 * @return bool
	 */
	public function load_cache()
	{
		$dir = $this->get_dir();
		if(file_exists($dir))
		{
			$var_save = unserialize(file_get_contents($dir));
			$this->list_class 	= $var_save['list_class'];
			$this->list_file 	= $var_save['list_file'];
			$this->path 		= $var_save['path'];
			return true;
		}else
			return false;
	}
	
	/**
	* Permite adicionar un path
	*
	* @param string or array $path
	*/
	public function add_path($path ='.')
	{
		if(is_array($path))
		{
		   foreach ($path as $value) {
		   	  if(!in_array($value,$this->path))
		   	    $this->path[] = $value;
		   }
		}else
		  if(!in_array($path,$this->path))
		   	    $this->path[] = $path;
	}
  
	/**
	* Carga la información de las clases de los path introducidos
	* primeramente busca la existencia de un fichero de cache con la información
	* en caso de no encontrarlo de que se quiera sobreescribir se realiza el mapeo 
	* de las dirección pasadas
	*/
	public function load($rewrite = false)
	{
		if($rewrite || !$this->load_cache())
		{
			foreach ($this->path as $path) {
				$this->mapping($path);
			}
			$this->save_cache();
		}
	}
  
	/**
	 * Guarda en un arreglo la dirección de todos los ficheros que se encuentran 
	 * dentro de un path
	 *
	 * @param string $path
	 */
	public function mapping($path)
	{
		$handle=opendir($path);
		$list_ignore = array('.','..','.svn');
		
	    while ($file = readdir($handle))
	    {
	    	if(!in_array($file,$list_ignore))
	    	{
				$dirFile = "$path".DIRECTORY_SEPARATOR."$file";
				
				if(is_file($dirFile))
				{
					$return = array();
					if(preg_match("/.php$/",$file) || preg_match("/.phtml$/",$file))
					{
					  	$contents =  file_get_contents($dirFile);
						preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi',$contents , $classes);
						preg_match_all('~(?:new\s|extends\s|implements\s|@var\s|@class\s)+(\w+)~mi', $contents, $class_depends);
						preg_match_all('~(?:@file\s)+(\w+.\w+)~mi', $contents, $file_depends);
						preg_match_all('~(?:@ignore\s)+(\w+.\w+)~mi', $contents, $class_ignore);
						preg_match_all('~(\w+)(::)~mi', $contents, $class_statics);
					
						foreach ($class_statics[1] as $key => $value) 
						{
						  if($value == "parent" || $value == "self")
						    unset($class_statics[1][$key]);
						}
						
						$class_depends = array_merge($class_statics[1],$class_depends[1]);
						
						foreach ($class_depends as $key => $value)
						{
							if(is_numeric($key))
							{
								if(!in_array($value,$return))
									$return[] = $value;
							}else 
								$return[$key] = $value;
						}
						
						foreach ($classes[1] as $class)
						{
							$this->list_class[$class]['dir'] = $file;
						}
						
						$return = array_diff($return,$class_ignore[1]);
						
						$this->list_file[$file]['class_depend'] = $return;
						$this->list_file[$file]['file_depend'] = $file_depends[1];
					}
					$this->list_file[$file]['dir'] = $dirFile;
				}
		    	else
		    		$this->mapping($dirFile);	
	    	}
	    }
	    closedir($handle);
	}
	
  	/**
	 * Permite incluir cualquier clase registrada
	 *
	 * @param string $class
	 */
	public function include_class($class)
	{
		if(class_exists($class))
			return true;
			
		if(interface_exists($class))
			return true;
		
		if(count($this->list_class) == 0)
			$this->load();
		
		if($this->list_class[$class] && !$this->list_class[$class]['include'])
		{
			$this->list_class[$class]['include'] = true;
			$this->include_file($this->list_class[$class]['dir']);
			return true;
		}
		else
			return false;
	}
  
  	/**
	 * Permite incluir cualquier fichero registrado
	 *
	 * @param string $file
	 */
	public function include_file($file)
	{		
		if(count($this->list_file) == 0)
			$this->load();
		
		if($this->list_file[$file] && !$this->list_file[$class]['include'])
		{
			$this->list_file[$file]['include'] = true;
			$this->include_depende_file($file);
			include_once($this->list_file[$file]['dir']);
			return true;
		}
		else
			return false;
	}
	
	/**
	 * Devuelve la dirección de cualquier fichero registrado
	 *
	 * @param string $file
	 * @return string
	 */
	public function file_dir($file)
	{
		return $this->list_file[$file][dir];
	}
	
	/**
	 * Incluye todas las dependencias de un fichero registrado
	 *
	 * @param string $file
	 * @return bool
	 */
	public function include_depende_file($file)
	{
		if($this->list_file[$file])
		{
			if(is_array($this->list_file[$file]['class_depend']))
			{
				foreach ($this->list_file[$file]['class_depend'] as $value)
				{
					if(!class_exists($value))
						$this->include_class($value);
				}
			}
			if(is_array($this->list_file[$file]['file_depend']))
			{
				foreach ($this->list_file[$file]['file_depend'] as $value)
				{
					if(!class_exists($value))
						$this->include_file($value);
				}
			}
			return true;
		}
		else
			return false;
	}
}
?>
Return current item: ubinclude