|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dafiti\Silex\Listener; |
|
4
|
|
|
|
|
5
|
|
|
use Dafiti\Silex\Response\Accept; |
|
6
|
|
|
use Dafiti\Silex\Response\Factory as ResponseFactory; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
10
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
11
|
|
|
|
|
12
|
|
|
class Header implements EventSubscriberInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Accept |
|
16
|
|
|
*/ |
|
17
|
|
|
private $accept; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param Accept $accept |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(Accept $accept) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->accept = $accept; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param GetResponseEvent $event |
|
29
|
|
|
* |
|
30
|
|
|
* @return GetResponseEvent |
|
31
|
|
|
*/ |
|
32
|
|
|
public function onKernelRequest(GetResponseEvent $event) |
|
33
|
|
|
{ |
|
34
|
|
|
if (!$event->getRequest()->headers->get('Accept')) { |
|
35
|
|
|
$this->setNotAcceptableError($event); |
|
36
|
|
|
|
|
37
|
|
|
return $event; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$raw_accepts = explode(',', $event->getRequest()->headers->get('Accept')); |
|
41
|
|
|
$accepts = array_map('trim', $raw_accepts); |
|
42
|
|
|
|
|
43
|
|
|
if (!$this->accept->hasAllowed($accepts)) { |
|
44
|
|
|
$this->setNotAcceptableError($event); |
|
45
|
|
|
|
|
46
|
|
|
return $event; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$accept = $this->accept->getBest($accepts); |
|
50
|
|
|
$event->getRequest()->request->set('_accept', $accept); |
|
51
|
|
|
|
|
52
|
|
|
return $event; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function setNotAcceptableError(GetResponseEvent $event) |
|
56
|
|
|
{ |
|
57
|
|
|
$responseFactory = new ResponseFactory($this->accept->getDefault()); |
|
58
|
|
|
$catalogResponse = new \Dafiti\Silex\Response(HttpFoundation\Response::HTTP_NOT_ACCEPTABLE); |
|
59
|
|
|
$response = $responseFactory->create($catalogResponse); |
|
60
|
|
|
$event->setResponse($response); |
|
61
|
|
|
$event->stopPropagation(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function getSubscribedEvents() |
|
68
|
|
|
{ |
|
69
|
|
|
return [ |
|
70
|
|
|
KernelEvents::REQUEST => ['onKernelRequest', 100], |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array $config |
|
76
|
|
|
* |
|
77
|
|
|
* @return ContentNegotiationHeader |
|
78
|
|
|
* |
|
79
|
|
|
* @throws \InvalidArgumentException |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function create(array $config) |
|
82
|
|
|
{ |
|
83
|
|
|
$availableAccepts = $config['available_accepts']; |
|
84
|
|
|
|
|
85
|
|
|
if (is_null($availableAccepts)) { |
|
86
|
|
|
throw new \InvalidArgumentException('available_accepts'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$defaultAccept = $config['default_accept']; |
|
90
|
|
|
if (is_null($defaultAccept)) { |
|
91
|
|
|
throw new \InvalidArgumentException('default_accept'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$accept = new Accept($defaultAccept, $availableAccepts); |
|
95
|
|
|
|
|
96
|
|
|
return new self($accept); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|