Files
objects/class_management.php
2020-03-31 21:19:03 +02:00

257 lines
5.5 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 GLOBAL_SETTINGS_FILE = 'globalsettings.php';
const TOOLBOX_CLASS = 'toolbox';
const MAIN_CLASS_ABS = 'main';
private $asIncFiles;
function __construct($sMainClass)
{
parent::__construct(__FILE__, true);
$this->asIncFiles = array();
//try to include default files
$this->incFile(self::OBJECT_FOLDER.self::GLOBAL_SETTINGS_FILE);
$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;
}
}
/**
* PHP Object
* @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->asMessageStack = array();
$this->resetMessageStack();
$this->setDebug($bDebug);
$this->setExtractMode($iExtractMode);
$this->sChildClass = $sClass;
}
public function setExtractMode($iExtractMode) {
$this->iExtractMode = $iExtractMode;
}
/* private function, use Settings::DEBUG for global setting */
private 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][] = array('msg'=>$sMessage, 'time'=>date('d.m.Y H:i:s'), 'type'=>$sType, 'location'=>$this->sChildClass);
}
protected function getCleanMessageStack($sType=self::ALL_TAB)
{
if($sType==self::ALL_TAB) {
$asMessages = array_merge( //No overlap, numeric keys
$this->asMessageStack[self::NOTICE_TAB],
$this->asMessageStack[self::WARNING_TAB],
$this->asMessageStack[self::ERROR_TAB]
);
}
else $asMessages = $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".$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)
{
$asFlatTab = array();
foreach($asTab as $asRow) {
$asFlatTab[] = '['.$asRow['time'].'] '.$asRow['type'].' - '.$asRow['location'].' - '.$asRow['msg'];
}
return $asFlatTab;
}
private static function recursiveImplode($sGlue, $asTab)
{
$asTab = self::flattenMessageStack($asTab);
return implode($sGlue, $asTab);
}
function __destruct()
{
$this->cleanMessageStack();
}
}
?>