|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webino™ (http://webino.sk) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/webino for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2015-2017 Webino, s.r.o. (http://webino.sk) |
|
7
|
|
|
* @author Peter Bačinský <[email protected]> |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace WebinoAppLib\Application\Traits; |
|
12
|
|
|
|
|
13
|
|
|
use WebinoAppLib\Application; |
|
14
|
|
|
use WebinoAppLib\Application\Config; |
|
15
|
|
|
use WebinoAppLib\Event\AppEvent; |
|
16
|
|
|
use WebinoAppLib\Exception; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Trait Config |
|
20
|
|
|
*/ |
|
21
|
|
|
trait ConfigTrait |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Config |
|
25
|
|
|
*/ |
|
26
|
|
|
private $config; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Return registered service |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $service Service name |
|
32
|
|
|
* @return mixed |
|
33
|
|
|
*/ |
|
34
|
|
|
abstract public function get($service); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $name |
|
38
|
|
|
* @param mixed $service |
|
39
|
|
|
*/ |
|
40
|
|
|
abstract protected function setServicesService($name, $service); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Attach a listener to an event |
|
44
|
|
|
* |
|
45
|
|
|
* @param string|\Zend\EventManager\ListenerAggregateInterface $event |
|
46
|
|
|
* @param string|callable|int $callback If string $event provided, expects PHP callback; |
|
47
|
|
|
* @param int $priority Invocation priority |
|
48
|
|
|
* @return \Zend\Stdlib\CallbackHandler|mixed CallbackHandler if attaching callable |
|
49
|
|
|
* (to allow later unsubscribe); mixed if attaching aggregate |
|
50
|
|
|
*/ |
|
51
|
|
|
abstract public function bind($event, $callback = null, $priority = 1); |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Require service from services into application |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $service Service name |
|
57
|
|
|
* @throws Exception\DomainException Unable to get service |
|
58
|
|
|
*/ |
|
59
|
|
|
abstract protected function requireService($service); |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string|null $name |
|
63
|
|
|
* @param mixed|null $default |
|
64
|
|
|
* @return Config|mixed |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getConfig($name = null, $default = null) |
|
67
|
|
|
{ |
|
68
|
|
|
if (null === $this->config) { |
|
69
|
|
|
$this->requireService(Application::CONFIG); |
|
70
|
|
|
$this->setServicesService(Application::CONFIG, $this->config); |
|
71
|
|
|
} |
|
72
|
|
|
return $name ? $this->config->get($name, $default) : $this->config; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param array|Config|object $config |
|
77
|
|
|
* @throws Exception\InvalidArgumentException Expected config as array|Config |
|
78
|
|
|
* @throws Exception\DomainException Disallowed config modifications |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setConfig($config) |
|
81
|
|
|
{ |
|
82
|
|
|
if (is_array($config)) { |
|
83
|
|
|
$this->getConfig()->mergeConfig($config); |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (!($config instanceof Config)) { |
|
88
|
|
|
throw (new Exception\InvalidArgumentException('Expected config of type %s but got %s')) |
|
89
|
|
|
->format(Config::class, $config); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if ($this->config && $this->config->isReadOnly()) { |
|
93
|
|
|
throw new Exception\DomainException( |
|
94
|
|
|
'Unable to set new application configuration; restricted to read only' |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->config = $config; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getCoreConfig($name = null, $default = null) |
|
105
|
|
|
{ |
|
106
|
|
|
$config = $this->get(Application::CORE_CONFIG); |
|
107
|
|
|
return $name ? $config->get($name, $default) : $config; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param callable $callback |
|
112
|
|
|
*/ |
|
113
|
|
|
public function onConfig(callable $callback) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->bind(AppEvent::CONFIGURE, function (AppEvent $event) use ($callback) { |
|
116
|
|
|
$event->getApp()->setConfig(call_user_func($callback)); |
|
117
|
|
|
}); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|