1
|
|
|
<?php |
|
|
|
|
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* Copyright (C) 2015 Alexander Schmidt |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
* @author Alexander Schmidt <[email protected]> |
19
|
|
|
* @copyright Copyright (c) 2016, Alexander Schmidt |
20
|
|
|
* @date 07.02.16 |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Bonefish\Bootstrap; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
use AValnar\Doctrine\Factory\AnnotationReaderFactory; |
27
|
|
|
use AValnar\EventDispatcher\EventDispatcher; |
28
|
|
|
use AValnar\EventDispatcher\EventSubscriber; |
29
|
|
|
use Bonefish\Bootstrap\Event\ObjectEvent; |
30
|
|
|
use Bonefish\Events; |
31
|
|
|
use Bonefish\Injection\Container\Container; |
32
|
|
|
use Bonefish\Injection\Resolver\ClassNameResolver; |
33
|
|
|
use Bonefish\Injection\Resolver\InterfaceResolver; |
34
|
|
|
use Bonefish\Reflection\ReflectionService; |
35
|
|
|
use Bonefish\Utility\Configuration\NeonConfiguration; |
36
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
37
|
|
|
use Doctrine\Common\Cache\Cache; |
38
|
|
|
use Tracy\Debugger; |
39
|
|
|
|
40
|
|
|
final class BootSubscriber implements EventSubscriber |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var EventDispatcher |
44
|
|
|
*/ |
45
|
|
|
private $eventDispatcher; |
46
|
|
|
|
47
|
|
|
public function __construct(EventDispatcher $eventDispatcher) |
48
|
|
|
{ |
49
|
|
|
$this->eventDispatcher = $eventDispatcher; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function getEvents() : array |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
'initTracy' => [[Events::BOOT_INIT]], |
59
|
|
|
'initCache' => [[Events::BOOT_INIT]], |
60
|
|
|
'initAnnotationReader' => [[Events::CACHE_INIT]], |
61
|
|
|
'initReflectionService' => [[Events::CACHE_INIT, Events::ANNOTATION_READER_INIT]], |
62
|
|
|
'initContainer' => [[Events::REFLECTION_SERVICE_INIT]], |
63
|
|
|
'configureContainer' => [[Events::CONTAINER_INIT]], |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function initTracy() |
68
|
|
|
{ |
69
|
|
|
Debugger::enable(!BONEFISH_DEV_MODE, BONEFISH_LOG_PATH, BONEFISH_ADMIN_MAIL); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function initCache() |
73
|
|
|
{ |
74
|
|
|
$cacheType = BONEFISH_CACHE_TYPE; |
75
|
|
|
/** @var Cache $cache */ |
76
|
|
|
$cache = new $cacheType(); |
77
|
|
|
|
78
|
|
|
$this->emitObject(Events::CACHE_INIT, $cache); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array $events |
83
|
|
|
*/ |
84
|
|
|
public function initAnnotationReader(array $events) |
85
|
|
|
{ |
86
|
|
|
/** @var Cache $cache */ |
87
|
|
|
$cache = $this->getObject($events, Events::CACHE_INIT); |
88
|
|
|
$factory = new AnnotationReaderFactory(); |
89
|
|
|
$annotationReader = $factory->create([ |
90
|
|
|
'cache' => $cache, |
91
|
|
|
'debug' => BONEFISH_DEV_MODE, |
92
|
|
|
'indexed' => false |
93
|
|
|
]); |
94
|
|
|
|
95
|
|
|
$this->emitObject(Events::ANNOTATION_READER_INIT, $annotationReader); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $events |
100
|
|
|
*/ |
101
|
|
|
public function initReflectionService(array $events) |
102
|
|
|
{ |
103
|
|
|
/** @var Cache $cache */ |
104
|
|
|
$cache = $this->getObject($events, Events::CACHE_INIT); |
105
|
|
|
/** @var AnnotationReader $annotationReader */ |
106
|
|
|
$annotationReader = $this->getObject($events, Events::ANNOTATION_READER_INIT); |
107
|
|
|
$reflectionService = new ReflectionService($cache, $annotationReader); |
108
|
|
|
|
109
|
|
|
$this->emitObject(Events::REFLECTION_SERVICE_INIT, $reflectionService); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $events |
114
|
|
|
*/ |
115
|
|
|
public function initContainer(array $events) |
116
|
|
|
{ |
117
|
|
|
/** @var ReflectionService $reflectionService */ |
118
|
|
|
$reflectionService = $this->getObject($events, Events::REFLECTION_SERVICE_INIT); |
119
|
|
|
|
120
|
|
|
$container = new Container($reflectionService); |
121
|
|
|
|
122
|
|
|
$this->emitObject(Events::CONTAINER_INIT, $container); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param array $events |
127
|
|
|
*/ |
128
|
|
|
public function configureContainer(array $events) |
129
|
|
|
{ |
130
|
|
|
/** @var Container $container */ |
131
|
|
|
$container = $this->getObject($events, Events::CONTAINER_INIT); |
132
|
|
|
|
133
|
|
|
$configuration = $container->create(NeonConfiguration::class) |
134
|
|
|
->getConfiguration(BONEFISH_CONFIG_PATH . '/interfaces.neon'); |
135
|
|
|
|
136
|
|
|
$interfaceResolver = new InterfaceResolver(); |
137
|
|
|
foreach ($configuration as $interface => $implementation) { |
138
|
|
|
$interfaceResolver->addImplementation($interface, $implementation); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$container->addResolver($interfaceResolver); |
142
|
|
|
$container->addResolver($container->get(ClassNameResolver::class), 10); |
143
|
|
|
$container->add($this->eventDispatcher); |
144
|
|
|
|
145
|
|
|
$this->emitObject(Events::CONTAINER_SETUP, $container); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** Helper methods */ |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $eventName |
152
|
|
|
* @param $object |
153
|
|
|
*/ |
154
|
|
|
private function emitObject(string $eventName, $object) |
155
|
|
|
{ |
156
|
|
|
$event = new ObjectEvent($object); |
157
|
|
|
$this->eventDispatcher->dispatch($eventName, $event); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $events |
162
|
|
|
* @param string $eventName |
163
|
|
|
* @return object |
164
|
|
|
*/ |
165
|
|
|
private function getObject(array $events, string $eventName) |
166
|
|
|
{ |
167
|
|
|
return $events[$eventName][0]->getObject(); |
168
|
|
|
} |
169
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.