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 string */
  14.     private $projectDir;
  15.     /** @var array */
  16.     private $swaggerFiles;
  17.     /** @var string */
  18.     private $directory;
  19.     /** @var string */
  20.     private $assetUrlPath;
  21.     public function __construct($projectDir$swaggerFiles$directory$assetUrlPath)
  22.     {
  23.         $this->projectDir $projectDir;
  24.         $this->swaggerFiles $swaggerFiles;
  25.         $this->directory $directory;
  26.         $this->assetUrlPath $assetUrlPath;
  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.             $defaultSpecFile reset($this->swaggerFiles);
  38.             return $this->redirect($this->getRedirectUrlToSpec($defaultSpecFile));
  39.         }
  40.         $contents = @file_get_contents(__DIR__ '/../Resources/public/index.html');
  41.         if ($contents === false) {
  42.             throw new \RuntimeException('Unable to load [Resources/public/index.html]. Did [ScriptHandler::linkAssets] run correctly?');
  43.         }
  44.         return new Response($contents);
  45.     }
  46.     /**
  47.      * @param string $fileName
  48.      *
  49.      * @return RedirectResponse
  50.      */
  51.     public function redirectAction($fileName)
  52.     {
  53.         // redirect to swagger file if that's what we're looking for
  54.         if (in_array($fileName$this->swaggerFilestrue)) {
  55.             return $this->redirect($this->getRedirectUrlToSpec($fileName));
  56.         }
  57.         // redirect to the assets dir so that relative links work
  58.         return $this->redirect($this->assetUrlPath $fileName);
  59.     }
  60.     /**
  61.      * @param string $fileName
  62.      *
  63.      * @return JsonResponse|Response
  64.      */
  65.     public function swaggerFileAction($fileName)
  66.     {
  67.         try {
  68.             $filePath $this->getFilePath($fileName);
  69.         } catch (\Exception $e) {
  70.             return new JsonResponse($e->getMessage(), Response::HTTP_NOT_FOUND);
  71.         }
  72.         $extension strtolower(pathinfo($filePathPATHINFO_EXTENSION));
  73.         if ($extension === 'yml' || $extension === 'yaml') {
  74.             $fileContents Yaml::parse(file_get_contents($filePath));
  75.             return new JsonResponse($fileContents);
  76.         }
  77.         $fileContents file_get_contents($filePath);
  78.         return new Response(
  79.             $fileContents,
  80.             Response::HTTP_OK,
  81.             ['Content-Type' => 'application/json']
  82.         );
  83.     }
  84.     /**
  85.      * @param string $fileName
  86.      *
  87.      * @return string
  88.      */
  89.     private function getFilePath($fileName '')
  90.     {
  91.         if ($fileName !== '' && !in_array($fileName$this->swaggerFiles)) {
  92.             throw new \RuntimeException(
  93.                 sprintf('File [%s] not defined under [hb_swagger_ui.files] in config.yml.'$fileName)
  94.             );
  95.         }
  96.         if ($this->directory === '') {
  97.             throw new \RuntimeException(
  98.                 'Directory [hb_swagger_ui.directory] not defined or empty in config.yml.'
  99.             );
  100.         }
  101.         $filePath realpath($this->directory DIRECTORY_SEPARATOR $fileName);
  102.         if (!is_file($filePath)) {
  103.             throw new FileNotFoundException(sprintf('File [%s] not found.'$fileName));
  104.         }
  105.         return $filePath;
  106.     }
  107.     /**
  108.      * @param string $fileName
  109.      *
  110.      * @return string
  111.      */
  112.     private function getRedirectUrlToSpec($fileName)
  113.     {
  114.         if (strpos($fileName'/') === || preg_match('#http[s]?://#'$fileName)) {
  115.             // if absolute path or URL use it raw
  116.             $specUrl $fileName;
  117.         } else {
  118.             $specUrl $this->generateUrl(
  119.                 'hb_swagger_ui_swagger_file',
  120.                 ['fileName' => $fileName],
  121.                 UrlGeneratorInterface::ABSOLUTE_PATH
  122.             );
  123.         }
  124.         return $this->generateUrl('hb_swagger_ui_default', ['url' => $specUrl]);
  125.     }
  126. }