|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Obblm\Core\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Obblm\Core\Contracts\BuildAssetsInterface; |
|
6
|
|
|
use Obblm\Core\Exception\NotFoundEntrypointException; |
|
7
|
|
|
use Symfony\Component\Asset\Package; |
|
8
|
|
|
use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy; |
|
9
|
|
|
|
|
10
|
|
|
class AssetPackager |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var Package */ |
|
13
|
|
|
private $package; |
|
14
|
|
|
private $entrypoints = []; |
|
15
|
|
|
|
|
16
|
|
|
public function addBuildAsset(BuildAssetsInterface $buildAssets) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->load($buildAssets->getPath()); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
4 |
|
public function addDirectory($directory) |
|
22
|
|
|
{ |
|
23
|
4 |
|
$this->load($directory); |
|
24
|
2 |
|
} |
|
25
|
|
|
|
|
26
|
4 |
|
private function load($directory) |
|
27
|
|
|
{ |
|
28
|
4 |
|
$manifestPath = $directory.'/manifest.json'; |
|
29
|
4 |
|
$entrypointsPath = $directory.'/entrypoints.json'; |
|
30
|
|
|
|
|
31
|
4 |
|
$this->package = new Package(new JsonManifestVersionStrategy($manifestPath)); |
|
32
|
|
|
|
|
33
|
4 |
|
if (!is_file($entrypointsPath)) { |
|
34
|
2 |
|
throw new \RuntimeException(sprintf('Entrypoints file "%s" does not exist.', $entrypointsPath)); |
|
35
|
|
|
} |
|
36
|
2 |
|
$json = file_get_contents($entrypointsPath); |
|
37
|
|
|
|
|
38
|
2 |
|
$loadedEntrypoints = json_decode($json, true); |
|
39
|
|
|
|
|
40
|
2 |
|
$this->entrypoints = array_merge_recursive($this->entrypoints, $loadedEntrypoints['entrypoints']); |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
public function getCssEntry(string $entrypoint) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$entrypoint = $this->getEntryPoint($entrypoint); |
|
46
|
1 |
|
return isset($entrypoint['css']) ? $entrypoint['css'] : []; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function getJsEntry(string $entrypoint) |
|
50
|
|
|
{ |
|
51
|
1 |
|
$entrypoint = $this->getEntryPoint($entrypoint); |
|
52
|
1 |
|
return isset($entrypoint['js']) ? $entrypoint['js'] : []; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function getEntryPoint(string $entrypoint) |
|
56
|
|
|
{ |
|
57
|
2 |
|
if (!isset($this->entrypoints[$entrypoint])) { |
|
58
|
1 |
|
throw new NotFoundEntrypointException($entrypoint, self::class); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
return $this->entrypoints[$entrypoint]; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|