1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JDesrosiers\Silex\Provider; |
4
|
|
|
|
5
|
|
|
use JDesrosiers\Silex\Provider\ContentNegotiation\JmsSerializerContentNegotiation; |
6
|
|
|
use JDesrosiers\Silex\Provider\ContentNegotiation\SymfonySerializerContentNegotiation; |
7
|
|
|
use JMS\Serializer as JMS; |
8
|
|
|
use Pimple\Container; |
9
|
|
|
use Pimple\ServiceProviderInterface; |
10
|
|
|
use Silex\Application; |
11
|
|
|
use Silex\Api\BootableProviderInterface; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException; |
14
|
|
|
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; |
15
|
|
|
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException; |
16
|
|
|
use Symfony\Component\Serializer as SymfonySerializer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This ServiceProvider provides HTTP Content Negotiation support to a silex |
20
|
|
|
* application. |
21
|
|
|
*/ |
22
|
|
|
class ContentNegotiationServiceProvider implements ServiceProviderInterface, BootableProviderInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Add filters to check that the request and repsonse formats are supported |
26
|
|
|
* by the application. |
27
|
|
|
* |
28
|
|
|
* @param Application $app |
29
|
|
|
*/ |
30
|
20 |
|
public function boot(Application $app) |
31
|
|
|
{ |
32
|
20 |
|
$app->before(array($this, "setRequestFormat"), Application::EARLY_EVENT); |
33
|
20 |
|
$app->before(array($this, "validateRequestContentType"), Application::EARLY_EVENT); |
34
|
20 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set defaults and build the conneg service. |
38
|
|
|
* |
39
|
|
|
* @param Container $app |
40
|
|
|
* @throws ServiceUnavailableHttpException |
41
|
|
|
*/ |
42
|
20 |
|
public function register(Container $app) |
43
|
|
|
{ |
44
|
20 |
|
$app["conneg.responseFormats"] = array("html"); |
45
|
20 |
|
$app["conneg.requestFormats"] = array("form"); |
46
|
20 |
|
$app["conneg.defaultFormat"] = "html"; |
47
|
|
|
|
48
|
20 |
|
$app["conneg"] = function (Container $app) { |
49
|
12 |
|
if ($app->offsetExists("serializer")) { |
50
|
12 |
|
if ($app["serializer"] instanceof JMS\Serializer) { |
51
|
6 |
|
if (!$app->offsetExists("conneg.serializationContext")) { |
52
|
6 |
|
$app["conneg.serializationContext"] = null; |
53
|
6 |
|
} |
54
|
6 |
|
if (!$app->offsetExists("conneg.deserializationContext")) { |
55
|
6 |
|
$app["conneg.deserializationContext"] = null; |
56
|
6 |
|
} |
57
|
|
|
|
58
|
6 |
|
return new JmsSerializerContentNegotiation($app); |
|
|
|
|
59
|
6 |
|
} elseif ($app["serializer"] instanceof SymfonySerializer\Serializer) { |
60
|
6 |
|
if (!$app->offsetExists("conneg.serializationContext")) { |
61
|
6 |
|
$app["conneg.serializationContext"] = array(); |
62
|
6 |
|
} |
63
|
6 |
|
if (!$app->offsetExists("conneg.deserializationContext")) { |
64
|
6 |
|
$app["conneg.deserializationContext"] = array(); |
65
|
6 |
|
} |
66
|
|
|
|
67
|
6 |
|
return new SymfonySerializerContentNegotiation($app); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
throw new ServiceUnavailableHttpException(null, "No supported serializer found"); |
72
|
|
|
}; |
73
|
20 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* This before middleware validates whether the application will be able to |
77
|
|
|
* respond in a format that the client understands. |
78
|
|
|
* |
79
|
|
|
* @param Request $request |
80
|
|
|
* @throws NotAcceptableHttpException |
81
|
|
|
*/ |
82
|
20 |
|
public function setRequestFormat(Request $request, Application $app) |
83
|
|
|
{ |
84
|
|
|
// If there is no Accept header, do nothing |
85
|
20 |
|
if (!$request->headers->get("Accept")) { |
86
|
1 |
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Check the Accept header for acceptable formats |
90
|
19 |
|
foreach ($request->getAcceptableContentTypes() as $contentType) { |
91
|
|
|
// If any format is acceptable, do nothing |
92
|
19 |
|
if ($contentType === "*/*") { |
93
|
1 |
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
18 |
|
$format = $request->getFormat($contentType); |
97
|
18 |
|
if (in_array($format, $app["conneg.responseFormats"])) { |
98
|
|
|
// An acceptable format was found. Set it as the requestFormat |
99
|
|
|
// where it can be referenced later. |
100
|
15 |
|
$request->setRequestFormat($format); |
101
|
15 |
|
return; |
102
|
|
|
} |
103
|
9 |
|
} |
104
|
|
|
|
105
|
|
|
// No acceptable formats were found |
106
|
3 |
|
throw new NotAcceptableHttpException(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* This before middleware validates that the request body is in a format |
111
|
|
|
* that the application understands. |
112
|
|
|
* |
113
|
|
|
* @param Request $request |
114
|
|
|
* @throws UnsupportedMediaTypeHttpException |
115
|
|
|
*/ |
116
|
17 |
|
public function validateRequestContentType(Request $request, Application $app) |
117
|
|
|
{ |
118
|
|
|
// Define the "form" format so we can use it for validation |
119
|
17 |
|
$request->setFormat("form", array("application/x-www-form-urlencoded", "multipart/form-data")); |
120
|
|
|
|
121
|
17 |
|
$format = $request->getContentType(); |
122
|
17 |
|
if (strlen($request->getContent()) > 0 && !in_array($format, $app["conneg.requestFormats"])) { |
123
|
|
|
// The request has a body but it is not a supported media type |
124
|
3 |
|
throw new UnsupportedMediaTypeHttpException(); |
125
|
|
|
} |
126
|
14 |
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.