Skip to content

Commit

Permalink
Use Request::getUriForPath to build absolute URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
andyexeter committed Jan 23, 2024
1 parent 0d22731 commit 26525ca
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/vite-bundle/src/Asset/ViteAssetVersionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
use Pentatrion\ViteBundle\Service\FileAccessor;
use Symfony\Component\Asset\Exception\AssetNotFoundException;
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class ViteAssetVersionStrategy implements VersionStrategyInterface
{
private FileAccessor $fileAccessor;
private array $configs;
private string $configName;
private $useAbsoluteUrl;
private ?RouterInterface $router;
private ?RequestStack $requestStack;
private bool $strictMode;

private ?string $viteMode = null;
Expand All @@ -26,14 +26,14 @@ public function __construct(
array $configs,
string $defaultConfigName,
bool $useAbsoluteUrl,
RouterInterface $router = null,
RequestStack $requestStack = null,
bool $strictMode = true
) {
$this->fileAccessor = $fileAccessor;
$this->configs = $configs;
$this->configName = $defaultConfigName;
$this->useAbsoluteUrl = $useAbsoluteUrl;
$this->router = $router;
$this->requestStack = $requestStack;
$this->strictMode = $strictMode;

$this->setConfig($this->configName);
Expand Down Expand Up @@ -64,11 +64,11 @@ public function applyVersion(string $path): string

private function completeURL(string $path): string
{
if (0 === strpos($path, 'http') || false === $this->useAbsoluteUrl || null === $this->router) {
if (0 === strpos($path, 'http') || false === $this->useAbsoluteUrl || null === $this->requestStack || null === $this->requestStack->getCurrentRequest()) {
return $path;
}

return $this->router->getContext()->getScheme().'://'.$this->router->getContext()->getHost().$path;
return $this->requestStack->getCurrentRequest()->getUriForPath($path);
}

private function getassetsPath(string $path): ?string
Expand Down
4 changes: 2 additions & 2 deletions src/vite-bundle/src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
- "@pentatrion_vite.tag_renderer_collection"
- "%pentatrion_vite.absolute_url%"
- "%pentatrion_vite.preload%"
- "@?router"
- "@?request_stack"
- "@?event_dispatcher"

Pentatrion\ViteBundle\Service\EntrypointRenderer:
Expand Down Expand Up @@ -47,7 +47,7 @@ services:
- "%pentatrion_vite.configs%"
- "%pentatrion_vite.default_config%"
- "%pentatrion_vite.absolute_url%"
- "@?router"
- "@?request_stack"
- true


Expand Down
12 changes: 6 additions & 6 deletions src/vite-bundle/src/Service/EntrypointRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Pentatrion\ViteBundle\Event\RenderAssetTagEvent;
use Pentatrion\ViteBundle\Model\Tag;
use Pentatrion\ViteBundle\Util\InlineContent;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\Service\ResetInterface;

Expand All @@ -15,7 +15,7 @@ class EntrypointRenderer implements ResetInterface
private TagRendererCollection $tagRendererCollection;
private bool $useAbsoluteUrl;
private string $preload;
private ?RouterInterface $router;
private ?RequestStack $requestStack;
private ?EventDispatcherInterface $eventDispatcher;

private $returnedViteClients = [];
Expand All @@ -32,14 +32,14 @@ public function __construct(
TagRendererCollection $tagRendererCollection,
bool $useAbsoluteUrl = false,
string $preload = 'link-tag',
RouterInterface $router = null,
RequestStack $requestStack = null,
EventDispatcherInterface $eventDispatcher = null
) {
$this->entrypointsLookupCollection = $entrypointsLookupCollection;
$this->tagRendererCollection = $tagRendererCollection;
$this->useAbsoluteUrl = $useAbsoluteUrl;
$this->preload = $preload;
$this->router = $router;
$this->requestStack = $requestStack;
$this->eventDispatcher = $eventDispatcher;
}

Expand All @@ -55,11 +55,11 @@ private function getTagRenderer(string $configName = null): TagRenderer

private function completeURL(string $path, bool $useAbsoluteUrl = false): string
{
if (0 === strpos($path, 'http') || false === $useAbsoluteUrl || null === $this->router) {
if (0 === strpos($path, 'http') || false === $useAbsoluteUrl || null === $this->requestStack || null === $this->requestStack->getCurrentRequest()) {
return $path;
}

return $this->router->getContext()->getScheme().'://'.$this->router->getContext()->getHost().$path;
return $this->requestStack->getCurrentRequest()->getUriForPath($path);
}

private function shouldUseAbsoluteURL(array $options, string $configName = null): bool
Expand Down

0 comments on commit 26525ca

Please sign in to comment.