|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace uuf6429\ElderBrother\Config; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
use uuf6429\ElderBrother\Action\ActionAbstract; |
|
8
|
|
|
|
|
9
|
|
|
class Config implements ConfigInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $config = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* {@inheritdoc} |
|
18
|
|
|
*/ |
|
19
|
1 |
|
public function loadFromFile($fileName, LoggerInterface $logger) |
|
20
|
|
|
{ |
|
21
|
1 |
|
$eventActions = include $fileName; |
|
22
|
1 |
|
$this->loadFromArray($eventActions, $logger); |
|
23
|
1 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
4 |
|
public function loadFromArray($array, LoggerInterface $logger) |
|
29
|
|
|
{ |
|
30
|
|
|
// merge config and set up actions |
|
31
|
4 |
|
foreach ($array as $event => $prioritizedActions) { |
|
32
|
4 |
|
foreach ($prioritizedActions as $key => $action) { |
|
33
|
3 |
|
$this->setUpAction($action, $logger); |
|
34
|
4 |
|
$this->config[$event][$key] = $action; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// reorder actions by index |
|
39
|
4 |
|
array_map('ksort', $this->config); |
|
40
|
4 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function getAllEventActions() |
|
46
|
|
|
{ |
|
47
|
1 |
|
return (array) $this->config; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
4 |
|
public function getActionsForEvent($event, $supportedOnly = true) |
|
54
|
|
|
{ |
|
55
|
4 |
|
$config = isset($this->config[$event]) |
|
56
|
4 |
|
? array_values($this->config[$event]) : []; |
|
57
|
|
|
|
|
58
|
4 |
|
if ($supportedOnly) { |
|
59
|
4 |
|
$config = array_filter( |
|
60
|
|
|
$config, |
|
61
|
4 |
|
function (ActionAbstract $action) { |
|
62
|
3 |
|
return $action->isSupported(); |
|
63
|
4 |
|
} |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
4 |
|
return $config; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param ActionAbstract $action |
|
72
|
|
|
* @param LoggerInterface $logger |
|
73
|
|
|
*/ |
|
74
|
3 |
|
private function setUpAction(ActionAbstract $action, LoggerInterface $logger) |
|
75
|
|
|
{ |
|
76
|
3 |
|
if ($action instanceof ConfigAwareInterface) { |
|
77
|
3 |
|
$action->setConfig($this); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
if ($action instanceof LoggerAwareInterface) { |
|
81
|
3 |
|
$action->setLogger($logger); |
|
82
|
|
|
} |
|
83
|
3 |
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|