1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Zed\Application\Communication; |
||
9 | |||
10 | use Spryker\Service\Container\ContainerInterface; |
||
11 | use Spryker\Shared\Application\Application as SprykerApplication; |
||
12 | use Spryker\Shared\Application\ApplicationConstants; |
||
13 | use Spryker\Shared\Config\Config; |
||
14 | use Spryker\Shared\Kernel\Communication\Application; |
||
15 | use Spryker\Shared\Kernel\Store; |
||
16 | use Spryker\Zed\Application\ApplicationConfig; |
||
17 | use Spryker\Zed\Application\ApplicationDependencyProvider; |
||
18 | use Spryker\Zed\Kernel\AbstractBundleDependencyProvider; |
||
19 | use Spryker\Zed\Kernel\BundleDependencyProviderResolverAwareTrait; |
||
20 | use Spryker\Zed\Kernel\Communication\Plugin\Pimple; |
||
21 | use Spryker\Zed\Kernel\Container; |
||
22 | use Spryker\Zed\Kernel\ControllerResolver\ZedFragmentControllerResolver; |
||
23 | use Spryker\Zed\Kernel\Dependency\Injector\DependencyInjector; |
||
24 | use Symfony\Component\HttpFoundation\Request; |
||
25 | |||
26 | class ZedBootstrap |
||
27 | { |
||
28 | use BundleDependencyProviderResolverAwareTrait; |
||
29 | |||
30 | /** |
||
31 | * @var \Spryker\Shared\Kernel\Communication\Application |
||
32 | */ |
||
33 | protected $application; |
||
34 | |||
35 | /** |
||
36 | * @var \Spryker\Shared\Application\Application|null |
||
37 | */ |
||
38 | protected $sprykerApplication; |
||
39 | |||
40 | /** |
||
41 | * @var \Spryker\Zed\Application\ApplicationConfig |
||
42 | */ |
||
43 | protected $config; |
||
44 | |||
45 | public function __construct() |
||
46 | { |
||
47 | $this->application = $this->getBaseApplication(); |
||
48 | |||
49 | if ($this->application instanceof ContainerInterface) { |
||
50 | $this->sprykerApplication = new SprykerApplication($this->application); |
||
51 | } |
||
52 | |||
53 | $this->config = new ApplicationConfig(); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @return \Spryker\Shared\Application\Application|\Spryker\Shared\Kernel\Communication\Application |
||
58 | */ |
||
59 | public function boot() |
||
60 | { |
||
61 | $this->application['debug'] = function () { |
||
62 | return Config::get(ApplicationConstants::ENABLE_APPLICATION_DEBUG, false); |
||
63 | }; |
||
64 | |||
65 | $this->application['locale'] = Store::getInstance()->getCurrentLocale(); |
||
66 | |||
67 | $this->enableHttpMethodParameterOverride(); |
||
68 | $this->setUp(); |
||
69 | |||
70 | $this->application->boot(); |
||
71 | |||
72 | if ($this->sprykerApplication === null) { |
||
73 | return $this->application; |
||
74 | } |
||
75 | |||
76 | $this->sprykerApplication->boot(); |
||
77 | |||
78 | return $this->sprykerApplication; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return void |
||
83 | */ |
||
84 | protected function setUp() |
||
85 | { |
||
86 | $this->optimizeApp(); |
||
87 | // For BC |
||
88 | if ($this->isInternalRequest()) { |
||
89 | $this->registerServiceProviderForInternalRequestWithAuthentication(); |
||
90 | $this->setupApplication(); |
||
91 | |||
92 | return; |
||
93 | } |
||
94 | |||
95 | $this->registerServiceProvider(); |
||
96 | |||
97 | $this->setupApplication(); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return void |
||
102 | */ |
||
103 | protected function setupApplicationPlugins(): void |
||
104 | { |
||
105 | foreach ($this->getApplicationPlugins() as $applicationPlugin) { |
||
106 | $this->sprykerApplication->registerApplicationPlugin($applicationPlugin); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return void |
||
112 | */ |
||
113 | protected function setupApplication(): void |
||
114 | { |
||
115 | if ($this->sprykerApplication !== null) { |
||
116 | $this->setupApplicationPlugins(); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @return array<\Spryker\Shared\ApplicationExtension\Dependency\Plugin\ApplicationPluginInterface> |
||
122 | */ |
||
123 | protected function getApplicationPlugins(): array |
||
124 | { |
||
125 | return $this->getProvidedDependency(ApplicationDependencyProvider::PLUGINS_APPLICATION); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @return void |
||
130 | */ |
||
131 | protected function registerServiceProvider() |
||
132 | { |
||
133 | foreach ($this->getServiceProvider() as $provider) { |
||
134 | $this->application->register($provider); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @return void |
||
140 | */ |
||
141 | protected function registerServiceProviderForInternalRequestWithAuthentication() |
||
142 | { |
||
143 | $serviceProviders = $this->getMergedServiceProviders(); |
||
144 | |||
145 | foreach ($serviceProviders as $provider) { |
||
146 | $this->application->register($provider); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @return array<\Silex\ServiceProviderInterface> |
||
152 | */ |
||
153 | protected function getMergedServiceProviders(): array |
||
154 | { |
||
155 | $serviceProviders = $this->getServiceProvider(); |
||
156 | $internalCallServiceProvidersWithAuth = $this->getInternalCallServiceProviderWithAuthentication(); |
||
157 | $internalCallServiceProviders = $this->getInternalCallServiceProvider(); |
||
158 | $mergedServiceProviders = array_merge($serviceProviders, $internalCallServiceProvidersWithAuth, $internalCallServiceProviders); |
||
159 | |||
160 | return array_unique($mergedServiceProviders, SORT_REGULAR); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @return array<\Silex\ServiceProviderInterface> |
||
165 | */ |
||
166 | protected function getServiceProvider() |
||
167 | { |
||
168 | return $this->getProvidedDependency(ApplicationDependencyProvider::SERVICE_PROVIDER); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @return array<\Silex\ServiceProviderInterface> |
||
173 | */ |
||
174 | protected function getInternalCallServiceProvider() |
||
175 | { |
||
176 | return $this->getProvidedDependency(ApplicationDependencyProvider::INTERNAL_CALL_SERVICE_PROVIDER); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return array<\Silex\ServiceProviderInterface> |
||
181 | */ |
||
182 | protected function getInternalCallServiceProviderWithAuthentication() |
||
183 | { |
||
184 | return $this->getProvidedDependency(ApplicationDependencyProvider::INTERNAL_CALL_SERVICE_PROVIDER_WITH_AUTHENTICATION); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return void |
||
189 | */ |
||
190 | protected function registerApiServiceProvider() |
||
191 | { |
||
192 | foreach ($this->getApiServiceProvider() as $provider) { |
||
193 | $this->application->register($provider); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @return array<\Silex\ServiceProviderInterface> |
||
199 | */ |
||
200 | protected function getApiServiceProvider() |
||
201 | { |
||
202 | return $this->getProvidedDependency(ApplicationDependencyProvider::SERVICE_PROVIDER_API); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @return \Spryker\Shared\Kernel\Communication\Application |
||
207 | */ |
||
208 | protected function getBaseApplication() |
||
209 | { |
||
210 | $application = new Application(); |
||
211 | |||
212 | $this->unsetSilexExceptionHandler($application); |
||
213 | |||
214 | Pimple::setApplication($application); |
||
215 | |||
216 | return $application; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @param \Spryker\Shared\Kernel\Communication\Application $application |
||
221 | * |
||
222 | * @return void |
||
223 | */ |
||
224 | private function unsetSilexExceptionHandler(Application $application) |
||
225 | { |
||
226 | unset($application['exception_handler']); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @return void |
||
231 | */ |
||
232 | protected function optimizeApp() |
||
233 | { |
||
234 | $application = $this->application; |
||
235 | $application['resolver'] = function () use ($application) { |
||
236 | return new ZedFragmentControllerResolver($application); |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
237 | }; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Allow overriding http method. Needed to use the "_method" parameter in forms. |
||
242 | * This should not be changeable by projects |
||
243 | * |
||
244 | * @return void |
||
245 | */ |
||
246 | private function enableHttpMethodParameterOverride() |
||
247 | { |
||
248 | Request::enableHttpMethodParameterOverride(); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param \Spryker\Zed\Kernel\AbstractBundleDependencyProvider $dependencyProvider |
||
253 | * @param \Spryker\Zed\Kernel\Container $container |
||
254 | * |
||
255 | * @return \Spryker\Zed\Kernel\Container |
||
256 | */ |
||
257 | protected function provideExternalDependencies(AbstractBundleDependencyProvider $dependencyProvider, Container $container) |
||
258 | { |
||
259 | $dependencyProvider->provideBusinessLayerDependencies($container); |
||
260 | $dependencyProvider->provideCommunicationLayerDependencies($container); |
||
261 | $dependencyProvider->providePersistenceLayerDependencies($container); |
||
262 | |||
263 | return $container; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @param \Spryker\Zed\Kernel\Dependency\Injector\DependencyInjector $dependencyInjector |
||
268 | * @param \Spryker\Zed\Kernel\Container $container |
||
269 | * |
||
270 | * @return \Spryker\Shared\Kernel\ContainerInterface|\Spryker\Zed\Kernel\Container |
||
271 | */ |
||
272 | protected function injectExternalDependencies(DependencyInjector $dependencyInjector, Container $container) |
||
273 | { |
||
274 | $container = $dependencyInjector->injectCommunicationLayerDependencies($container); |
||
275 | |||
276 | return $container; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @return bool |
||
281 | */ |
||
282 | protected function isInternalRequest() |
||
283 | { |
||
284 | return array_key_exists('HTTP_X_INTERNAL_REQUEST', $_SERVER); |
||
285 | } |
||
286 | } |
||
287 |