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