vendor/shopware/storefront/Theme/ThemeAssetPackage.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Theme;
  3. use Shopware\Core\Framework\Adapter\Asset\FallbackUrlPackage;
  4. use Shopware\Core\Framework\Feature;
  5. use Shopware\Core\PlatformRequest;
  6. use Shopware\Core\SalesChannelRequest;
  7. use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. class ThemeAssetPackage extends FallbackUrlPackage
  10. {
  11.     private RequestStack $requestStack;
  12.     private AbstractThemePathBuilder $themePathBuilder;
  13.     /**
  14.      * @internal
  15.      */
  16.     public function __construct(
  17.         $baseUrls,
  18.         VersionStrategyInterface $versionStrategy,
  19.         RequestStack $requestStack,
  20.         AbstractThemePathBuilder $themePathBuilder
  21.     ) {
  22.         parent::__construct($baseUrls$versionStrategy);
  23.         $this->requestStack $requestStack;
  24.         $this->themePathBuilder $themePathBuilder;
  25.     }
  26.     /**
  27.      * @return string
  28.      */
  29.     public function getUrl(string $path)
  30.     {
  31.         if ($this->isAbsoluteUrl($path)) {
  32.             return $path;
  33.         }
  34.         $url $path;
  35.         if ($url && $url[0] !== '/') {
  36.             $url '/' $url;
  37.         }
  38.         /**
  39.          * @deprecated tag:v6.5.0 - whole if can be removed, as it is not supported anymore
  40.          */
  41.         if (str_starts_with($url'/bundles') || str_starts_with($url'/theme/')) {
  42.             Feature::triggerDeprecationOrThrow(
  43.                 'v6.5.0.0',
  44.                 'Accessing "theme" asset with "/bundles" or "/themes" prefixed path will be removed with 6.5.0.0'
  45.             );
  46.             $url $this->getVersionStrategy()->applyVersion($url);
  47.             if ($this->isAbsoluteUrl($url)) {
  48.                 return $url;
  49.             }
  50.             return $this->getBaseUrl($path) . $url;
  51.         }
  52.         $url $this->getVersionStrategy()->applyVersion($this->appendThemePath() . $url);
  53.         if ($this->isAbsoluteUrl($url)) {
  54.             return $url;
  55.         }
  56.         return $this->getBaseUrl($path) . $url;
  57.     }
  58.     private function appendThemePath(): string
  59.     {
  60.         $currentRequest $this->requestStack->getMainRequest();
  61.         if ($currentRequest === null) {
  62.             return '';
  63.         }
  64.         $salesChannelId $currentRequest->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  65.         $themeId $currentRequest->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_ID);
  66.         if ($themeId === null || $salesChannelId === null) {
  67.             return '';
  68.         }
  69.         return '/theme/' $this->themePathBuilder->assemblePath($salesChannelId$themeId);
  70.     }
  71. }