1 | <?php |
||||
2 | /** |
||||
3 | * Vite plugin for Craft CMS |
||||
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
![]() |
|||||
8 | * @copyright Copyright (c) 2021 nystudio107 |
||||
0 ignored issues
–
show
|
|||||
9 | */ |
||||
0 ignored issues
–
show
|
|||||
10 | |||||
11 | namespace nystudio107\vite\services; |
||||
12 | |||||
13 | use Craft; |
||||
14 | use craft\base\Component; |
||||
15 | use craft\helpers\Html; |
||||
16 | use nystudio107\pluginvite\helpers\FileHelper; |
||||
17 | use nystudio107\pluginvite\helpers\ManifestHelper; |
||||
18 | use nystudio107\vite\models\Settings; |
||||
19 | use nystudio107\vite\Vite; |
||||
20 | use Twig\Error\LoaderError; |
||||
21 | |||||
22 | /** |
||||
0 ignored issues
–
show
|
|||||
23 | * @author nystudio107 |
||||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||||
24 | * @package Vite |
||||
0 ignored issues
–
show
|
|||||
25 | * @since 1.0.5 |
||||
0 ignored issues
–
show
|
|||||
26 | */ |
||||
0 ignored issues
–
show
|
|||||
27 | class Helper extends Component |
||||
28 | { |
||||
29 | /** |
||||
30 | * Returns the Critical CSS file for $template wrapped in <style></style> |
||||
31 | * tags |
||||
32 | * |
||||
33 | * @param null|string $name |
||||
0 ignored issues
–
show
|
|||||
34 | * @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
||||
0 ignored issues
–
show
|
|||||
35 | * |
||||
36 | * @return string |
||||
37 | * @throws LoaderError |
||||
38 | */ |
||||
39 | public function getCriticalCssTags(?string $name = null, array $attributes = []): string |
||||
40 | { |
||||
41 | // Resolve the template name |
||||
42 | $template = Craft::$app->getView()->resolveTemplate($name ?? Vite::$templateName ?? ''); |
||||
43 | if ($template) { |
||||
44 | /** @var Settings $settings */ |
||||
0 ignored issues
–
show
|
|||||
45 | $settings = Vite::$plugin->getSettings(); |
||||
46 | $name = FileHelper::createUrl( |
||||
47 | pathinfo($template, PATHINFO_DIRNAME), |
||||
0 ignored issues
–
show
It seems like
pathinfo($template, nyst...vices\PATHINFO_DIRNAME) can also be of type array ; however, parameter $url of nystudio107\pluginvite\h...FileHelper::createUrl() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
48 | pathinfo($template, PATHINFO_FILENAME) |
||||
0 ignored issues
–
show
It seems like
pathinfo($template, nyst...ices\PATHINFO_FILENAME) can also be of type array ; however, parameter $path of nystudio107\pluginvite\h...FileHelper::createUrl() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
49 | ); |
||||
50 | $dirPrefix = 'templates/'; |
||||
51 | if (defined('CRAFT_TEMPLATES_PATH')) { |
||||
52 | $dirPrefix = CRAFT_TEMPLATES_PATH; |
||||
53 | } |
||||
54 | $name = strstr($name, $dirPrefix); |
||||
55 | $pos = strpos($name, $dirPrefix); |
||||
56 | if ($pos !== false) { |
||||
57 | $name = (string)substr_replace($name, '', $pos, strlen($dirPrefix)); |
||||
58 | } |
||||
59 | $path = FileHelper::createUrl( |
||||
60 | $settings->criticalPath, |
||||
0 ignored issues
–
show
|
|||||
61 | $name |
||||
0 ignored issues
–
show
|
|||||
62 | ) . $settings->criticalSuffix; |
||||
0 ignored issues
–
show
|
|||||
63 | |||||
64 | return $this->getCssInlineTags($path, $attributes); |
||||
65 | } |
||||
66 | |||||
67 | return ''; |
||||
68 | } |
||||
69 | |||||
70 | /** |
||||
71 | * Returns the passed in CSS file at the specified file system path or URL, wrapped in |
||||
72 | * <style></style> tags |
||||
73 | * |
||||
74 | * @param string $path |
||||
0 ignored issues
–
show
|
|||||
75 | * @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
||||
0 ignored issues
–
show
|
|||||
76 | * |
||||
77 | * @return string |
||||
78 | */ |
||||
79 | public function getCssInlineTags(string $path, array $attributes = []): string |
||||
80 | { |
||||
81 | /** @var Settings $settings */ |
||||
0 ignored issues
–
show
|
|||||
82 | $settings = Vite::$plugin->getSettings(); |
||||
83 | $result = FileHelper::fetch($path, null, $settings->cacheKeySuffix); |
||||
84 | if ($result) { |
||||
85 | $config = []; |
||||
86 | |||||
87 | return Html::style($result, array_merge($config, $attributes)); |
||||
0 ignored issues
–
show
It seems like
$result can also be of type array ; however, parameter $content of yii\helpers\BaseHtml::style() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
88 | } |
||||
89 | |||||
90 | return ''; |
||||
91 | } |
||||
92 | |||||
93 | /** |
||||
94 | * Return the hash value for the first CSS file bundled with the module specified via $path |
||||
95 | * |
||||
96 | * @param $path |
||||
0 ignored issues
–
show
|
|||||
97 | * @return string |
||||
0 ignored issues
–
show
|
|||||
98 | */ |
||||
99 | public function getCssHash($path): string |
||||
100 | { |
||||
101 | /** @var Settings $settings */ |
||||
0 ignored issues
–
show
|
|||||
102 | $settings = Vite::$plugin->getSettings(); |
||||
103 | ManifestHelper::fetchManifest($settings->manifestPath); |
||||
104 | $tags = ManifestHelper::manifestTags($path, false); |
||||
105 | foreach ($tags as $tag) { |
||||
106 | if (!empty($tag) && $tag['type'] === 'css') { |
||||
107 | // Extract only the Hash Value |
||||
108 | $modulePath = pathinfo($tag['url']); |
||||
109 | $moduleFilename = $modulePath['filename']; |
||||
110 | $hashPos = strpos($moduleFilename, '.') ?: strlen($moduleFilename); |
||||
111 | $moduleHash = substr($moduleFilename, $hashPos + 1); |
||||
112 | // Vite 5 now uses a `-` to separate the version hash, so account for that as well |
||||
113 | if (empty($moduleHash) && str_contains($moduleFilename, '-')) { |
||||
114 | $moduleHash = substr($moduleFilename, strpos($moduleFilename, '-') + 1); |
||||
115 | } |
||||
116 | |||||
117 | return $moduleHash; |
||||
118 | } |
||||
119 | } |
||||
120 | |||||
121 | return ''; |
||||
122 | } |
||||
123 | } |
||||
124 |