|
1
|
|
|
<?php |
|
2
|
|
|
namespace Drupal\df_tools_services\EventSubscriber; |
|
3
|
|
|
|
|
4
|
|
|
use Drupal\Core\Routing\RouteBuildEvent; |
|
5
|
|
|
use Drupal\Core\Routing\RoutingEvents; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
7
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
8
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
|
9
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
10
|
|
|
|
|
11
|
|
|
class DFToolsServicesSubscriber implements EventSubscriberInterface { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Forces CORS enablement for all responses. |
|
15
|
|
|
* |
|
16
|
|
|
* @param FilterResponseEvent $event |
|
17
|
|
|
*/ |
|
18
|
|
|
public function allowCORSResponse(FilterResponseEvent $event) { |
|
19
|
|
|
$response = $event->getResponse(); |
|
20
|
|
|
$request = $event->getRequest(); |
|
21
|
|
|
|
|
22
|
|
|
// Forcibly allow CORS for the purposes of the headless demo. |
|
23
|
|
|
if ($request->getMethod() == 'OPTIONS') { |
|
24
|
|
|
$response = new Response(); |
|
25
|
|
|
$this->setCORSHeaders($response); |
|
26
|
|
|
$response->send(); |
|
27
|
|
|
exit; |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if ($response) { |
|
|
|
|
|
|
31
|
|
|
$this->setCORSHeaders($response); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Allows OPTIONS as an acceptable method for all Drupal routes. |
|
37
|
|
|
* |
|
38
|
|
|
* @param RouteBuildEvent $event |
|
39
|
|
|
*/ |
|
40
|
|
|
public function allowOPTIONSRoute(RouteBuildEvent $event) { |
|
41
|
|
|
$collection = $event->getRouteCollection(); |
|
42
|
|
|
$routes = $collection->all(); |
|
43
|
|
|
foreach ($routes as $name => $route) { |
|
44
|
|
|
$methods = $route->getMethods(); |
|
45
|
|
|
if (!in_array('OPTIONS', $methods)) { |
|
46
|
|
|
$methods[] = 'OPTIONS'; |
|
47
|
|
|
$route->setMethods($methods); |
|
48
|
|
|
$collection->add($name, $route); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Sets headers required to enable CORS. |
|
55
|
|
|
* |
|
56
|
|
|
* @param Response $response |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function setCORSHeaders($response) { |
|
59
|
|
|
$response->headers->set('Access-Control-Allow-Origin', '*'); |
|
60
|
|
|
$response->headers->set('Access-Control-Allow-Methods', 'GET,POST,PUT,PATCH,DELETE,OPTIONS'); |
|
61
|
|
|
$response->headers->set('Access-Control-Allow-Headers', 'Authorization, Origin, Accept, Content-Type, X-CSRF-Token'); |
|
62
|
|
|
$response->headers->set('Access-Control-Allow-Credentials', 'true'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function getSubscribedEvents() { |
|
69
|
|
|
$events = []; |
|
70
|
|
|
$events[KernelEvents::RESPONSE][] = ['allowCORSResponse']; |
|
71
|
|
|
$events[RoutingEvents::ALTER][] = ['allowOPTIONSRoute']; |
|
72
|
|
|
return $events; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.