User Tools

Site Tools


php:kompost:includeclasses

[PHP] InlcudeClasses

Diese Klasse ist meine Alternative zu autoload(). autoload() wird bei grösseren Projekten schnell langsam und unübersichtlich. Die Pfade werden ab dem root in der Datei 'include.properties' hinterlegt. Wenn eine Datei bereits mit include_once() geladen wurde, dann wird dies gespeichert und bei erneutem laden nicht nochmal geladen.

Achtung. Diese Klasse ist eine Vererbung von [PHP] Property Reader. Diese wird also auch verwendet.

IncludeClasses.php
<?php
/**
* @copyright	© by ERB software
*       		All rights reserved
* @author		stefan.erb@erb-software.com
*/
 
/**
 * @example
 * IncludeClasses::import('Smarty');
 *
 */
class IncludeClasses extends PropertyReader implements IPropertyClass{
	public $__CHIELD__ = __CLASS__;
 
	const PROPERTIES_DEFAULT_PATH = 'include.properties';
 
	protected static $instance;
 
	/**
	 * Singleton-Instanz erzeugen
	 * @param <String> $propetyFile    Name der Propertydatei
	 */
	public static function singleton($propetyFile = self::PROPERTIES_DEFAULT_PATH){
		if (!isset(self::$instance)) {
			$c = __CLASS__;
			self::$instance = new $c;
		}	    
		return self::$instance;	     
	}
 
	/**
	 * include_once auf ein Propertiy
	 * @param <String> $className      Name des Property
	 * @param <String> $propetyFile    Name der Propertydatei
	 */
	public static function import($className, $propetyFile = self::PROPERTIES_DEFAULT_PATH){
		try {
			self::singleton($propetyFile)->includeByKey($className);
		} catch (Exception $e){
			echo ($e);
		}
	}
 
 
	/**
	* @var      Array<String>   array of included filepaths
	* @access   private
	*/
	private $cach = array();
 
	/**
	* @access  public
	*/
	public function __construct($propetyFile = self::PROPERTIES_DEFAULT_PATH){
		parent::__construct($propetyFile);
	}
 
	/**
	* @access   public
	* @param    String key
	* @retrun   Boolean
	* @throw    Exception
	*/
	public function includeByKey($key){
		$path = $this->$key;
		$retVal = false;
 
		/**  check if the file is allreadx included */
		if (in_array($path, $this->cach)){
		/** check if the file exist */
		} elseif (file_exists($path)){
			/** include the file */
			include_once($path);
			/** add the path to the cach */
			$this->cach[] = $path;
			$retVal = true;
		/** else create error */
		} else {
			throw new Exception(__METHOD__." cant include the file {$key} => {$path}");
		}
 
 
		return $retVal;
	}
}
?>

Und hier ein Beispiel eines entsprechenden Proerty-Files

include.properties
##
# @Copyright	mpl by ERB software
# @cauthor		stefan.erb@erb-software.com
##
 
##
# Wird von der Library IncludeClasses verwendet. 
##
 
#Functions
filterInputDefaults                = lib/functions/filterInputDefaults.php
ifnull                             = lib/functions/ifnull.php
buildPath                          = lib/functions/buildPath.php
objectToArray                      = lib/functions/objectToArray.php
ifNotKeyExists                     = lib/functions/ifNotKeyExists.php
array_extract_sub_item             = lib/functions/array_extract_sub_item.php
 
#Libraries
Libs                               = lib/Libs.php
Database                           = lib/db/mysql/include.php
Smarty                             = lib/Smarty/libs/Smarty.class.php
 
#Exceptions
UserException                      = app/exceptions/UserException.php
FieldException                     = app/exceptions/FieldException.php
UserExceptions                     = app/exceptions/UserExceptions.php
FieldExceptions                    = app/exceptions/FieldExceptions.php
 
#Application
DropDownListDataFactory            = app/DropDownListDataFactory.php
FileInfo                           = app/FileInfo.php
Checker                            = app/Checker.php
SqlHelper                          = app/SqlHelper.php
 
#Application.Upload
Uploader                           = app/upload/Uploader.php
UploadProperties                   = app/upload/UploadProperties.php
 
#Application.Upload.FieldDefs
IUploadField                       = app/upload/fieldDefs/IUploadField.php
UploadField                        = app/upload/fieldDefs/UploadField.php
UploadFieldFile                    = app/upload/fieldDefs/UploadFieldFile.php
 
 
#Action
UploadUkatDDList                   = webComponent/action/classes/UploadUkatDDList.php
 
#Ajax
AjaxUploadUkatDDList               = webComponent/ajax/ajaxUploadUkatDDList.php
AjaxUploadFile                     = webComponent/ajax/ajaxUploadFile.php

Am Anfang jedes Scriptes liste ich nun einfach alle Dinge auf die ich verwende und kümmere mich nicht mehr darum ob das Ding schon geladen ist und was der genaue Pfad ist. Im gegensatz zu autoload können so auch Nicht-Klassenscripte so geladen werden.

beispiel1.php
<?php
/**
 * mpl           by ERB software
 * @author       stefan.erb(at)erb-software.com
 */
 
 
/**
 * benötigte Scripte für dieses Script importieren 
 */
//Funktionen
IncludeClasses::import('objectToArray');
IncludeClasses::import('ifnull');
IncludeClasses::import('ifNotKeyExists');
IncludeClasses::import('array_extract_sub_item');
//Klassen
IncludeClasses::import('UploadProperties');
IncludeClasses::import('FieldException');
IncludeClasses::import('FieldExceptions');
IncludeClasses::import('FileInfo');
IncludeClasses::import('SqlHelper');
IncludeClasses::import('Checker');
 
//TODO: Das Script schreiben
php/kompost/includeclasses.txt · Last modified: 11.12.2013 11:47:08 (external edit)