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 |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2021 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\vite\services; |
12
|
|
|
|
13
|
|
|
use nystudio107\vite\Vite; |
14
|
|
|
use nystudio107\vite\models\Settings; |
15
|
|
|
|
16
|
|
|
use nystudio107\pluginvite\helpers\FileHelper; |
17
|
|
|
|
18
|
|
|
use Craft; |
19
|
|
|
use craft\base\Component; |
20
|
|
|
use craft\helpers\Html; |
21
|
|
|
|
22
|
|
|
use Twig\Error\LoaderError; |
23
|
|
|
|
24
|
|
|
/** |
|
|
|
|
25
|
|
|
* @author nystudio107 |
|
|
|
|
26
|
|
|
* @package Vite |
|
|
|
|
27
|
|
|
* @since 1.0.5 |
|
|
|
|
28
|
|
|
*/ |
|
|
|
|
29
|
|
|
class Helper extends Component |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Returns the Critical CSS file for $template wrapped in <style></style> |
33
|
|
|
* tags |
34
|
|
|
* |
35
|
|
|
* @param null|string $name |
|
|
|
|
36
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
|
|
|
|
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
* @throws LoaderError |
40
|
|
|
*/ |
41
|
|
|
public function getCriticalCssTags($name = null, array $attributes = []): string |
42
|
|
|
{ |
43
|
|
|
// Resolve the template name |
44
|
|
|
$template = Craft::$app->getView()->resolveTemplate($name ?? Vite::$templateName ?? ''); |
45
|
|
|
if ($template) { |
46
|
|
|
/** @var Settings $settings */ |
|
|
|
|
47
|
|
|
$settings = Vite::$plugin->getSettings(); |
48
|
|
|
$name = FileHelper::createUrl( |
49
|
|
|
pathinfo($template, PATHINFO_DIRNAME), |
|
|
|
|
50
|
|
|
pathinfo($template, PATHINFO_FILENAME) |
|
|
|
|
51
|
|
|
); |
52
|
|
|
$dirPrefix = 'templates/'; |
53
|
|
|
if (defined('CRAFT_TEMPLATES_PATH')) { |
54
|
|
|
$dirPrefix = CRAFT_TEMPLATES_PATH; |
55
|
|
|
} |
56
|
|
|
$name = strstr($name, $dirPrefix); |
57
|
|
|
$name = (string)str_replace($dirPrefix, '', $name); |
58
|
|
|
$path = FileHelper::createUrl( |
59
|
|
|
$settings->criticalPath, |
|
|
|
|
60
|
|
|
$name |
|
|
|
|
61
|
|
|
) . $settings->criticalSuffix; |
|
|
|
|
62
|
|
|
|
63
|
|
|
return $this->getCssInlineTags($path, $attributes); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return ''; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
|
|
|
|
70
|
|
|
* @param string $path |
|
|
|
|
71
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
|
|
|
|
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getCssInlineTags(string $path, array $attributes = []): string |
76
|
|
|
{ |
77
|
|
|
/** @var Settings $settings */ |
|
|
|
|
78
|
|
|
$settings = Vite::$plugin->getSettings(); |
79
|
|
|
$result = FileHelper::fetch($path, null, $settings->cacheKeySuffix); |
80
|
|
|
if ($result) { |
81
|
|
|
$config = []; |
82
|
|
|
|
83
|
|
|
return Html::style($result, array_merge($config, $attributes)); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return ''; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|