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\Service; |
12
|
|
|
|
13
|
|
|
use WebinoAppLib\Application; |
14
|
|
|
use WebinoAppLib\Event\AppEvent; |
15
|
|
|
use WebinoAppLib\Feature\Config; |
16
|
|
|
use WebinoAppLib\Log; |
17
|
|
|
use WebinoConfigLib\Feature\ConfigCacheEnabled; |
18
|
|
|
use Zend\ServiceManager\Config as ServicesConfig; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Application bootstrap |
22
|
|
|
*/ |
23
|
|
|
class Bootstrap |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Application configuration cache key |
27
|
|
|
*/ |
28
|
|
|
const CACHE_KEY = 'config'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Application |
32
|
|
|
*/ |
33
|
|
|
private $app; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $cacheKey; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param object|Application $app |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Application $app) |
44
|
|
|
{ |
45
|
|
|
$this->app = $app; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
private function getCacheKey() |
52
|
|
|
{ |
53
|
|
|
return $this::CACHE_KEY . '_' . $this->cacheKey; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $key |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setCacheKey($key) |
61
|
|
|
{ |
62
|
|
|
$this->cacheKey .= md5($key); |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Configures the application |
68
|
|
|
* |
69
|
|
|
* Returns cached early if any. |
70
|
|
|
* |
71
|
|
|
* @triggers configure |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function configure() |
75
|
|
|
{ |
76
|
|
|
$config = $this->app->getConfig(); |
77
|
|
|
$useCache = $this->app->getConfig(ConfigCacheEnabled::KEY); |
78
|
|
|
|
79
|
|
|
if ($useCache) { |
80
|
|
|
$cached = $this->app->getCache($this->getCacheKey()); |
81
|
|
|
if ($cached) { |
82
|
|
|
$this->app->setConfig($cached); |
83
|
|
|
$this->configureServices(); |
84
|
|
|
$this->app->log(Log\LoadCachedAppConfig::class); |
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->app->emit(AppEvent::CONFIGURE, [], [$this->app, 'mergeConfig']); |
90
|
|
|
$this->configureServices(); |
91
|
|
|
$this->app->log(Log\ConfigureApp::class); |
92
|
|
|
|
93
|
|
|
$useCache and $this->app->setCache($this->getCacheKey(), $config); |
|
|
|
|
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Configure application services |
100
|
|
|
* |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
|
|
protected function configureServices() |
104
|
|
|
{ |
105
|
|
|
$services = $this->app->getConfig(Config::SERVICES); |
106
|
|
|
if (null === $services) { |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$config = $services->toArray(); |
111
|
|
|
(new ServicesConfig($config))->configureServiceManager($this->app->getServices()); |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Attach core listeners |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function attachCoreListeners() |
121
|
|
|
{ |
122
|
|
|
$this->eachCoreListener([$this, 'attachListener']); |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Detach core listeners |
128
|
|
|
* |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
|
|
public function detachCoreListeners() |
132
|
|
|
{ |
133
|
|
|
$this->eachCoreListener([$this, 'detachListener']); |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Attach application listeners |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function attachListeners() |
143
|
|
|
{ |
144
|
|
|
$this->eachListener([$this, 'attachListener']); |
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param $listener |
150
|
|
|
*/ |
151
|
|
|
protected function attachListener($listener) |
152
|
|
|
{ |
153
|
|
|
$service = $this->app->get($listener); |
154
|
|
|
$service and $this->app->bind($service); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $listener |
159
|
|
|
*/ |
160
|
|
|
protected function detachListener($listener) |
161
|
|
|
{ |
162
|
|
|
$service = $this->app->get($listener); |
163
|
|
|
$service and $this->app->unbind($service); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param callable $callback |
168
|
|
|
*/ |
169
|
|
View Code Duplication |
protected function eachCoreListener(callable $callback) |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
$listeners = $this->app->getCoreConfig(Config::LISTENERS); |
172
|
|
|
if (empty($listeners)) { |
173
|
|
|
return; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
foreach ($listeners as $listener) { |
177
|
|
|
call_user_func($callback, $listener); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param callable $callback |
183
|
|
|
*/ |
184
|
|
View Code Duplication |
protected function eachListener(callable $callback) |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
$listeners = $this->app->getConfig(Config::LISTENERS); |
187
|
|
|
if (empty($listeners)) { |
188
|
|
|
return; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
foreach ($listeners as $listener) { |
192
|
|
|
call_user_func($callback, $listener); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.