Files
objects/inc/PhpObject.php

192 lines
4.1 KiB
PHP

<?php
namespace Franzz\Objects;
use \Settings;
/**
* 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();
}
}
?>