Commit 02c2e0c6 authored by Alex Ne's avatar Alex Ne

ETrace Interface Ready

parent a67eea3c
<?php
namespace X\ETrace;
class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable
{
/**
* Хранилище объектов
* @var array
*/
protected $__collection = [];
// --------------------------------------------------------------------
public function Serialize()
{
return serialize($this);
}
/**
* Проверяет тип объекта.
* Препятствует добавлению в коллекцию объектов `чужого` типа.
*
* @param object $object Объект для проверки
* @throws Exception
* @return void
*/
private function __check_type(&$object)
{
if ( ! ($object instanceof EItem))
{
throw new \Exception('Объект типа `' . get_class($object)
. '` не может быть добавлен в коллекцию объектов типа `' . $this->__type . '`');
}
}
// --------------------------------------------------------------------
/**
* Добавляет в коллекцию объекты, переданные в аргументах.
*
* @param object(s) Объекты
* @return mixed Collection
*/
public function add()
{
$args = func_get_args();
foreach ($args as $object)
{
$this->offsetSet(null, $object);
}
return $this;
}
// --------------------------------------------------------------------
/**
* Удаляет из коллекции объекты, переданные в аргументах.
*
* @param object(s) Объекты
* @return mixed Collection
*/
public function remove()
{
$args = func_get_args();
foreach ($args as $object)
{
unset($this->__collection[array_search($object, $this->__collection)]);
}
return $this;
}
// --------------------------------------------------------------------
/**
* Очищает коллекцию.
*
* @return mixed Collection
*/
public function clear()
{
$this->__collection = [];
return $this;
}
// --------------------------------------------------------------------
/**
* Выясняет, пуста ли коллекция.
*
* @return bool
*/
public function isEmpty()
{
return empty($this->__collection);
}
// --------------------------------------------------------------------
/**
* Реализация интерфейса IteratorAggregate
*/
/**
* Возвращает объект итератора.
*
* @return CollectionIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->__collection);
}
// --------------------------------------------------------------------
/**
* Реализация интерфейса ArrayAccess.
*/
/**
* Sets an element of collection at the offset
*
* @param ineter $offset Offset
* @param mixed $offset Object
* @return void
*/
public function offsetSet($offset, $object)
{
$this->__check_type($object);
$offset = $object->getHash();
if (isset($this->__collection[$offset]))
{
$this->__collection[$offset]->increment();
}
else
{
$this->__collection[$offset] = $object;
}
}
// --------------------------------------------------------------------
/**
* Выясняет существует ли элемент с данным ключом.
*
* @param integer $offset Ключ
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->__collection[$offset]);
}
// --------------------------------------------------------------------
/**
* Удаляет элемент, на который ссылается ключ $offset.
*
* @param integer $offset Ключ
* @return void
*/
public function offsetUnset($offset)
{
unset($this->__collection[$offset]);
}
// --------------------------------------------------------------------
/**
* Возвращает элемент по ключу.
*
* @param integer $offset Ключ
* @return mixed
*/
public function offsetGet($offset)
{
if (isset($this->__collection[$offset]) === FALSE)
{
return NULL;
}
return $this->__collection[$offset];
}
// --------------------------------------------------------------------
/**
* Реализация интерфейса Countable
*/
/**
* Возвращает кол-во элементов в коллекции.
*
* @return integer
*/
public function count()
{
return sizeof($this->__collection);
}
}
?>
\ No newline at end of file
......@@ -4,8 +4,12 @@ namespace X\ETrace;
/**
*
*/
class TItem extends \Exception
class EItem extends \Exception
{
/**
* @var int
*/
protected $count = 1;
/**
* @var mixed
*/
......@@ -38,6 +42,7 @@ class TItem extends \Exception
}
$this->context = $context;
$this->hash = $this->calcHash();
}
/**
......@@ -51,9 +56,9 @@ class TItem extends \Exception
/**
* @return mixed
*/
public function Dump()
public function Serialize()
{
return $this->Model();
return serialize($this->Model());
}
public function Model()
......@@ -65,14 +70,29 @@ class TItem extends \Exception
"code" => $this->code,
"file" => $this->file,
"line" => $this->line,
"count" => $this->count,
"trace" => $this->Trace(),
"context" => $this->context,
"globals" => $GLOBALS,
];
}
public function Save()
public function increment()
{
$this->count++;
}
/**
* @return hex
*/
public function getHash()
{
return $this->hash;
}
private function calcHash()
{
# code...
return md5(serialize([$this->message, $this->code, $this->file, $this->line, $this->Trace()]));
}
}
?>
\ No newline at end of file
<?php
namespace X\ETrace;
class Error extends TItem
class Error extends EItem
{
/**
* @var mixed
*/
protected $severity;
/**
* Для функции set_error_handler(). Для отлова ошибок.
*
* @param string $message
* @param int $code [= 0]
* @param int $severity [= 0]
* @param string $file [= false]
* @param int $line [= false]
* @param array $context [= []]
*/
public function __construct($message, $code = 0, $file = false, $line = false, $context = [])
public function __construct($message, $severity = 0, $file = false, $line = false, $context = [])
{
parent::__construct("error", $message, 0, $file, $line, $context);
$this->severity = $severity;
}
/**
* @return mixed
*/
public function getSeverity()
{
return $this->severity;
}
/**
* @return mixed
*/
public function Trace()
{
$trace = parent::Trace();
array_shift($trace);
return $trace;
}
public function Model()
{
parent::__construct("error", $message, $code, $file, $line, $context);
return array_merge(["severity" => $this->severity], parent::Model());
}
}
?>
\ No newline at end of file
<?php
namespace X\ETrace;
class Fatal extends EItem
{
/**
* @var mixed
*/
protected $severity;
/**
* Для функции register_shutdown_function(). Для отлова критических ошибок.
* Используется отлов последней ошибки error_get_last() котора подходит под условие критической ошибки.
*
* @param $message
* @param $severity
* @param $file
* @param false $line
* @param array $context
*/
public function __construct($message, $severity = 0, $file = false, $line = false)
{
parent::__construct("fatal", $message, 0, $file, $line, []);
$this->severity = $severity;
}
/**
* @return mixed
*/
public function getSeverity()
{
return $this->severity;
}
/**
* @return mixed
*/
public function Trace()
{
$trace = parent::Trace();
array_shift($trace);
return $trace;
}
}
?>
\ No newline at end of file
<?php
namespace X\ETrace;
class Notification extends EItem
{
/**
* Упрощенный интерфейс для посылки сообщений администратору из неких участков кода.
*
* @param $message
* @param array $context
*/
public function __construct($message, $context = [])
{
parent::__construct("notification", $message, 0, false, false, $context);
}
/**
* @return mixed
*/
public function Model()
{
$model = parent::Model();
unset($model["global"]);
return $model;
}
}
?>
<?php
namespace X\ETrace;
class Paranoid extends EItem
{
/**
* Параноидальный лог. Нужен, что бы не сыпался в основной лог, но с возможностю отслежки недостатков кода.
*
* @param $message
* @param array $context
*/
public function __construct($message, $code = 0, $context = [])
{
parent::__construct("paranoid", $message, $code, false, false, $context);
}
}
?>
\ No newline at end of file
<?php
namespace X\ETrace;
class Trace extends EItem
{
/**
* Упрощенный интерфейс для отладки кода, проверки стека и переменных окружения.
*
* @param $message
* @param array $context
*/
public function __construct($message, $context = [])
{
parent::__construct("trace", $message, 0, false, false, $context);
}
}
?>
<?php
require_once "TItem.php";
require_once "Error.php";
//throw new ErrorException("Error Processing Request", 1);
class wwwww
{
/**
* @var mixed
*/
static $eer;
/**
* @param $value
* @param $rrrr
*/
public function qwewqew($value, $rrrr)
{
$ee = new X\ETrace\Error("Error Processing Request", 1);
print_r(
$ee->Trace()
);
}
public static function qqq()
{
self::$eer = new self();
self::fff();
}
public static function fff()
{
self::$eer->qwewqew([1, 2, 5, 7, 9], "dfgd");
}
}
wwwww::qqq();
//throw $ddd;
?>
\ No newline at end of file
<?php
require_once "test.php";
?>
\ No newline at end of file
<?php
require_once "test2.php";
?>
\ No newline at end of file
<?php
function FunctionName()
{
require_once "test3.php";
# code...
}
FunctionName();
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment