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 |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2021 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\pluginvite\helpers; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\helpers\FileHelper as CraftFileHelper; |
15
|
|
|
use craft\helpers\UrlHelper; |
16
|
|
|
use GuzzleHttp\Client; |
17
|
|
|
use GuzzleHttp\RequestOptions; |
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
19
|
|
|
use Throwable; |
20
|
|
|
use yii\caching\ChainedDependency; |
21
|
|
|
use yii\caching\FileDependency; |
22
|
|
|
use yii\caching\TagDependency; |
23
|
|
|
|
24
|
|
|
/** |
|
|
|
|
25
|
|
|
* @author nystudio107 |
|
|
|
|
26
|
|
|
* @package Vite |
|
|
|
|
27
|
|
|
* @since 1.0.5 |
|
|
|
|
28
|
|
|
*/ |
|
|
|
|
29
|
|
|
class FileHelper |
30
|
|
|
{ |
31
|
|
|
// Constants |
32
|
|
|
// ========================================================================= |
33
|
|
|
|
34
|
|
|
public const CACHE_KEY = 'vite'; |
35
|
|
|
public const CACHE_TAG = 'vite'; |
36
|
|
|
|
37
|
|
|
public const DEVMODE_CACHE_DURATION = 1; |
38
|
|
|
|
39
|
|
|
public const SCRIPTS_DIR = '@vendor/nystudio107/craft-plugin-vite/src/web/assets/dist/'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Return the contents of a local file (via path) or remote file (via URL), |
43
|
|
|
* or null if the file doesn't exist or couldn't be fetched |
44
|
|
|
* Yii2 aliases and/or environment variables may be used |
45
|
|
|
* |
46
|
|
|
* @param string $pathOrUrl |
|
|
|
|
47
|
|
|
* @param callable|null $callback |
|
|
|
|
48
|
|
|
* @param string $cacheKeySuffix |
|
|
|
|
49
|
|
|
* |
50
|
|
|
* @return string|array|null |
51
|
|
|
*/ |
52
|
|
|
public static function fetch(string $pathOrUrl, callable $callback = null, string $cacheKeySuffix = '') |
53
|
|
|
{ |
54
|
|
|
$pathOrUrl = (string)Craft::parseEnv($pathOrUrl); |
|
|
|
|
55
|
|
|
// Create the dependency tags |
56
|
|
|
$dependency = new TagDependency([ |
|
|
|
|
57
|
|
|
'tags' => [ |
58
|
|
|
self::CACHE_TAG . $cacheKeySuffix, |
59
|
|
|
self::CACHE_TAG . $cacheKeySuffix . $pathOrUrl, |
60
|
|
|
], |
61
|
|
|
]); |
|
|
|
|
62
|
|
|
// If this is a file path such as for the `manifest.json`, add a FileDependency so it's cache bust if the file changes |
63
|
|
|
if (!UrlHelper::isAbsoluteUrl($pathOrUrl)) { |
64
|
|
|
$dependency = new ChainedDependency([ |
|
|
|
|
65
|
|
|
'dependencies' => [ |
66
|
|
|
new FileDependency([ |
|
|
|
|
67
|
|
|
'fileName' => $pathOrUrl, |
68
|
|
|
]), |
|
|
|
|
69
|
|
|
$dependency, |
70
|
|
|
], |
71
|
|
|
]); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
// Set the cache duration based on devMode |
74
|
|
|
$cacheDuration = Craft::$app->getConfig()->getGeneral()->devMode |
75
|
|
|
? self::DEVMODE_CACHE_DURATION |
76
|
|
|
: null; |
77
|
|
|
// Get the result from the cache, or parse the file |
78
|
|
|
$cache = Craft::$app->getCache(); |
79
|
|
|
return $cache->getOrSet( |
80
|
|
|
self::CACHE_KEY . $cacheKeySuffix . $pathOrUrl, |
81
|
|
|
function() use ($pathOrUrl, $callback) { |
|
|
|
|
82
|
|
|
$contents = null; |
83
|
|
|
if (UrlHelper::isAbsoluteUrl($pathOrUrl)) { |
84
|
|
|
$response = self::fetchResponse($pathOrUrl); |
85
|
|
|
if ($response && $response->getStatusCode() === 200) { |
86
|
|
|
$contents = $response->getBody()->getContents(); |
87
|
|
|
} |
88
|
|
|
} else { |
89
|
|
|
// If this is a file path, normalize it first |
90
|
|
|
$pathOrUrl = CraftFileHelper::normalizePath($pathOrUrl); |
91
|
|
|
$contents = @file_get_contents($pathOrUrl); |
92
|
|
|
} |
93
|
|
|
if ($contents && $callback) { |
94
|
|
|
$contents = $callback($contents); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $contents; |
98
|
|
|
}, |
99
|
|
|
$cacheDuration, |
100
|
|
|
$dependency |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return a Guzzle ResponseInterface for the passed in $url |
106
|
|
|
* |
107
|
|
|
* @param string $url |
|
|
|
|
108
|
|
|
* @return ResponseInterface|null |
|
|
|
|
109
|
|
|
*/ |
110
|
|
|
public static function fetchResponse(string $url): ?ResponseInterface |
111
|
|
|
{ |
112
|
|
|
$response = null; |
|
|
|
|
113
|
|
|
$clientOptions = [ |
114
|
|
|
RequestOptions::HTTP_ERRORS => false, |
115
|
|
|
RequestOptions::CONNECT_TIMEOUT => 3, |
116
|
|
|
RequestOptions::VERIFY => false, |
117
|
|
|
RequestOptions::TIMEOUT => 5, |
118
|
|
|
]; |
119
|
|
|
$client = new Client($clientOptions); |
120
|
|
|
try { |
121
|
|
|
$response = $client->request('GET', $url, [ |
|
|
|
|
122
|
|
|
RequestOptions::HEADERS => [ |
123
|
|
|
'Accept' => '*/*', |
124
|
|
|
], |
125
|
|
|
]); |
|
|
|
|
126
|
|
|
} catch (Throwable $e) { |
127
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $response; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Combine a path with a URL to create a URL |
135
|
|
|
* |
136
|
|
|
* @param string $url |
|
|
|
|
137
|
|
|
* @param string $path |
|
|
|
|
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public static function createUrl(string $url, string $path): string |
142
|
|
|
{ |
143
|
|
|
$url = (string)Craft::parseEnv($url); |
|
|
|
|
144
|
|
|
return rtrim($url, '/') . '/' . trim($path, '/'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Fetch a script file |
149
|
|
|
* |
150
|
|
|
* @param string $name |
|
|
|
|
151
|
|
|
* @param string $cacheKeySuffix |
|
|
|
|
152
|
|
|
* @return string |
|
|
|
|
153
|
|
|
*/ |
154
|
|
|
public static function fetchScript(string $name, string $cacheKeySuffix = ''): string |
155
|
|
|
{ |
156
|
|
|
$path = self::createUrl(self::SCRIPTS_DIR, $name); |
157
|
|
|
|
158
|
|
|
return self::fetch($path, null, $cacheKeySuffix) ?? ''; |
|
|
|
|
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|