1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JDesrosiers\Silex\Provider; |
4
|
|
|
|
5
|
|
|
use Silex\Application; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
9
|
|
|
|
10
|
|
|
class ResourceDefinitionController |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Route for GETting each of the resource definitions |
14
|
|
|
* |
15
|
|
|
* @param Application $app |
16
|
|
|
* @param Request $request |
17
|
|
|
* @param string $service |
18
|
|
|
* |
19
|
|
|
* @return Response |
20
|
|
|
* @throws NotFoundHttpException |
21
|
|
|
*/ |
22
|
6 |
|
public function __invoke(Application $app, Request $request, $service) |
23
|
|
|
{ |
24
|
6 |
|
$resourceName = "/" . str_replace("-", "/", $service); |
25
|
6 |
|
$resourceNames = $app["swagger"]->getResourceNames(); |
26
|
6 |
|
if (!in_array($resourceName, $resourceNames)) { |
27
|
6 |
|
$resourceNamesDisplay = implode('", "', $resourceNames); |
28
|
6 |
|
throw new NotFoundHttpException("Resource \"$resourceName\" not found, try \"$resourceNamesDisplay\""); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$options = array( |
32
|
6 |
|
"output" => "json", |
33
|
6 |
|
"json_pretty_print" => $app["swagger.prettyPrint"], |
34
|
6 |
|
"defaultBasePath" => $app["swagger.basePath"], |
35
|
6 |
|
"defaultApiVersion" => $app["swagger.apiVersion"], |
36
|
6 |
|
"defaultSwaggerVersion" => $app["swagger.swaggerVersion"], |
37
|
|
|
); |
38
|
6 |
|
$json = $app["swagger"]->getResource($resourceName, $options); |
39
|
|
|
|
40
|
6 |
|
$response = Response::create($json, 200, array("Content-Type" => "application/json")); |
41
|
6 |
|
$response->setCache($app["swagger.cache"]); |
42
|
6 |
|
$response->setEtag(md5($json)); |
43
|
6 |
|
$response->isNotModified($request); |
44
|
|
|
|
45
|
6 |
|
return $response; |
46
|
|
|
} |
47
|
|
|
} |