| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace PSFS\controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PSFS\base\exception\RouterException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use PSFS\base\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PSFS\base\types\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PSFS\base\types\helpers\ResponseHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Class DocumentorController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package PSFS\controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class DocumentorController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     const DOMAIN = 'ROOT'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @Injectable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var \PSFS\services\DocumentorService $srv | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $srv; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @GET | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @CACHE 600 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @label Generador de documentación API | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @route /{domain}/api/doc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param string $domain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @return mixed|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @throws \ReflectionException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     public function createApiDocs($domain) | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         ini_set('memory_limit', -1); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         ini_set('max_execution_time', -1); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $type = $this->getRequest()->get('type') ?: ApiController::PSFS_DOC; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $download = $this->getRequest()->get('download') ?: false; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $module = $this->srv->getModules($domain); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         if (empty($module)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             return ResponseHelper::httpNotFound(null, true); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $doc = $this->srv->extractApiEndpoints($module); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         switch (strtolower($type)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             case ApiController::SWAGGER_DOC: | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |                 $doc = $this->srv->swaggerFormatter($module, $doc); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             case ApiController::POSTMAN_DOC: | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 $doc = ['Pending...']; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         if ($download && $type === ApiController::SWAGGER_DOC) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             return $this->download(json_encode($doc), 'application/json', 'swagger.json'); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         if ($type === ApiController::HTML_DOC) { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             return $this->render('documentation.html.twig', ["data" => json_encode($doc)]); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         return $this->json($doc, 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @GET | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @route /admin/{domain}/swagger-ui | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param string $domain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return string HTML | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function swaggerUi($domain) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if (!Router::getInstance()->domainExists($domain)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             throw new RouterException('Domains is empty'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return $this->render('swagger.html.twig', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             'domain' => $domain, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  |