Passed
Branch develop (7cbc6e)
by Andrew
07:57 queued 03:21
created

ManifestHelper::extractCssFiles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 13
rs 10
1
<?php
2
/**
3
 * Vite plugin for Craft CMS 3.x
4
 *
5
 * Allows the use of the Vite.js next generation frontend tooling with Craft CMS
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2021 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
10
11
namespace nystudio107\pluginvite\helpers;
12
13
use Craft;
14
use craft\helpers\Json as JsonHelper;
15
16
17
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
18
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
19
 * @package   Vite
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
20
 * @since     1.0.5
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
21
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
22
class ManifestHelper
23
{
24
    // Constants
25
    // =========================================================================
26
27
    const LEGACY_EXTENSION = '-legacy.';
28
29
    // Protected Static Properties
30
    // =========================================================================
31
32
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
33
     * @var array|null
34
     */
35
    protected static $manifest;
36
37
    // Public Static Methods
38
    // =========================================================================
39
40
    /**
41
     * Fetch and memoize the manifest file
42
     *
43
     * @param string $manifestPath
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
44
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
45
    public static function fetchManifest(string $manifestPath)
46
    {
47
        // Grab the manifest
48
        $pathOrUrl = (string)Craft::parseEnv($manifestPath);
49
        $manifest = FileHelper::fetch($pathOrUrl, [JsonHelper::class, 'decodeIfJson']);
50
        // If no manifest file is found, log it
51
        if ($manifest === null) {
52
            Craft::error('Manifest not found at ' . $manifestPath, __METHOD__);
53
        }
54
        // Ensure we're dealing with an array
55
        self::$manifest = (array)$manifest;
56
    }
57
58
    /**
59
     * Return an array of tags from the manifest, for both modern and legacy builds
60
     *
61
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
62
     * @param bool $asyncCss
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
63
     * @param array $scriptTagAttrs
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
64
     * @param array $cssTagAttrs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
65
     *
66
     * @return array
67
     */
68
    public static function manifestTags(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): array
69
    {
70
        // Get the modern tags for this $path
71
        return self::extractManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
72
    }
73
74
    /**
75
     * Return an array of tags from the manifest, for both modern and legacy builds
76
     *
77
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
78
     * @param bool $asyncCss
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
79
     * @param array $scriptTagAttrs
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
80
     * @param array $cssTagAttrs
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
81
     *
82
     * @return array
83
     */
84
    public static function legacyManifestTags(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): array
85
    {
86
        // Get the legacy tags for this $path
87
        $parts = pathinfo($path);
88
        $legacyPath = $parts['dirname']
89
            . '/'
90
            . $parts['filename']
91
            . self::LEGACY_EXTENSION
92
            . $parts['extension'];
93
94
        return self::extractManifestTags($legacyPath, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true);
95
    }
96
97
    /**
98
     * Return an array of data describing the  script, module link, and CSS link tags for the
99
     * script from the manifest.json file
100
     *
101
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
102
     * @param bool $asyncCss
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
103
     * @param array $scriptTagAttrs
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
104
     * @param array $cssTagAttrs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
105
     * @param bool $legacy
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
106
     *
107
     * @return array
108
     */
109
    public static function extractManifestTags(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = [], bool $legacy = false): array
110
    {
111
        if (self::$manifest === null) {
112
            return [];
113
        }
114
        $tags = [];
115
        // Set the async CSS args
116
        $asyncCssOptions = [];
117
        if ($asyncCss) {
118
            $asyncCssOptions = [
119
                'media' => 'print',
120
                'onload' => "this.media='all'",
121
            ];
122
        }
123
        // Set the script args
124
        $scriptOptions = [
125
            'type' => 'module',
126
            'crossorigin' => true,
127
        ];
128
        if ($legacy) {
129
            $scriptOptions = [
130
                'type' => 'nomodule',
131
            ];
132
        }
133
        // Iterate through the manifest
134
        foreach (self::$manifest as $manifestKey => $entry) {
135
            // If it's not an entry, skip it
136
            if (!isset($entry['isEntry']) || !$entry['isEntry']) {
137
                continue;
138
            }
139
            // If there's no file, skip it
140
            if (!isset($entry['file'])) {
141
                continue;
142
            }
143
            // If the $path isn't in the $manifestKey, skip it
144
            if (strpos($path, $manifestKey) === false) {
145
                continue;
146
            }
147
            // Include the entry script
148
            $tags[] = [
149
                'type' => 'file',
150
                'url' => $entry['file'],
151
                'options' => array_merge($scriptOptions, $scriptTagAttrs)
152
            ];
153
            // Include any CSS tags
154
            $cssFiles = [];
155
            self::extractCssFiles(self::$manifest, $manifestKey, $cssFiles);
156
            foreach ($cssFiles as $cssFile) {
157
                $tags[] = [
158
                    'type' => 'css',
159
                    'url' => $cssFile,
160
                    'options' => array_merge([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
161
                        'rel' => 'stylesheet',
162
                    ], $asyncCssOptions, $cssTagAttrs)
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
163
                ];
164
            }
165
        }
166
167
        return $tags;
168
    }
169
170
    /**
171
     * Extract any CSS files from entries recursively
172
     *
173
     * @param array $manifest
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
174
     * @param string $manifestKey
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
175
     * @param array $cssFiles
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
176
     *
177
     * @return array
178
     */
179
    protected static function extractCssFiles(array $manifest, string $manifestKey, array &$cssFiles): array
180
    {
181
        $entry = $manifest[$manifestKey] ?? null;
182
        if (!$entry) {
183
            return [];
184
        }
185
        $cssFiles = array_merge($cssFiles, $entry['css'] ?? []);
186
        $imports = array_merge($entry['imports'] ?? [], $entry['dynamicImport'] ?? []);
187
        foreach ($imports as $import) {
188
            self::extractCssFiles($manifest, $import, $cssFiles);
189
        }
190
191
        return $cssFiles;
192
    }
193
}
194