-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
AssetMapper.php
89 lines (73 loc) · 2.47 KB
/
AssetMapper.php
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
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\AssetMapper\Factory\MappedAssetFactoryInterface;
/**
* Finds and returns assets in the pipeline.
*
* @final
*/
class AssetMapper implements AssetMapperInterface
{
public const MANIFEST_FILE_NAME = 'manifest.json';
private ?array $manifestData = null;
public function __construct(
private readonly AssetMapperRepository $mapperRepository,
private readonly MappedAssetFactoryInterface $mappedAssetFactory,
private readonly CompiledAssetMapperConfigReader $compiledConfigReader,
) {
}
public function getAsset(string $logicalPath): ?MappedAsset
{
$filePath = $this->mapperRepository->find($logicalPath);
if (null === $filePath) {
return null;
}
return $this->mappedAssetFactory->createMappedAsset($logicalPath, $filePath);
}
public function allAssets(): iterable
{
foreach ($this->mapperRepository->all() as $logicalPath => $filePath) {
$asset = $this->getAsset($logicalPath);
if (null === $asset) {
throw new \LogicException(\sprintf('Asset "%s" could not be found.', $logicalPath));
}
yield $asset;
}
}
public function getAssetFromSourcePath(string $sourcePath): ?MappedAsset
{
$logicalPath = $this->mapperRepository->findLogicalPath($sourcePath);
if (null === $logicalPath) {
return null;
}
return $this->getAsset($logicalPath);
}
public function getPublicPath(string $logicalPath): ?string
{
$manifestData = $this->loadManifest();
if (isset($manifestData[$logicalPath])) {
return $manifestData[$logicalPath];
}
$asset = $this->getAsset($logicalPath);
return $asset?->publicPath;
}
private function loadManifest(): array
{
if (null === $this->manifestData) {
if (!$this->compiledConfigReader->configExists(self::MANIFEST_FILE_NAME)) {
$this->manifestData = [];
} else {
$this->manifestData = $this->compiledConfigReader->loadConfig(self::MANIFEST_FILE_NAME);
}
}
return $this->manifestData;
}
}