summaryrefslogtreecommitdiff
path: root/src/Assets.php
blob: 981ac76d492e28e20a48dd056a6f33032a2db857 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

namespace Lightscale\LaralightAssets;

use InvalidArgumentException;

class Assets
{
    private ?string $defaultManifest = null;
    private array $manifests = [];
    private array $manifestsParsers = [];
    private array $files = [];
    private array $queuedHeadFiles = [];
    private array $queuedFooterFiles = [];

    public function addManifest(
        string $name,
        Manifest $manifest,
        bool $isDefault = false
    ): void
    {
        $this->manifests[$name] = $manifest;

        if ($isDefault) {
            $this->defaultManifest = $name;
        }
    }

    public function registerManifest(
        string $name,
        string $path,
        string $baseUrl,
        string $parserClass = JsonManifestParser::class,
        bool $isDefault = false,
    ): void
    {
        $this->addManifest(
            $name,
            new Manifest($path, $baseUrl, $parserClass),
            $isDefault
        );
    }

    public function getManifests(): array
    {
        return $this->manifests;
    }

    public function getManifest(string $name): Manifest
    {
        return $this->getManifests()[$name];
    }

    public function setDefaultManifest(?string $name): void
    {
        if ($name !== null && ($this->manifests[$name] ?? false) === false) {
            throw new InvalidArgumentException(
                "No manifest with name {$name} exists"
            );
        }

        $this->defaultManifest = $name;
    }

    public function getDefaultManifest(): ?Manifest
    {
        return (
            $this->defaultManifest === null ?
            null :
            $this->getManifest($this->defaultManifest)
        );
    }

    public function getFileManifest(FileString $file): ?Manifest
    {
        if ($file->manifestName !== null) {
            return $this->getManifest($file->manifestName);
        }
        else {
            return $this->getDefaultManifest();
        }
    }

    public function queueFile(string $file, string $fileClass, bool $footer): void
    {
        $file = new FileString($file);
        $manifest = $this->getFileManifest($file);
    }

    public function queueScript(string $file, bool $footer = true): void
    {
        $this->queueFile($file, ScriptFile::class, $footer);
    }

    public function queueStyle(string $file): void
    {
        $this->queueFile($file, StyleFile::class, false);
    }

    public function getQueuedHeadFiles(): array
    {
        return $this->queuedHeadFiles;
    }

    public function getQueuedFooterFiles(): array
    {
        return $this->queuedFooterFiles;
    }
}