|
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; |
|
12
|
|
|
|
|
13
|
|
|
use WebinoAppLib\Event\AppEvent; |
|
14
|
|
|
use WebinoAppLib\Exception\DomainException; |
|
15
|
|
|
use WebinoAppLib\Exception\InvalidArgumentException; |
|
16
|
|
|
use WebinoAppLib\Service\Bootstrap; |
|
17
|
|
|
use Zend\Config\Config; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class AbstractBaseApplication |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AbstractBaseApplication extends AbstractApplication |
|
|
|
|
|
|
23
|
|
|
implements BaseApplicationInterface |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritDoc} |
|
27
|
|
|
*/ |
|
28
|
|
|
public function bootstrap() |
|
29
|
|
|
{ |
|
30
|
|
|
if ($this->getConfig()->isReadOnly()) { |
|
31
|
|
|
throw new DomainException('Application is already bootstrapped'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $this->internalBootstrap( |
|
35
|
|
|
$this->getServices()->get($this::BOOTSTRAP), |
|
|
|
|
|
|
36
|
|
|
function () { |
|
37
|
|
|
$this->emit(AppEvent::BOOTSTRAP, $this); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param object|Bootstrap $bootstrap |
|
44
|
|
|
* @param callable $trigger |
|
45
|
|
|
* @return AbstractConfiguredApplication |
|
46
|
|
|
* @triggers bootstrap |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function internalBootstrap(Bootstrap $bootstrap, callable $trigger) |
|
49
|
|
|
{ |
|
50
|
|
|
// lock core config |
|
51
|
|
|
$this->getConfig()->get(CoreConfig::CORE)->setReadOnly(); |
|
52
|
|
|
|
|
53
|
|
|
$bootstrap->attachCoreListeners(); |
|
54
|
|
|
$listeners = $this->listeners; |
|
55
|
|
|
|
|
56
|
|
|
// trigger bootstrap |
|
57
|
|
|
call_user_func($trigger); |
|
58
|
|
|
|
|
59
|
|
|
// configure application |
|
60
|
|
|
$bootstrap->configure(); |
|
61
|
|
|
|
|
62
|
|
|
$detachedListeners = $this->detachListeners($listeners); |
|
63
|
|
|
$bootstrap->attachListeners()->detachCoreListeners(); |
|
64
|
|
|
|
|
65
|
|
|
// trigger bootstrap again |
|
66
|
|
|
call_user_func($trigger); |
|
67
|
|
|
|
|
68
|
|
|
$bootstrap->attachCoreListeners(); |
|
69
|
|
|
$this->attachListeners($detachedListeners); |
|
70
|
|
|
|
|
71
|
|
|
// lock config |
|
72
|
|
|
$this->getConfig()->setReadOnly(); |
|
73
|
|
|
|
|
74
|
|
|
// return configured app |
|
75
|
|
|
return new ConfiguredApplication($this->getServices()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Detach listeners from application events |
|
80
|
|
|
* |
|
81
|
|
|
* @param \Zend\EventManager\ListenerAggregateInterface[] $listeners |
|
82
|
|
|
* @return \Zend\EventManager\ListenerAggregateInterface[] Detached listeners |
|
83
|
|
|
*/ |
|
84
|
|
|
private function detachListeners(array $listeners) |
|
85
|
|
|
{ |
|
86
|
|
|
$detachedListeners = []; |
|
87
|
|
|
|
|
88
|
|
|
foreach ($listeners as $listener) { |
|
89
|
|
|
$detachedListeners[] = clone $listener; |
|
90
|
|
|
$this->unbind($listener); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $detachedListeners; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Attach event listeners |
|
98
|
|
|
* |
|
99
|
|
|
* @param \Zend\EventManager\ListenerAggregateInterface[] $listeners |
|
100
|
|
|
*/ |
|
101
|
|
|
private function attachListeners(array $listeners) |
|
102
|
|
|
{ |
|
103
|
|
|
foreach ($listeners as $listener) { |
|
104
|
|
|
$this->bind($listener); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Merge config into application config. |
|
110
|
|
|
* |
|
111
|
|
|
* @param array|Config|null $config |
|
112
|
|
|
* @return $this |
|
113
|
|
|
* @throws InvalidArgumentException |
|
114
|
|
|
*/ |
|
115
|
|
|
public function mergeConfig($config = null) |
|
116
|
|
|
{ |
|
117
|
|
|
if (empty($config)) { |
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (is_object($config) && method_exists($config, 'toArray')) { |
|
122
|
|
|
$config = $config->toArray(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if (is_array($config)) { |
|
126
|
|
|
$config = new Config($config); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (!($config instanceof Config)) { |
|
130
|
|
|
throw new InvalidArgumentException('Expected array|Config|null'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$this->getConfig()->merge($config); |
|
134
|
|
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: