forked from easy-swoole/easyswoole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrigger.php
76 lines (64 loc) · 1.76 KB
/
Trigger.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
<?php
/**
* Created by PhpStorm.
* User: yf
* Date: 2018/8/14
* Time: 下午6:17
*/
namespace EasySwoole\EasySwoole;
use EasySwoole\Component\Event;
use EasySwoole\Component\Singleton;
use EasySwoole\Trigger\Location;
use EasySwoole\Trigger\TriggerInterface;
class Trigger implements TriggerInterface
{
use Singleton;
private $trigger;
private $onError;
private $onException;
function __construct(TriggerInterface $trigger)
{
$this->trigger = $trigger;
$this->onError = new Event();
$this->onException = new Event();
}
public function error($msg,int $errorCode = E_USER_ERROR,Location $location = null)
{
// TODO: Implement error() method.
if($location == null){
$location = $this->getLocation();
}
$this->trigger->error($msg,$errorCode,$location);
$all = $this->onError->all();
foreach ($all as $call){
call_user_func($call,$msg,$errorCode,$location);
}
}
public function throwable(\Throwable $throwable)
{
// TODO: Implement throwable() method.
$this->trigger->throwable($throwable);
$all = $this->onException->all();
foreach ($all as $call){
call_user_func($call,$throwable);
}
}
public function onError():Event
{
return $this->onError;
}
public function onException():Event
{
return $this->onException;
}
private function getLocation():Location
{
$location = new Location();
$debugTrace = debug_backtrace();
array_shift($debugTrace);
$caller = array_shift($debugTrace);
$location->setLine($caller['line']);
$location->setFile($caller['file']);
return $location;
}
}