vendor/contao/core-bundle/src/Controller/FaviconController.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\Controller;
  11. use Contao\CoreBundle\Framework\ContaoFramework;
  12. use Contao\FilesModel;
  13. use Contao\PageModel;
  14. use FOS\HttpCache\ResponseTagger;
  15. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Webmozart\PathUtil\Path;
  20. /**
  21.  * @Route(defaults={"_scope" = "frontend"})
  22.  *
  23.  * @internal
  24.  */
  25. class FaviconController
  26. {
  27.     /**
  28.      * @var ContaoFramework
  29.      */
  30.     private $contaoFramework;
  31.     /**
  32.      * @var string
  33.      */
  34.     private $projectDir;
  35.     /**
  36.      * @var ResponseTagger|null
  37.      */
  38.     private $responseTagger;
  39.     public function __construct(ContaoFramework $contaoFrameworkstring $projectDirResponseTagger $responseTagger null)
  40.     {
  41.         $this->contaoFramework $contaoFramework;
  42.         $this->projectDir $projectDir;
  43.         $this->responseTagger $responseTagger;
  44.     }
  45.     /**
  46.      * @Route("/favicon.ico")
  47.      */
  48.     public function __invoke(Request $request): Response
  49.     {
  50.         $this->contaoFramework->initialize();
  51.         /** @var PageModel $pageModel */
  52.         $pageModel $this->contaoFramework->getAdapter(PageModel::class);
  53.         /** @var PageModel|null $rootPage */
  54.         $rootPage $pageModel->findPublishedFallbackByHostname(
  55.             $request->server->get('HTTP_HOST'),
  56.             ['fallbackToEmpty' => true]
  57.         );
  58.         if (null === $rootPage || null === ($favicon $rootPage->favicon)) {
  59.             return new Response(''Response::HTTP_NOT_FOUND);
  60.         }
  61.         /** @var FilesModel $filesModel */
  62.         $filesModel $this->contaoFramework->getAdapter(FilesModel::class);
  63.         $faviconModel $filesModel->findByUuid($favicon);
  64.         if (null === $faviconModel) {
  65.             return new Response(''Response::HTTP_NOT_FOUND);
  66.         }
  67.         // Cache the response for 1 year and tag it, so it is invalidated when the settings are edited
  68.         $response = new BinaryFileResponse(Path::join($this->projectDir$faviconModel->path));
  69.         $response->setSharedMaxAge(31556952);
  70.         switch ($faviconModel->extension) {
  71.             case 'svg':
  72.                 $response->headers->set('Content-Type''image/svg+xml');
  73.                 break;
  74.             case 'ico':
  75.                 $response->headers->set('Content-Type''image/x-icon');
  76.                 break;
  77.         }
  78.         if (null !== $this->responseTagger) {
  79.             $this->responseTagger->addTags(['contao.db.tl_page.'.$rootPage->id]);
  80.         }
  81.         return $response;
  82.     }
  83. }