vendor/harmbandstra/swagger-ui-bundle/src/Controller/DocsController.php line 41

Open in your IDE?
  1. <?php
  2. namespace HarmBandstra\SwaggerUiBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Filesystem\Exception\FileNotFoundException;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Yaml\Yaml;
  11. class DocsController extends AbstractController
  12. {
  13.     /** @var array */
  14.     private $swaggerFiles;
  15.     /** @var string */
  16.     private $directory;
  17.     /** @var string */
  18.     private $assetUrlPath;
  19.     /** @var string|null */
  20.     private $configFile;
  21.     public function __construct($swaggerFiles$directory$assetUrlPath$configFile)
  22.     {
  23.         $this->swaggerFiles $swaggerFiles;
  24.         $this->directory $directory;
  25.         $this->assetUrlPath $assetUrlPath;
  26.         $this->configFile $configFile;
  27.     }
  28.     /**
  29.      * @param Request $request
  30.      *
  31.      * @return Response
  32.      */
  33.     public function indexAction(Request $request)
  34.     {
  35.         if (!$request->get('url')) {
  36.             // if there is no ?url=... parameter, redirect to the default one
  37.             $defaultSwaggerFile reset($this->swaggerFiles);
  38.             return $this->redirect($this->getRedirectUrlToSpec($defaultSwaggerFile));
  39.         }
  40.         $contents = @file_get_contents(__DIR__ '/../Resources/public/index.html');
  41.         if ($contents === false) {
  42.             return new Response(
  43.                 'Unable to load [Resources/public/index.html]. Did [ScriptHandler::linkAssets] run correctly?',
  44.                 Response::HTTP_INTERNAL_SERVER_ERROR
  45.             );
  46.         }
  47.         return new Response($contents);
  48.     }
  49.     /**
  50.      * @param string $fileName
  51.      *
  52.      * @return RedirectResponse
  53.      */
  54.     public function redirectAction($fileName)
  55.     {
  56.         // redirect to swagger file if that's what we're looking for
  57.         if (in_array($fileName$this->swaggerFilestrue)) {
  58.             return $this->redirect($this->getRedirectUrlToSpec($fileName));
  59.         }
  60.         // redirect to the assets dir so that relative links work
  61.         return $this->redirect($this->assetUrlPath $fileName);
  62.     }
  63.     /**
  64.      * @param string $fileName
  65.      *
  66.      * @return JsonResponse|Response
  67.      */
  68.     public function swaggerFileAction($fileName)
  69.     {
  70.         try {
  71.             $filePath $this->getFilePath($fileName);
  72.         } catch (\Exception $e) {
  73.             return new JsonResponse($e->getMessage(), Response::HTTP_NOT_FOUND);
  74.         }
  75.         $extension strtolower(pathinfo($filePathPATHINFO_EXTENSION));
  76.         if ($extension === 'yml' || $extension === 'yaml') {
  77.             $fileContents Yaml::parse(file_get_contents($filePath));
  78.             return new JsonResponse($fileContents);
  79.         }
  80.         $fileContents file_get_contents($filePath);
  81.         return new Response(
  82.             $fileContents,
  83.             Response::HTTP_OK,
  84.             ['Content-Type' => 'application/json']
  85.         );
  86.     }
  87.     /**
  88.      * @param string $fileName
  89.      *
  90.      * @return string
  91.      */
  92.     private function getFilePath($fileName '')
  93.     {
  94.         if ($this->configFile !== $fileName) {
  95.             if ($fileName !== '' && !in_array($fileName$this->swaggerFiles)) {
  96.                 throw new \RuntimeException(
  97.                     sprintf('File [%s] not defined under [hb_swagger_ui.files] in config.yml.'$fileName)
  98.                 );
  99.             }
  100.         }
  101.         if ($this->directory === '') {
  102.             throw new \RuntimeException(
  103.                 'Directory [hb_swagger_ui.directory] not defined or empty in config.yml.'
  104.             );
  105.         }
  106.         $filePath realpath($this->directory DIRECTORY_SEPARATOR $fileName);
  107.         if (!is_file($filePath)) {
  108.             throw new FileNotFoundException(sprintf('File [%s] not found.'$fileName));
  109.         }
  110.         return $filePath;
  111.     }
  112.     /**
  113.      * @param string $fileName
  114.      *
  115.      * @return string
  116.      */
  117.     private function getRedirectUrlToSpec($fileName)
  118.     {
  119.         if (strpos($fileName'/') === || preg_match('#http[s]?://#'$fileName)) {
  120.             // if absolute path or URL use it raw
  121.             $specUrl $fileName;
  122.         } else {
  123.             $specUrl $this->generateUrl(
  124.                 'hb_swagger_ui_swagger_file',
  125.                 ['fileName' => $fileName],
  126.                 UrlGeneratorInterface::ABSOLUTE_PATH
  127.             );
  128.         }
  129.         $parameters = ['url' => $specUrl];
  130.         if ($this->configFile) {
  131.             $parameters['configUrl'] = $this->generateUrl(
  132.                 'hb_swagger_ui_swagger_file',
  133.                 ['fileName' => $this->configFile],
  134.                 UrlGeneratorInterface::ABSOLUTE_PATH
  135.             );
  136.         }
  137.         return $this->generateUrl('hb_swagger_ui_default'$parameters);
  138.     }
  139. }