Repository navigation
Expand file tree
/
Copy pathMapperAwareAssetPackage.php
More file actions
77 lines (65 loc) · 3.14 KB
/
Copy pathMapperAwareAssetPackage.php
File metadata and controls
77 lines (65 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\AssetMapper;
use Symfony\Component\Asset\PackageInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Decorates asset packages to support resolving assets from the asset mapper.
*
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
final class MapperAwareAssetPackage implements PackageInterface
{
private readonly ?string $devServerPrefix;
private readonly ?string $publicPrefix;
/**
* @param string|null $devServerPublicPrefix The public prefix served by AssetMapperDevServerSubscriber, null when it is disabled
* @param PackageInterface|null $innerPackageWithoutVersion The decorated package without its version strategy, null when the Asset component is not configured
* @param string|null $publicPrefix The public prefix of the mapped assets, used to recognize a path the mapper already resolved
*/
public function __construct(
private readonly PackageInterface $innerPackage,
private readonly AssetMapperInterface $assetMapper,
private readonly ?RequestStack $requestStack = null,
?string $devServerPublicPrefix = null,
private readonly ?PackageInterface $innerPackageWithoutVersion = null,
?string $publicPrefix = null,
) {
$this->devServerPrefix = null === $devServerPublicPrefix ? null : '/'.trim($devServerPublicPrefix, '/').'/';
$this->publicPrefix = null === $publicPrefix ? null : '/'.trim($publicPrefix, '/').'/';
}
public function getVersion(string $path): string
{
return $this->innerPackage->getVersion($path);
}
public function getUrl(string $path): string
{
// the import map renders public paths, which the mapper already resolved
$isPublicPath = null !== $this->publicPrefix && str_starts_with('/'.$path, $this->publicPrefix);
$publicPath = $isPublicPath ? null : $this->assetMapper->getPublicPath($path);
// the content hash is the version, so the configured strategy must not add a second one.
// The package must be picked before the block below prepends the front controller.
$package = $publicPath || $isPublicPath
? $this->innerPackageWithoutVersion ?? $this->innerPackage
: $this->innerPackage;
if ($publicPath) {
$path = ltrim($publicPath, '/');
}
if (null !== $this->devServerPrefix && str_starts_with('/'.$path, $this->devServerPrefix)) {
// the dev server serves those assets through the kernel, so the front controller must be part of the URL
$request = $this->requestStack?->getMainRequest();
$frontController = $request ? trim(substr($request->getBaseUrl(), \strlen($request->getBasePath())), '/') : '';
if ('' !== $frontController) {
$path = $frontController.'/'.$path;
}
}
return $package->getUrl($path);
}
}