AvocadoAmber/AvocadoEdition_Light/classes/module.class.php

112 lines
2.3 KiB
PHP

<?php
class Module
{
protected static $performanceStopwatch = [];
protected $error = 0;
protected $result = "";
protected $variables = [];
protected $createdAt;
public function __construct($err = 0)
{
$this->createdAt = microtime(true);
$this->setError($err);
}
public function init($data = [])
{
$this->triggerEvent('beforeInit');
if (!empty($data)) {
$this->adds($data);
}
$this->triggerEvent('afterInit');
}
public function setResult($msg = "")
{
$this->result = $msg;
}
public function getResult()
{
return $this->result;
}
public function setError($err = 0)
{
$this->error = $err;
}
public function getError()
{
return $this->error;
}
public function get($key)
{
return array_key_exists($key, $this->variables) ? $this->variables[$key] : null;
}
public function gets()
{
$args = func_get_args();
$ret = new stdClass();
foreach ($args as $arg) {
$ret->$arg = $this->get($arg);
}
return $ret;
}
public function add($key, $val)
{
$this->variables[$key] = $val;
}
public function adds($obj)
{
if (is_object($obj)) {
$obj = get_object_vars($obj);
}
if (is_array($obj)) {
foreach ($obj as $k => $v) {
$this->variables[$k] = $v;
}
}
}
protected function addStopwatch($key)
{
if (defined("__IS_DEBUG__")) {
if (count($this->performanceStopwatch) > 0) {
$prev = end($this->performanceStopwatch);
$this->performanceStopwatch[$key] = microtime(true) - $this->createdAt - $prev;
} else {
$this->performanceStopwatch[$key] = microtime(true) - $this->createdAt;
}
}
}
public function addStopwatchWithCallStack($key)
{
if (defined("__IS_DEBUG__")) {
if ($key === 0)
$key = "start";
else if ($key === 1)
$key = "end";
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$cf = $backtrace[1]['function'] ? $backtrace[1]['function'] : 'global scope';
$this->addStopwatch("{$cf} {$key}");
}
}
public function addEventHandler($event, $callback)
{
EventHandler::addEventHandler($event, $callback);
}
protected static function triggerEvent($event, $data = [])
{
EventHandler::triggerEvent($event, $data);
}
}