ManifestBuilder::buildAssets()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
4
namespace ExoUNX\Vasri;
5
6
use Illuminate\Support\Facades\File;
7
use Exception;
8
9
/**
10
 * Constructs Manifest
11
 * Class ManifestBuilder
12
 *
13
 * @package ExoUNX\Vasri
14
 * @author  Gaige Lama <[email protected]>
15
 * @license MIT License
16
 * @link    https://github.com/ExoUNX/Vasri
17
 */
18
class ManifestBuilder
19
{
20
21
    /**
22
     * @var array
23
     */
24
    private $vasriConfig;
25
    /**
26
     * @var bool
27
     */
28
    private $isMixManifestEnabled;
29
30
    /**
31
     * @var string
32
     */
33
    private $mixManifestPath;
34
    /**
35
     * @var array
36
     */
37
    private $mixManifest;
38
    /**
39
     * @var ManifestReader
40
     */
41
    private $manifestReader;
42
43
    /**
44
     * @var mixed
45
     */
46
    private $isMixManifestAltEnabled;
47
48
    /**
49
     * @var Builder
50
     */
51
    private $builder;
52
53
    /**
54
     * ManifestBuilder constructor.
55
     * @throws Exception
56
     */
57
    public function __construct()
58
    {
59
        $this->manifestReader          = new ManifestReader();
60
        $this->builder                 = new Builder();
61
        $this->vasriConfig             = config('vasri');
62
        $this->isMixManifestEnabled    = $this->vasriConfig['mix-manifest'];
63
        $this->isMixManifestAltEnabled = $this->vasriConfig['mix-manifest-alt'];
64
        $this->mixManifestPath         = public_path('mix-manifest.json');
65
    }
66
67
    /**
68
     * Builds the basic asset list
69
     *
70
     * @param  array  $mixManifest
71
     * @param  array  $vasriConfigAssets
72
     *
73
     * @return array
74
     * @throws Exception
75
     */
76
    private function buildAssets(array $mixManifest = [], array $vasriConfigAssets = []): array
77
    {
78
79
        $vasriManifest = [];
80
81
        if ($this->isMixManifestEnabled && File::exists($this->mixManifestPath)) {
82
83
            $this->loopManifest($mixManifest, $vasriManifest);
84
85
        } elseif ( ! empty($vasriConfigAssets)) {
86
87
            $vasriManifest = $this->vasriConfig['assets'];
88
89
        } else {
90
91
            throw new Exception('No manifest or valid assets found');
92
93
        }
94
95
        return $vasriManifest;
96
    }
97
98
    /**
99
     * @param  array  $mixManifest
100
     * @param  array  $vasriManifest
101
     */
102
    private function loopManifest(array $mixManifest, array &$vasriManifest = []): void
103
    {
104
        if ($this->isMixManifestAltEnabled) {
105
106
            foreach ($mixManifest as $key => $val) {
107
                $vasriManifest[$key] = $val;
108
            }
109
110
        } else {
111
112
            foreach ($mixManifest as $key => $val) {
113
                $vasriManifest[] = $key;
114
            }
115
116
        }
117
    }
118
119
    /**
120
     * Builds the manifest based off the asset list
121
     *
122
     * @return array
123
     * @throws Exception
124
     */
125
    private function buildManifest(): array
126
    {
127
        $manifest = [];
128
129
        foreach (
130
            $this->buildAssets(
131
                $this->manifestReader->getManifest($this->mixManifestPath),
132
                $this->vasriConfig['assets']
133
            ) as $asset => $alt
134
        ) {
135
            if ($this->builder->parseExtension($asset) === 'js' || $this->builder->parseExtension($asset) === 'css') {
136
                $manifest[$asset] = [
137
                    'sri'     => $this->builder->sri($alt),
138
                    'version' => $this->builder->versioning($alt),
139
                    'alt'     => $alt
140
                ];
141
            }
142
        }
143
144
        return $manifest;
145
    }
146
147
    /**
148
     * Deploys the manifest as json file in the Laravel base directory
149
     *
150
     * @throws Exception
151
     */
152
    public function deployManifest(): void
153
    {
154
        file_put_contents(base_path('vasri-manifest.json'),
155
            stripslashes(json_encode($this->buildManifest(), JSON_PRETTY_PRINT)));
156
    }
157
158
}
159