This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
ErrorHandler.php
105 lines (92 loc) · 2.56 KB
/
ErrorHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace prgTW\ErrorHandlerBundle;
use prgTW\ErrorHandler\Error\ErrorException;
use prgTW\ErrorHandler\Error\Severity;
use prgTW\ErrorHandler\Handler\HandlerInterface;
use prgTW\ErrorHandler\Metadata\Metadata;
use prgTW\ErrorHandler\Processor\ProcessorInterface;
class ErrorHandler implements HandlerInterface
{
/** @var \prgTW\ErrorHandler\ErrorHandler */
protected $errorHandler;
/**
* @param \prgTW\ErrorHandler\ErrorHandler $errorHandler
*/
public function __construct(\prgTW\ErrorHandler\ErrorHandler $errorHandler)
{
$this->errorHandler = $errorHandler;
register_shutdown_function(array($this, 'handleShutdown'));
}
/** {@inheritdoc} */
public function handleError(ErrorException $error, Metadata $metadata = null)
{
$metadata = $this->addDefaultCategory($metadata);
$this->errorHandler->handleError(
$error->getCode(),
$error->getMessage(),
$error->getFile(),
$error->getLine(),
$error->getContext(),
$metadata
);
}
/** {@inheritdoc} */
public function handleException(\Exception $exception, Metadata $metadata = null)
{
$metadata = $this->addDefaultCategory($metadata);
$this->errorHandler->handleException($exception, $metadata);
}
/** {@inheritdoc} */
public function handleEvent($event, Metadata $metadata = null)
{
$metadata = $this->addDefaultCategory($metadata);
$this->errorHandler->handleEvent($event, $metadata);
}
/**
* Handle fatal errors and such
*
* @codeCoverageIgnore
*/
public function handleShutdown()
{
$error = error_get_last();
if ($error && Severity::fromPhpErrorNo($error['type']) >= $this->errorHandler->getMinSeverityOnShutdown())
{
$error = new ErrorException($error['message'], $error['type'], $error['file'], $error['line']);
$this->handleError($error);
}
}
/**
* @param HandlerInterface $handler
* @param array $categories
*/
public function addHandler(HandlerInterface $handler, array $categories = array())
{
$categories = array() !== $categories ? $categories : array('default');
$this->errorHandler->getHandlerManager()->attach($handler, $categories);
}
/**
* @param ProcessorInterface $processor
*/
public function addProcessor(ProcessorInterface $processor)
{
$this->errorHandler->getProcessorManager()->attach($processor);
}
/**
* @param Metadata $metadata
*
* @return Metadata
*/
protected function addDefaultCategory(Metadata $metadata = null)
{
if (null === $metadata)
{
$metadata = new Metadata;
}
if (array() === $metadata->getCategories())
{
$metadata->addCategory('default');
}
return $metadata;
}
}