Passed
Push — v1 ( 270350...b42adf )
by Andrew
09:36 queued 04:37
created

ViteService::entry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\pluginvite\services;
12
13
use nystudio107\pluginvite\helpers\FileHelper;
14
use nystudio107\pluginvite\helpers\ManifestHelper;
15
16
use Craft;
17
use craft\base\Component;
18
use craft\helpers\Html as HtmlHelper;
19
use craft\helpers\Json as JsonHelper;
20
use craft\web\View;
21
22
use yii\base\InvalidConfigException;
23
use yii\caching\TagDependency;
24
25
use Throwable;
26
27
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
28
 * @author    nystudio107
0 ignored issues
show
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...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
29
 * @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...
30
 * @since     1.0.0
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...
31
 */
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...
32
class ViteService extends Component
33
{
34
    // Constants
35
    // =========================================================================
36
37
    const VITE_CLIENT = '@vite/client';
38
    const LEGACY_POLYFILLS = 'vite/legacy-polyfills';
39
40
    // Public Properties
41
    // =========================================================================
42
43
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
44
     * @var bool Should the dev server be used for?
45
     */
46
    public $useDevServer;
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @var string File system path (or URL) to the Vite-built manifest.json
50
     */
51
    public $manifestPath;
52
53
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
54
     * @var string The public URL to the dev server (what appears in `<script src="">` tags
55
     */
56
    public $devServerPublic;
57
58
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
59
     * @var string The public URL to use when not using the dev server
60
     */
61
    public $serverPublic;
62
63
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
64
     * @var string The JavaScript entry from the manifest.json to inject on Twig error pages
65
     *              This can be a string or an array of strings
66
     */
67
    public $errorEntry = '';
68
69
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
70
     * @var string String to be appended to the cache key
71
     */
72
    public $cacheKeySuffix = '';
73
74
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
75
     * @var string The internal URL to the dev server, when accessed from the environment in which PHP is executing
76
     *              This can be the same as `$devServerPublic`, but may be different in containerized or VM setups.
77
     *              ONLY used if $checkDevServer = true
78
     */
79
    public $devServerInternal;
80
81
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
82
     * @var bool Should we check for the presence of the dev server by pinging $devServerInternal to make sure it's running?
83
     */
84
    public $checkDevServer = false;
85
86
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
87
     * @var bool Whether the react-refresh-shim should be included
88
     */
89
    public $includeReactRefreshShim = false;
90
91
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
92
     * @var bool Whether the modulepreload-polyfill shim should be included
93
     */
94
    public $includeModulePreloadShim = true;
95
96
    // Protected Properties
97
    // =========================================================================
98
99
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
100
     * @var bool Whether the manifest shims has been included yet or not
101
     */
102
    protected $manifestShimsIncluded = false;
103
104
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
105
     * @var bool Whether the dev server shims has been included yet or not
106
     */
107
    protected $devServerShimsIncluded = false;
108
109
    // Public Methods
110
    // =========================================================================
111
112
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
113
     * @inheritDoc
114
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
115
    public function init()
116
    {
117
        parent::init();
118
        // Do nothing for console requests
119
        $request = Craft::$app->getRequest();
120
        if ($request->getIsConsoleRequest()) {
121
            return;
122
        }
123
        // Our component is lazily loaded, so the View will be instantiated by now
124
        if (Craft::$app->getConfig()->getGeneral()->devMode) {
125
            Craft::$app->getView()->on(View::EVENT_END_BODY, [$this, 'injectErrorEntry']);
126
        }
127
    }
128
129
    /**
130
     * Return the appropriate tags to load the Vite script, either via the dev server or
131
     * extracting it from the manifest.json file
132
     *
133
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
134
     * @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...
135
     * @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...
136
     * @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...
137
     *
138
     * @return string
139
     */
140
    public function script(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string
141
    {
142
        if ($this->devServerRunning()) {
143
            return $this->devServerScript($path, $scriptTagAttrs);
144
        }
145
146
        return $this->manifestScript($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
147
    }
148
149
    /**
150
     * Return the script tag to load the script from the Vite dev server
151
     *
152
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
153
     * @param array $scriptTagAttrs
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...
154
     *
155
     * @return string
156
     */
157
    public function devServerScript(string $path, array $scriptTagAttrs = []): string
158
    {
159
        $lines = [];
160
        // Include any dev server shims
161
        if (!$this->devServerShimsIncluded) {
162
            // Include the react-refresh-shim
163
            if ($this->includeReactRefreshShim) {
164
                $script = FileHelper::fetchScript('react-refresh-shim.min.js', $this->cacheKeySuffix);
165
                // Replace the hard-coded dev server URL with whatever they have theirs set to
166
                $script = str_replace(
167
                    'http://localhost:3000/',
168
                    rtrim($this->devServerPublic, '/') . '/',
169
                    $script
170
                );
171
                $lines[] = HtmlHelper::script(
172
                    $script,
173
                    [
174
                        'type' => 'module',
175
                    ]
176
                );
177
            }
178
            $this->devServerShimsIncluded = true;
179
        }
180
        // Include the entry script
181
        $url = FileHelper::createUrl($this->devServerPublic, $path);
182
        $lines[] = HtmlHelper::jsFile($url, 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...
183
            'type' => 'module',
184
        ], $scriptTagAttrs));
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...
185
186
        return implode("\r\n", $lines);
187
    }
188
189
    /**
190
     * Return the script, module link, and CSS link tags for the script from the manifest.json file
191
     *
192
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
193
     * @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...
194
     * @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...
195
     * @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...
196
     *
197
     * @return string
198
     */
199
    public function manifestScript(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string
200
    {
201
        $lines = [];
202
        ManifestHelper::fetchManifest($this->manifestPath);
203
        $tags = ManifestHelper::manifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
204
        $legacyTags = ManifestHelper::legacyManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
205
        // Include any manifest shims
206
        if (!$this->manifestShimsIncluded) {
207
            // Handle the modulepreload-polyfill shim
208
            if ($this->includeModulePreloadShim) {
209
                $lines[] = HtmlHelper::script(
210
                    FileHelper::fetchScript('modulepreload-polyfill.min.js', $this->cacheKeySuffix),
211
                    ['type' => 'module']
212
                );
213
            }
214
            // Handle any legacy polyfills
215
            if (!empty($legacyTags)) {
216
                $lines[] = HtmlHelper::script(
217
                    FileHelper::fetchScript('safari-nomodule-fix.min.js', $this->cacheKeySuffix),
218
                    []
219
                );
220
                $legacyPolyfillTags = ManifestHelper::extractManifestTags(self::LEGACY_POLYFILLS, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true);
221
                $tags = array_merge($legacyPolyfillTags, $tags);
222
            }
223
            $this->manifestShimsIncluded = true;
224
        }
225
        $lines = array_merge($lines, $this->manifestScriptTags($tags, $legacyTags));
226
227
        return implode("\r\n", $lines);
228
    }
229
230
    /**
231
     * Register the appropriate tags to the Craft View to load the Vite script, either via the dev server or
232
     * extracting it from the manifest.json file
233
     *
234
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
235
     * @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...
236
     * @param array $scriptTagAttrs
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...
237
     * @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...
238
     *
239
     * @return void
240
     * @throws InvalidConfigException
241
     */
242
    public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = [])
243
    {
244
        if ($this->devServerRunning()) {
245
            $this->devServerRegister($path, $scriptTagAttrs);
246
247
            return;
248
        }
249
250
        $this->manifestRegister($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
251
    }
252
253
    /**
254
     * Register the script tag to the Craft View to load the script from the Vite dev server
255
     *
256
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
257
     * @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...
258
     *
259
     * @return void
260
     * @throws InvalidConfigException
261
     */
262
    public function devServerRegister(string $path, array $scriptTagAttrs = [])
263
    {
264
        $view = Craft::$app->getView();
265
        // Include any dev server shims
266
        if (!$this->devServerShimsIncluded) {
267
            // Include the react-refresh-shim
268
            if ($this->includeReactRefreshShim) {
269
                $script = FileHelper::fetchScript('react-refresh-shim.min.js', $this->cacheKeySuffix);
270
                // Replace the hard-coded dev server URL with whatever they have theirs set to
271
                $script = str_replace(
272
                    'http://localhost:3000/',
273
                    rtrim($this->devServerPublic, '/') . '/',
274
                    $script
275
                );
276
                $view->registerScript(
277
                    $script,
278
                    $view::POS_HEAD,
279
                    [
280
                        'type' => 'module',
281
                    ],
282
                    'REACT_REFRESH_SHIM'
283
                );
284
            }
285
            $this->devServerShimsIncluded = true;
286
        }
287
        // Include the entry script
288
        $url = FileHelper::createUrl($this->devServerPublic, $path);
289
        $view->registerJsFile(
290
            $url,
291
            array_merge(['type' => 'module'], $scriptTagAttrs),
292
            md5($url . JsonHelper::encode($scriptTagAttrs))
293
        );
294
    }
295
296
    /**
297
     * Register the script, module link, and CSS link tags to the Craft View for the script from the manifest.json file
298
     *
299
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
300
     * @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...
301
     * @param array $scriptTagAttrs
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...
302
     * @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...
303
     *
304
     * @return void
305
     * @throws InvalidConfigException
306
     */
307
    public function manifestRegister(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = [])
308
    {
309
        $view = Craft::$app->getView();
310
        ManifestHelper::fetchManifest($this->manifestPath);
311
        $tags = ManifestHelper::manifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
312
        $legacyTags = ManifestHelper::legacyManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
313
        // Include any manifest shims
314
        if (!$this->manifestShimsIncluded) {
315
            // Handle the modulepreload-polyfill shim
316
            if ($this->includeModulePreloadShim) {
317
                $view->registerScript(
318
                    FileHelper::fetchScript('modulepreload-polyfill.min.js', $this->cacheKeySuffix),
319
                    $view::POS_HEAD,
320
                    ['type' => 'module'],
321
                    'MODULEPRELOAD_POLYFILL'
322
                );
323
            }
324
            // Handle any legacy polyfills
325
            if (!empty($legacyTags)) {
326
                $view->registerScript(
327
                    FileHelper::fetchScript('safari-nomodule-fix.min.js', $this->cacheKeySuffix),
328
                    $view::POS_HEAD,
329
                    [],
330
                    'SAFARI_NOMODULE_FIX'
331
                );
332
                $legacyPolyfillTags = ManifestHelper::extractManifestTags(self::LEGACY_POLYFILLS, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true);
333
                $tags = array_merge($legacyPolyfillTags, $tags);
334
            }
335
            $this->manifestShimsIncluded = true;
336
        }
337
        $this->manifestRegisterTags($tags, $legacyTags);
338
    }
339
340
    /**
341
     * Return the URL for the given entry
342
     *
343
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
344
     *
345
     * @return string
346
     */
347
    public function entry(string $path): string
348
    {
349
        ManifestHelper::fetchManifest($this->manifestPath);
350
        $entry = ManifestHelper::extractEntry($path);
351
352
        return FileHelper::createUrl($this->serverPublic, $entry);
353
    }
354
355
    /**
356
     * Return the URL for the given asset
357
     *
358
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
359
     *
360
     * @return string
361
     */
362
    public function asset(string $path): string
363
    {
364
        if ($this->devServerRunning()) {
365
            return $this->devServerAsset($path);
366
        }
367
368
        return $this->manifestAsset($path);
369
    }
370
371
    /**
372
     * Return the URL for the asset from the Vite dev server
373
     *
374
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
375
     *
376
     * @return string
377
     */
378
    public function devServerAsset(string $path): string
379
    {
380
        // Return a URL to the given asset
381
        return FileHelper::createUrl($this->devServerPublic, $path);
382
    }
383
384
    /**
385
     * Return the URL for the asset from the manifest.json file
386
     *
387
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
388
     *
389
     * @return string
390
     */
391
    public function manifestAsset(string $path): string
392
    {
393
        ManifestHelper::fetchManifest($this->manifestPath);
394
        $assets = ManifestHelper::extractAssetFiles();
395
        // Get just the file name
396
        $assetKeyParts = explode('/', $path);
397
        $assetKey = end($assetKeyParts);
398
        foreach($assets as $key => $value) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
399
            if ($key === $assetKey) {
400
                return FileHelper::createUrl($this->serverPublic, $value);
401
            }
402
        }
403
404
        return '';
405
    }
406
407
    /**
408
     * Return the contents of a local file (via path) or remote file (via URL),
409
     * or null if the file doesn't exist or couldn't be fetched
410
     * Yii2 aliases and/or environment variables may be used
411
     *
412
     * @param string $pathOrUrl
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
413
     * @param callable|null $callback
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
414
     *
415
     * @return string|array|null
416
     */
417
    public function fetch(string $pathOrUrl, callable $callback = null)
418
    {
419
        return FileHelper::fetch($pathOrUrl, $callback, $this->cacheKeySuffix);
420
    }
421
422
    /**
423
     * Determine whether the Vite dev server is running
424
     *
425
     * @return bool
426
     */
427
    public function devServerRunning(): bool
428
    {
429
        // If the dev server is turned off via config, say it's not running
430
        if (!$this->useDevServer) {
431
            return false;
432
        }
433
        // If we're not supposed to check that the dev server is actually running, just assume it is
434
        if (!$this->checkDevServer) {
435
            return true;
436
        }
437
        // Check to see if the dev server is actually running by pinging it
438
        $url = FileHelper::createUrl($this->devServerInternal, self::VITE_CLIENT);
439
440
        return !($this->fetch($url) === null);
441
    }
442
443
    /**
444
     * Invalidate all of the Vite caches
445
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
446
    public function invalidateCaches()
447
    {
448
        $cache = Craft::$app->getCache();
449
        TagDependency::invalidate($cache, FileHelper::CACHE_TAG . $this->cacheKeySuffix);
450
        Craft::info('All Vite caches cleared', __METHOD__);
451
    }
452
453
    // Protected Methods
454
    // =========================================================================
455
456
    /**
457
     * Inject the error entry point JavaScript for auto-reloading of Twig error
458
     * pages
459
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
460
    protected function injectErrorEntry()
461
    {
462
        // If there's no error entry provided, return
463
        if (empty($this->errorEntry)) {
464
            return;
465
        }
466
        // If it's not a server error or a client error, return
467
        $response = Craft::$app->getResponse();
468
        if (!($response->isServerError || $response->isClientError)) {
469
            return;
470
        }
471
        // If the dev server isn't running, return
472
        if (!$this->devServerRunning()) {
473
            return;
474
        }
475
        // Inject the errorEntry script tags to enable HMR on this page
476
        try {
477
            $errorEntry = $this->errorEntry;
478
            if (is_string($errorEntry)) {
0 ignored issues
show
introduced by
The condition is_string($errorEntry) is always true.
Loading history...
479
                $errorEntry = [$errorEntry];
480
            }
481
            foreach ($errorEntry as $entry) {
482
                $tag = $this->script($entry);
483
                if ($tag !== null) {
484
                    echo $tag;
485
                }
486
            }
487
        } catch (Throwable $e) {
488
            // That's okay, Vite will have already logged the error
489
        }
490
    }
491
492
493
    /**
494
     * Iterate through all the tags, and return them
495
     *
496
     * @param array $tags
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
497
     * @param array $legacyTags
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
498
     * @return array
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
499
     */
500
    protected function manifestScriptTags(array $tags, array $legacyTags): array
501
    {
502
        $lines = [];
503
        foreach (array_merge($tags, $legacyTags) as $tag) {
504
            if (!empty($tag)) {
505
                $url = FileHelper::createUrl($this->serverPublic, $tag['url']);
506
                switch ($tag['type']) {
507
                    case 'file':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
508
                        $lines[] = HtmlHelper::jsFile($url, $tag['options']);
509
                        break;
510
                    case 'css':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
511
                        $lines[] = HtmlHelper::cssFile($url, $tag['options']);
512
                        break;
513
                    case 'import':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
514
                        $lines[] = HtmlHelper::tag('link', '', [
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...
515
                            'crossorigin' => $tag['crossorigin'],
516
                            'href' => $url,
517
                            'rel' => 'modulepreload',
518
                        ]);
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...
519
                        break;
520
                    default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
521
                        break;
522
                }
523
            }
524
        }
525
526
        return $lines;
527
    }
528
529
    /**
530
     * Iterate through all the tags, and register them
531
     *
532
     * @param array $tags
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
533
     * @param array $legacyTags
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
534
     * @throws InvalidConfigException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
535
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
536
    protected function manifestRegisterTags(array $tags, array $legacyTags)
537
    {
538
        $view = Craft::$app->getView();
539
        foreach (array_merge($tags, $legacyTags) as $tag) {
540
            if (!empty($tag)) {
541
                $url = FileHelper::createUrl($this->serverPublic, $tag['url']);
542
                switch ($tag['type']) {
543
                    case 'file':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
544
                        $view->registerJsFile(
545
                            $url,
546
                            $tag['options'],
547
                            md5($url . JsonHelper::encode($tag['options']))
548
                        );
549
                        break;
550
                    case 'css':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
551
                        $view->registerCssFile(
552
                            $url,
553
                            $tag['options']
554
                        );
555
                        break;
556
                    case 'import':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
557
                        $view->registerLinkTag(
558
                            [
559
                                'crossorigin' => $tag['crossorigin'],
560
                                'href' => $url,
561
                                'rel' => 'modulepreload',
562
                            ],
563
                            md5($url)
564
                        );
565
                        break;
566
                    default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
567
                        break;
568
                }
569
            }
570
        }
571
    }
572
}
573