1
|
|
|
<?php |
2
|
|
|
namespace PSFS\controller; |
3
|
|
|
|
4
|
|
|
use PSFS\base\Router; |
5
|
|
|
use PSFS\base\types\Controller; |
6
|
|
|
use PSFS\services\DocumentorService; |
7
|
|
|
|
8
|
|
|
class DocumentorController extends Controller { |
9
|
|
|
const DOMAIN = 'ROOT'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @Inyectable |
13
|
|
|
* @var \PSFS\services\DocumentorService $srv |
14
|
|
|
*/ |
15
|
|
|
protected $srv; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @GET |
19
|
|
|
* @CACHE 600 |
20
|
|
|
* @label Generador de documentación API |
21
|
|
|
* @route /{domain}/api/doc |
22
|
|
|
* |
23
|
|
|
* @param string $domain |
24
|
|
|
* |
25
|
|
|
* @return string JSON |
26
|
|
|
*/ |
27
|
|
|
public function createApiDocs($domain) |
28
|
|
|
{ |
29
|
|
|
ini_set('memory_limit', -1); |
30
|
|
|
ini_set('max_execution_time', -1); |
31
|
|
|
|
32
|
|
|
$type = $this->getRequest()->get('type') ?: ApiController::PSFS_DOC; |
33
|
|
|
$download = $this->getRequest()->get('download') ?: false; |
34
|
|
|
|
35
|
|
|
$module = $this->srv->getModules($domain); |
36
|
|
|
if(empty($module)) { |
37
|
|
|
return Router::getInstance()->httpNotFound(null, true); |
38
|
|
|
} |
39
|
|
|
switch (strtolower($type)) { |
40
|
|
|
case ApiController::SWAGGER_DOC: |
41
|
|
|
$doc = DocumentorService::swaggerFormatter($module); |
42
|
|
|
break; |
43
|
|
|
case ApiController::POSTMAN_DOC: |
44
|
|
|
$doc = ['Pending...']; |
45
|
|
|
break; |
46
|
|
|
default: |
47
|
|
|
case ApiController::HTML_DOC: |
48
|
|
|
case ApiController::PSFS_DOC: |
49
|
|
|
$doc = $this->srv->extractApiEndpoints($module); |
50
|
|
|
break; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
ini_restore('max_execution_time'); |
54
|
|
|
ini_restore('memory_limit'); |
55
|
|
|
|
56
|
|
|
if($download && $type === ApiController::SWAGGER_DOC) { |
|
|
|
|
57
|
|
|
return $this->download(\GuzzleHttp\json_encode($doc), 'application/json', 'swagger.json'); |
58
|
|
|
} elseif($type === ApiController::HTML_DOC) { |
59
|
|
|
return $this->render('documentation.html.twig', ["data" => json_encode($doc)]); |
60
|
|
|
} else { |
61
|
|
|
return $this->json($doc, 200); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @GET |
67
|
|
|
* @route /admin/{domain}/swagger-ui |
68
|
|
|
* @param string $domain |
69
|
|
|
* @return string HTML |
|
|
|
|
70
|
|
|
*/ |
71
|
|
|
public function swaggerUi($domain) { |
72
|
|
|
return $this->render('swagger.html.twig', [ |
73
|
|
|
'domain' => $domain, |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: