|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Basster\Silex\Provider\Swagger; |
|
4
|
|
|
|
|
5
|
|
|
use Silex\Application; |
|
6
|
|
|
use Silex\ServiceProviderInterface; |
|
7
|
|
|
use Swagger\Annotations as SWG; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
|
|
10
|
|
|
class SwaggerProvider implements ServiceProviderInterface, SwaggerServiceKey |
|
11
|
|
|
{ |
|
12
|
|
|
/** {@inheritdoc} */ |
|
13
|
|
|
public function register(Application $app) |
|
14
|
|
|
{ |
|
15
|
|
|
$app[self::SWAGGER_API_DOC_PATH] = '/api/api-docs'; |
|
16
|
|
|
$app[self::SWAGGER_EXCLUDE_PATH] = null; |
|
17
|
|
|
$app[self::SWAGGER_CACHE] = []; |
|
18
|
|
|
$app[self::SWAGGER_SERVICE_PATH] = null; |
|
19
|
|
|
$app[self::SWAGGER_ANALYSER] = null; |
|
20
|
|
|
$app[self::SWAGGER_ANALYSIS] = null; |
|
21
|
|
|
$app[self::SWAGGER_PROCESSORS] = []; |
|
22
|
|
|
|
|
23
|
|
|
$app[self::SWAGGER_CONFIG] = $app::share(function (Application $app) { |
|
24
|
|
|
return new SwaggerConfig( |
|
25
|
|
|
$app[self::SWAGGER_SERVICE_PATH], |
|
26
|
|
|
$app[self::SWAGGER_ANALYSER], |
|
27
|
|
|
$app[self::SWAGGER_ANALYSIS], |
|
28
|
|
|
$app[self::SWAGGER_PROCESSORS], |
|
29
|
|
|
$app[self::SWAGGER_EXCLUDE_PATH] |
|
30
|
|
|
); |
|
31
|
|
|
}); |
|
32
|
|
|
|
|
33
|
|
|
$app[self::SWAGGER_SERVICE] = $app::share(function (Application $app) { |
|
34
|
|
|
return new SwaggerService($app[self::SWAGGER_CONFIG]); |
|
35
|
|
|
}); |
|
36
|
|
|
|
|
37
|
|
|
$app[self::SWAGGER] = $app::share(function (Application $app) { |
|
38
|
|
|
/** @var SwaggerService $swagger */ |
|
39
|
|
|
$swagger = $app[self::SWAGGER_SERVICE]; |
|
40
|
|
|
return $swagger->getSwagger(); |
|
41
|
|
|
}); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** {@inheritdoc} */ |
|
45
|
|
|
public function boot(Application $app) |
|
46
|
|
|
{ |
|
47
|
|
|
$app->get($app[self::SWAGGER_API_DOC_PATH], |
|
48
|
|
|
function (Application $app, Request $request) { |
|
49
|
|
|
/** @var SwaggerService $swagger */ |
|
50
|
|
|
$swagger = $app[self::SWAGGER_SERVICE]; |
|
51
|
|
|
$response = $swagger->getSwaggerResponse($app[self::SWAGGER_CACHE]); |
|
52
|
|
|
$response->isNotModified($request); |
|
53
|
|
|
|
|
54
|
|
|
return $response; |
|
55
|
|
|
}); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|