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\Yves\Application\Plugin\Provider; |
||
9 | |||
10 | use Silex\Application; |
||
11 | use Silex\Controller; |
||
12 | use Spryker\Shared\Application\ApplicationConstants; |
||
13 | use Spryker\Shared\Application\Communication\ControllerServiceBuilder; |
||
14 | use Spryker\Shared\Config\Config; |
||
15 | use Spryker\Yves\Kernel\BundleControllerAction; |
||
16 | use Spryker\Yves\Kernel\ClassResolver\Controller\ControllerResolver; |
||
17 | use Spryker\Yves\Kernel\Controller\BundleControllerActionRouteNameResolver; |
||
18 | use Symfony\Component\HttpFoundation\RedirectResponse; |
||
19 | use Symfony\Component\HttpFoundation\Request; |
||
20 | |||
21 | /** |
||
22 | * @deprecated Use {@link \Spryker\Yves\Router\Plugin\RouteProvider\AbstractRouteProviderPlugin} instead. |
||
23 | */ |
||
24 | abstract class YvesControllerProvider implements ControllerProviderInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var \Silex\ControllerCollection |
||
28 | */ |
||
29 | protected $controllerCollection; |
||
30 | |||
31 | /** |
||
32 | * @var \Silex\Application |
||
33 | */ |
||
34 | protected $app; |
||
35 | |||
36 | /** |
||
37 | * @var bool |
||
38 | */ |
||
39 | protected $sslEnabled; |
||
40 | |||
41 | /** |
||
42 | * Set the sslEnabledFlag to |
||
43 | * true to force ssl |
||
44 | * false to force http |
||
45 | * null to not force anything (both https or http allowed) |
||
46 | * |
||
47 | * @param bool|null $sslEnabled |
||
48 | */ |
||
49 | public function __construct($sslEnabled = null) |
||
50 | { |
||
51 | $this->sslEnabled = $sslEnabled; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param \Silex\Application $app |
||
56 | * |
||
57 | * @return \Silex\ControllerCollection |
||
58 | */ |
||
59 | public function connect(Application $app) |
||
60 | { |
||
61 | $this->app = $app; |
||
62 | $this->controllerCollection = $app['controllers_factory']; |
||
63 | $this->defineControllers($app); |
||
64 | |||
65 | return $this->controllerCollection; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return string |
||
70 | */ |
||
71 | public function getUrlPrefix() |
||
72 | { |
||
73 | return ''; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return bool |
||
78 | */ |
||
79 | protected function isSslEnabled() |
||
80 | { |
||
81 | return $this->sslEnabled; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param string $path |
||
86 | * @param string $name |
||
87 | * @param string $bundle |
||
88 | * @param string $controllerName |
||
89 | * @param string $actionName |
||
90 | * @param bool $parseJsonBody |
||
91 | * |
||
92 | * @return \Silex\Controller |
||
93 | */ |
||
94 | protected function createController( |
||
95 | $path, |
||
96 | $name, |
||
97 | $bundle, |
||
98 | $controllerName, |
||
99 | $actionName = 'index', |
||
100 | $parseJsonBody = false |
||
101 | ) { |
||
102 | $service = $this->getService($bundle, $controllerName, $actionName); |
||
103 | $controller = $this->getController($path, $name, $service); |
||
104 | if ($this->sslEnabled === true && !$this->isSslExcluded($name)) { |
||
105 | $controller->requireHttps(); |
||
106 | } elseif ($this->sslEnabled === false) { |
||
107 | $controller->requireHttp(); |
||
108 | } |
||
109 | if ($parseJsonBody) { |
||
110 | $this->addJsonParsing($controller); |
||
111 | } |
||
112 | |||
113 | return $controller; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param string $path |
||
118 | * @param string $name |
||
119 | * @param string $redirectPath |
||
120 | * @param int $status |
||
121 | * |
||
122 | * @return \Silex\Controller |
||
123 | */ |
||
124 | protected function createRedirectController($path, $name, $redirectPath, $status = 302) |
||
125 | { |
||
126 | $controller = $this->controllerCollection |
||
127 | ->match($path, function () use ($redirectPath, $status) { |
||
128 | return new RedirectResponse($redirectPath, $status); |
||
129 | }) |
||
130 | ->bind($name); |
||
131 | |||
132 | return $controller; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param string $path |
||
137 | * @param string $name |
||
138 | * @param string $bundle |
||
139 | * @param string $controllerName |
||
140 | * @param string $actionName |
||
141 | * |
||
142 | * @return \Silex\Controller |
||
143 | */ |
||
144 | protected function createGetController($path, $name, $bundle, $controllerName, $actionName = 'index') |
||
145 | { |
||
146 | return $this->createController($path, $name, $bundle, $controllerName, $actionName) |
||
147 | ->method('GET'); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param string $path |
||
152 | * @param string $name |
||
153 | * @param string $bundle |
||
154 | * @param string $controllerName |
||
155 | * @param string $actionName |
||
156 | * @param bool $parseJsonBody |
||
157 | * |
||
158 | * @return \Silex\Controller |
||
159 | */ |
||
160 | protected function createPostController($path, $name, $bundle, $controllerName, $actionName = 'index', $parseJsonBody = false) |
||
161 | { |
||
162 | return $this->createController($path, $name, $bundle, $controllerName, $actionName, $parseJsonBody) |
||
163 | ->method('POST'); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param \Silex\Application $app |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | abstract protected function defineControllers(Application $app); |
||
172 | |||
173 | /** |
||
174 | * @param \Silex\Controller $controller |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | private function addJsonParsing(Controller $controller) |
||
179 | { |
||
180 | $controller->before(function (Request $request) { |
||
181 | $isJson = (strpos($request->headers->get('Content-Type'), 'application/json') === 0); |
||
182 | if ($isJson) { |
||
183 | /** @var string $json */ |
||
184 | $json = $request->getContent(); |
||
185 | $data = json_decode($json, true); |
||
186 | $request->request->replace(is_array($data) ? $data : []); |
||
187 | } |
||
188 | }); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param string $routeName |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | protected function isSslExcluded($routeName) |
||
197 | { |
||
198 | $excludedUrls = $this->getExcludedUrls(); |
||
199 | |||
200 | return !empty($excludedUrls[$routeName]); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return array |
||
205 | */ |
||
206 | protected function getExcludedUrls() |
||
207 | { |
||
208 | return Config::get(ApplicationConstants::YVES_SSL_EXCLUDED, []); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param string $bundle |
||
213 | * @param string $controllerName |
||
214 | * @param string $actionName |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | protected function getService($bundle, $controllerName, $actionName) |
||
219 | { |
||
220 | $bundleControllerAction = new BundleControllerAction($bundle, $controllerName, $actionName); |
||
221 | $controllerResolver = new ControllerResolver(); |
||
222 | $routeResolver = new BundleControllerActionRouteNameResolver($bundleControllerAction); |
||
223 | |||
224 | $service = (new ControllerServiceBuilder())->createServiceForController($this->app, $bundleControllerAction, $controllerResolver, $routeResolver); |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
225 | |||
226 | return $service; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @param string $path |
||
231 | * @param string $name |
||
232 | * @param string $service |
||
233 | * |
||
234 | * @return \Silex\Controller |
||
235 | */ |
||
236 | protected function getController($path, $name, $service) |
||
237 | { |
||
238 | $controller = $this->controllerCollection |
||
239 | ->match($path, $service) |
||
240 | ->bind($name); |
||
241 | |||
242 | return $controller; |
||
243 | } |
||
244 | } |
||
245 |