|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tylercd100\Notify\Drivers; |
|
4
|
|
|
|
|
5
|
|
|
use Monolog\Logger; |
|
6
|
|
|
use Monolog\Handler\HandlerInterface; |
|
7
|
|
|
use Tylercd100\Notify\Factories\MonologHandlerFactory as Factory; |
|
8
|
|
|
|
|
9
|
|
|
abstract class Base |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $levels = ["debug","info","notice","warning","error","critical","alert","emergency"]; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $config; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Logger |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $logger; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param array $config An array of config values to overwrite |
|
28
|
|
|
* @param Logger $logger A Monolog instance to use |
|
29
|
|
|
*/ |
|
30
|
15 |
|
public function __construct(array $config = [], Logger $logger = null){ |
|
31
|
|
|
//Merge the existing config with the provided config |
|
32
|
15 |
|
$this->config = array_merge_recursive(config('notify'),$config); |
|
33
|
|
|
|
|
34
|
15 |
|
if(!$logger instanceof Logger){ |
|
35
|
12 |
|
$logger = new Logger($this->config['channel']); |
|
36
|
12 |
|
} |
|
37
|
15 |
|
$this->logger = $logger; |
|
38
|
|
|
|
|
39
|
15 |
|
$this->attachDrivers(); |
|
40
|
15 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns a list of names that correspond to a config key and the Tylercd100\Notify\Factory::create method |
|
44
|
|
|
* @return array An array of drivers to use |
|
45
|
|
|
*/ |
|
46
|
|
|
abstract protected function getDrivers(); |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Gets a hanlder instance for the provided name |
|
50
|
|
|
* @param string $name The name of the driver you want to use |
|
51
|
|
|
* @return HandlerInterface |
|
52
|
|
|
*/ |
|
53
|
15 |
|
protected function getHandlerInstanceByName($name){ |
|
54
|
15 |
|
$config = (isset($this->config[$name]) ? $this->config[$name] : []); |
|
55
|
15 |
|
return Factory::create($name,$config); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Pushes a Monolog Handler in to the Monolog Logger instance |
|
60
|
|
|
* @param HandlerInterface $handler The Handler to attach |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
15 |
|
protected function attachHandler(HandlerInterface $handler){ |
|
64
|
15 |
|
$this->logger->pushHandler($handler); |
|
65
|
15 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* This will attach all the monolog handlers specified in the drivers config array |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
15 |
|
protected function attachDrivers() |
|
72
|
|
|
{ |
|
73
|
15 |
|
$drivers = $this->getDrivers(); |
|
74
|
15 |
|
foreach ($drivers as $driver) { |
|
75
|
15 |
|
$handler = $this->getHandlerInstanceByName($driver); |
|
76
|
15 |
|
$this->attachHandler($handler); |
|
77
|
15 |
|
} |
|
78
|
15 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* This will call the log functions of Monolog\Logger |
|
82
|
|
|
* @param string $method The method to call |
|
83
|
|
|
* @param array $arguments The arguments provided |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
3 |
|
public function __call($method, $arguments) |
|
87
|
|
|
{ |
|
88
|
3 |
|
if(in_array($method, $this->levels)){ |
|
89
|
3 |
|
call_user_func([$this->logger,$method],$arguments); |
|
90
|
3 |
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |