Completed
Push — dev ( 42275e...223161 )
by Gaige
57s
created

ManifestBuilder::loopManifest()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 16
rs 9.7333
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
        if ($this->isMixManifestEnabled && File::exists($this->mixManifestPath)) {
80
81
            $this->loopManifest($mixManifest, $vasriManifest);
82
83
        } elseif ( ! empty($vasriConfigAssets)) {
84
85
            $vasriManifest = $this->vasriConfig['assets'];
86
87
        } else {
88
89
            throw new Exception('No manifest or valid assets found');
90
91
        }
92
93
        return $vasriManifest;
94
    }
95
96
    /**
97
     * @param  array  $mixManifest
98
     * @param  array  $vasriManifest
99
     */
100
    private function loopManifest(array $mixManifest, array &$vasriManifest = [])
101
    {
102
        if ($this->isMixManifestAltEnabled) {
103
104
            foreach ($mixManifest as $key => $val) {
105
                $vasriManifest[] = $val;
106
            }
107
108
        } else {
109
110
            foreach ($mixManifest as $key => $val) {
111
                $vasriManifest[] = $key;
112
            }
113
114
        }
115
    }
116
117
    /**
118
     * Builds the manifest based off the asset list
119
     *
120
     * @return array
121
     * @throws Exception
122
     */
123
    private function buildManifest(): array
124
    {
125
        $manifest = [];
126
127
        foreach (
128
            $this->buildAssets(
129
                $this->manifestReader->getManifest($this->mixManifestPath),
130
                $this->vasriConfig['assets']
131
            ) as $asset
132
        ) {
133
            $manifest[$asset] = [
134
                'sri'     => $this->builder->sri($asset),
135
                'version' => $this->builder->versioning($asset)
136
            ];
137
        }
138
139
        return $manifest;
140
    }
141
142
    /**
143
     * Deploys the manifest as json file in the Laravel base directory
144
     *
145
     * @throws Exception
146
     */
147
    public function deployManifest(): void
148
    {
149
        file_put_contents(base_path('vasri-manifest.json'),
150
            stripslashes(json_encode($this->buildManifest(), JSON_PRETTY_PRINT)));
151
    }
152
153
}
154