249 lines
5.1 KiB
PHP
Executable File
249 lines
5.1 KiB
PHP
Executable File
<?php
|
|
|
|
/**
|
|
* Manage includes
|
|
* @author franzz
|
|
* @version 1.2
|
|
*/
|
|
class ClassManagement extends PhpObject
|
|
{
|
|
const OBJECT_FOLDER = '../objects/';
|
|
const INC_FOLDER = 'inc/';
|
|
const INC_EXT = '.php';
|
|
const SETTINGS_FILE = 'settings.php';
|
|
const TOOLBOX_CLASS = 'toolbox';
|
|
const MAIN_CLASS_ABS = 'main';
|
|
|
|
private $asIncFiles;
|
|
|
|
function __construct($sMainClass)
|
|
{
|
|
parent::__construct(__CLASS__, true);
|
|
$this->asIncFiles = array();
|
|
|
|
//try to include default files
|
|
$this->incFile(self::SETTINGS_FILE);
|
|
$this->incClass(self::TOOLBOX_CLASS);
|
|
|
|
//Include main class
|
|
$this->incClass(self::MAIN_CLASS_ABS);
|
|
$this->incClass($sMainClass, true);
|
|
}
|
|
|
|
function __destruct()
|
|
{
|
|
parent::__destruct();
|
|
}
|
|
|
|
public function incClass($sClassName, $bProjectClass=false)
|
|
{
|
|
$sProject = $bProjectClass?'':self::OBJECT_FOLDER;
|
|
$sFile = $sProject.self::INC_FOLDER.$sClassName.self::INC_EXT;
|
|
return $this->incFile($sFile);
|
|
}
|
|
|
|
public function incFile($sFilePath, $bMandatory=true)
|
|
{
|
|
$bIncluded = false;
|
|
if(!in_array($sFilePath, $this->asIncFiles))
|
|
{
|
|
$sMissingFile = 'File "'.$sFilePath.'" missing.';
|
|
if(file_exists($sFilePath))
|
|
{
|
|
$bIncluded = require_once($sFilePath);
|
|
$this->asIncFiles[] = $sFilePath;
|
|
}
|
|
elseif($bMandatory)
|
|
{
|
|
die($sMissingFile.' Stopping process.');
|
|
}
|
|
else
|
|
{
|
|
$this->addError($sMissingFile);
|
|
}
|
|
}
|
|
return $bIncluded;
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @author franzz
|
|
* @version 1.1
|
|
*/
|
|
class PhpObject
|
|
{
|
|
//Log file name
|
|
const LOG_FILENAME = 'log.html';
|
|
|
|
//Message types
|
|
const NOTICE_TAB = 'Notice';
|
|
const WARNING_TAB = 'Warning';
|
|
const ERROR_TAB = 'Error';
|
|
const ALL_TAB = 'All';
|
|
|
|
//Extraction mode
|
|
const MODE_ARRAY = 0;
|
|
const MODE_TEXT = 1;
|
|
const MODE_HTML = 2;
|
|
const MODE_FILE = 3;
|
|
|
|
//Class variables
|
|
private $asMessageStack;
|
|
private $iExtractMode;
|
|
private $sChildClass;
|
|
private $bDebug;
|
|
|
|
function __construct($sClass='', $bDebug=false, $iExtractMode=self::MODE_FILE)
|
|
{
|
|
$this->resetMessageStack();
|
|
$this->setDebug($bDebug);
|
|
$this->iExtractMode = $iExtractMode;
|
|
$this->sChildClass = $sClass;
|
|
}
|
|
|
|
public function setDebug($bDebug)
|
|
{
|
|
$this->bDebug = $bDebug;
|
|
}
|
|
|
|
public function getDebug()
|
|
{
|
|
return $this->bDebug;
|
|
}
|
|
|
|
private static function getLogPath()
|
|
{
|
|
return dirname(__FILE__).'/'.self::LOG_FILENAME;
|
|
}
|
|
|
|
private function resetMessageStack($sType=self::ALL_TAB)
|
|
{
|
|
if($sType==self::ALL_TAB)
|
|
{
|
|
$this->resetMessageStack(self::NOTICE_TAB);
|
|
$this->resetMessageStack(self::WARNING_TAB);
|
|
$this->resetMessageStack(self::ERROR_TAB);
|
|
}
|
|
else
|
|
{
|
|
$this->asMessageStack[$sType] = array();
|
|
}
|
|
}
|
|
|
|
protected function addNotice($sNotice)
|
|
{
|
|
$this->addMessage(self::NOTICE_TAB, $sNotice);
|
|
}
|
|
|
|
protected function addWarning($sWarning)
|
|
{
|
|
$this->addMessage(self::WARNING_TAB, $sWarning);
|
|
}
|
|
|
|
protected function addError($sError)
|
|
{
|
|
$this->addMessage(self::ERROR_TAB, $sError);
|
|
}
|
|
|
|
private function addMessage($sType, $sMessage)
|
|
{
|
|
$this->asMessageStack[$sType][] = $sMessage;
|
|
}
|
|
|
|
protected function getCleanMessageStack($sType=self::ALL_TAB)
|
|
{
|
|
$asMessages = ($sType==self::ALL_TAB)?$this->asMessageStack:$this->asMessageStack[$sType];
|
|
$this->resetMessageStack($sType);
|
|
|
|
return $this->glueMessages($asMessages);
|
|
}
|
|
|
|
protected function cleanMessageStack()
|
|
{
|
|
$sErrorStack = $this->getCleanMessageStack($this->getDebug()?self::ALL_TAB:self::ERROR_TAB);
|
|
if($sErrorStack!='')
|
|
{
|
|
switch($this->iExtractMode)
|
|
{
|
|
case self::MODE_TEXT:
|
|
echo $sErrorStack;
|
|
break;
|
|
case self::MODE_HTML:
|
|
echo $sErrorStack;
|
|
break;
|
|
case self::MODE_ARRAY:
|
|
break;
|
|
case self::MODE_FILE:
|
|
@file_put_contents(self::getLogPath(), "\n\n".$this->sChildClass.' - '.date('r')."\n".$sErrorStack, FILE_APPEND);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function getCleanMessageStacks($aoExtsources, $sType=self::ALL_TAB)
|
|
{
|
|
$aoExtsources[] = $this;
|
|
$aoMessages = array();
|
|
foreach($aoExtsources as $oExtSource)
|
|
{
|
|
$oMessages = $oExtSource->getCleanMessageStack($sType);
|
|
if($oMessages!='')
|
|
{
|
|
$aoMessages[get_class($oExtSource)] = $oMessages;
|
|
}
|
|
}
|
|
return $this->glueMessages($aoMessages);
|
|
}
|
|
|
|
private function glueMessages($asMessages)
|
|
{
|
|
switch($this->iExtractMode)
|
|
{
|
|
case self::MODE_TEXT:
|
|
$oMessageStack = self::recursiveImplode("\n", $asMessages);
|
|
break;
|
|
case self::MODE_HTML:
|
|
$oMessageStack = self::recursiveImplode('<br />', $asMessages);
|
|
break;
|
|
case self::MODE_ARRAY:
|
|
$oMessageStack = $asMessages;
|
|
break;
|
|
case self::MODE_FILE:
|
|
$oMessageStack = self::recursiveImplode("\n", $asMessages);
|
|
break;
|
|
}
|
|
return $oMessageStack;
|
|
}
|
|
|
|
private static function flattenMessageStack($asTab, $sGlobalKey='')
|
|
{
|
|
$asFlatTab = array();
|
|
foreach($asTab as $oKey=>$oRow)
|
|
{
|
|
$sKey = is_numeric($oKey)?$sGlobalKey:$oKey.' - ';
|
|
if(is_array($oRow))
|
|
{
|
|
$asFlatTab = array_merge($asFlatTab, self::flattenMessageStack($oRow, $sKey));
|
|
}
|
|
else
|
|
{
|
|
$asFlatTab[] = $sKey.$oRow;
|
|
}
|
|
}
|
|
return $asFlatTab;
|
|
}
|
|
|
|
private static function recursiveImplode($sGlue, $asTab)
|
|
{
|
|
$asTab = self::flattenMessageStack($asTab);
|
|
return implode($sGlue, $asTab);
|
|
}
|
|
|
|
function __destruct()
|
|
{
|
|
$this->cleanMessageStack();
|
|
}
|
|
}
|
|
|
|
?>
|