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