Location: PHPKode > projects > WebMesh Framework (APIs and MVC) > webmesh/WEB-INF/Importer.php
<?php
// Require class
require_once("classes/phpx/import/ImporterException.php");

/**
 * Class to centralize the imports using the include_once function.
 * Before anything, this class must be included and an instance taken.
 * 
 * In current version, this class is a Singleton object and provide
 * the method getInstance to get unique instance.
 * 
 * This class must be placed in the WEB-INF directory to work correctly.
 * If is not in the WEB-INF directory, change the attribute $basePath to
 * a specific direcotry or use the method setBasePath to indicate the directory 
 * to be used as base path.
 *
 * Example:
 * require_once("Importer.php");
 * $importer = &Importer::getInstance();
 * $importer->importClass("lang/String.php");
 * 
 * @package		default
 * @author		Gustavo Gomes
 * @copyright	2007 Gustavo Gomes
 * @version		1.1
 */
final class Importer {
	
	// An instance of this object
	private static $instance = null;
	
	// The base path for API used in the projects
	private static $apiPHP = "classes/phpx/";

	// Base path for WEB-INF directory
	private static $basePath = "";
	
	// The default project package
	private static $project = "com.company.project/";
	
	// The default template package
	private $templatePackage = "view/";
	
	// The default template engine
	private $templateEngine = "smarty";

	/**
	 * Constructor
	 * Set basePath and import default classes
	 */
	private function __construct() {
		// Define the path to the importer
		self::$basePath = dirname(__FILE__).DIRECTORY_SEPARATOR;

		// import the essential classes
		$this->defaultImport();
	}
	
	/**
	 * Get instance of the class
	 * If was the first call, use method argument in the class constructor
	 * 
	 * @param	boolean $initDefault
	 * @return	Importer
	 */
	public static function &getInstance() {
		if (self::$instance == null)
			self::$instance = new Importer();
		return self::$instance;
	}
	
	/**
	 * Set project name
	 * 
	 * @param	string $name
	 */
	public static function setProject($name) {
		self::$project = $name;
	}
	
	/**
	 * Get project name
	 * 
	 * @return	string
	 */
	public static function getProject() {
		return self::$project;
	}

	/**
	 * Set template engine defined to this project
	 * 
	 * @param	string $name
	 */
	public function setTemplateEngine($name) {
		$this->templateEngine = $name;
	}
	
	/**
	 * Get template engine defined to this project
	 * 
	 * @return	string
	 */
	public function getTemplateEngine() {
		return $this->templateEngine;
	}

	/**
	 * Set template package defined to this project
	 * 
	 * @param	string $name
	 */
	public function setTemplatePackage($name) {
		$this->templatePackage = $name;
	}
	
	/**
	 * Get template package defined to this project
	 * 
	 * @return	string
	 */
	public function getTemplatePackage() {
		return $this->templatePackage;
	}
	
	/**
	 * Get base path
	 * 
	 * @return	string
	 */
	public static function getBasePath() {
		return self::$basePath;
	}
	
	/**
	 * Get class path
	 * This path is generally [<basePath>/classes/]
	 * 
	 * @return	string
	 */
	public static function getClassPath() {
		return self::$basePath."classes/";
	}
	
	/**
	 * Get lib path
	 * This path is generally [<basePath>/lib/]
	 * 
	 * @return	string
	 */
	public static function getLibPath() {
		return self::$basePath."lib/";
	}
	
	/**
	 * Get phpx package path
	 * This path is generally [<basePath>/classes/phpx/]
	 * 
	 * @return	string
	 */
	public static function getPhpxPath() {
		return self::$basePath.self::$apiPHP;
	}
	
	/**
	 * Get project path
	 * This path is generally [<basePath>/classes/<projectName>]
	 * 
	 * @return	string
	 */
	public static function getProjectPath() {
		return self::$basePath."classes/".self::$project;
	}

	/**
	 * Import any file contained on $basePath directory
	 *
	 * @param	string $file
	 */
	public function import($file) {
		$this->applyImport("classes/",$file);
	}

	/**
	 * Import file contained on classes/ directory
	 *
	 * @param	string $file
	 */
	public function importClass($file) {
		$this->applyImport(self::$apiPHP,$file);
	}
	
	/**
	 * Import classes within one project previously specified
	 * 
	 * @param	string $file
	 */
	public function importProjectClass($file) {
		$this->applyImport("classes/".self::$project,$file);
	}

	/**
	 * Import template classes within one project previously specified
	 * 
	 * @param	string $file
	 * @param	string $viewPackage
	 */
	public function importTemplateProjectClass($file, $viewPackage=null) {
		if ($viewPackage == null)
			$this->importProjectClass($this->templatePackage.$this->templateEngine."/".$file);
		else
			$this->importProjectClass($viewPackage.$this->templateEngine."/".$file);
	}

	/**
	 * Import file contained on lib/ directory
	 *
	 * @param	string $file
	 */
	public function importLib($file) {
		$this->applyImport("lib/",$file);
	}

	/**
	 * Import defaut session class
	 */
	public function importSession() {
		$this->importClass("http/HttpSession.php");
	}

	/**
	 * Import a set of components/classes
	 */
	public function defaultImport() {
		$this->importClass("http/HttpService.php");
		
		// Fundamentals classes
		$this->importClass("lang/String.php");
		$this->importClass("lang/IllegalStateException.php");
		$this->importClass("lang/IllegalArgumentException.php");
	}

	/**
	 * Import a class or interface located in a specific path and file
	 * Generic import file based on $basePath attribute
	 *
	 * @param	string $subdir
	 * @param	string $file
	 * @throws	ImporterException
	 */
	public function applyImport($subdir, $file) {
		if ($file != "") {
			$partes = explode(".",$file);
			if (eregi("php",$partes[count($partes)-1]))
				$this->includeFile(self::$basePath.$subdir,$file);
			else
				$this->includeFile(self::$basePath.$subdir,$file.".php");
		}
	}

	/**
	 * Implement a better security in include method
	 * Provide validation extension, file existing on path indicate
	 *
	 * @param	string $subDir
	 * @param	string $file
	 * @param	array $validExt
	 * @throws	ImporterException
	 */
	private function includeFile($uriDir, $file, $validExt=null) {
		if (file_exists($uriDir.$file)) {
			if (is_null($validExt)) {
				include_once($uriDir.$file);
			} else if (is_array($validExt)) {
				$ext = split(".",$file);
				$ext = $ext[count($ext)-1];
				if (in_array($ext,$validExt))
					include_once($uriDir.$file);
				else
					throw new ImporterException("Invalid Extension ".$uriDir.$ext);
			}
		} else {
			throw new ImporterException("File not found - ".$uriDir.$file);
		}
	}
}
?>
Return current item: WebMesh Framework (APIs and MVC)