diff options
Diffstat (limited to 'src/Manifest.php')
| -rw-r--r-- | src/Manifest.php | 51 | 
1 files changed, 51 insertions, 0 deletions
| diff --git a/src/Manifest.php b/src/Manifest.php new file mode 100644 index 0000000..b7c5e96 --- /dev/null +++ b/src/Manifest.php @@ -0,0 +1,51 @@ +<?php + +namespace Lightscale\LaralightAssets; + +class Manifest +{ +    private ?array $data; + +    public function __construct( +        private string $path, +        private string $baseUrl, +        private string $parserClass = JsonManifestParser::class, +    ) {} + +    private function readFile(): string +    { +        return filegetcontents($this->path); +    } + +    private function loadData(): void +    { +        $data = $this->readFile(); +        $parser = new $this->parserClass; +        $this->data = $parser->parse($data); +    } + +    private function getData(): array +    { +        if ($this->data === null) $this->loadData(); +        return $this->data; +    } + +    public function getFile(string $file): ?string +    { +        return $this->getData()[$file] ?? null; +    } + +    public function getUrl(string $file): ?string +    { +        $file = $this->getFile($file); +        return $file === null ? null : "{$this->baseUrl}/{$file}"; +    } + +    public function getPath(string $file): ?string +    { +        $file = $this->getFile($file); +        $dir = $this->getDir(); +        return $file === null ? null : "{$dir}/{$file}"; +    } + +} | 
