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
|
|
|
|
16
|
|
|
use yii\caching\ChainedDependency; |
17
|
|
|
use yii\caching\FileDependency; |
18
|
|
|
use yii\caching\TagDependency; |
19
|
|
|
|
20
|
|
|
use GuzzleHttp\Client; |
21
|
|
|
use GuzzleHttp\RequestOptions; |
22
|
|
|
|
23
|
|
|
use Throwable; |
24
|
|
|
|
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* @author nystudio107 |
|
|
|
|
27
|
|
|
* @package Vite |
|
|
|
|
28
|
|
|
* @since 1.0.5 |
|
|
|
|
29
|
|
|
*/ |
|
|
|
|
30
|
|
|
class FileHelper |
31
|
|
|
{ |
32
|
|
|
// Constants |
33
|
|
|
// ========================================================================= |
34
|
|
|
|
35
|
|
|
const CACHE_KEY = 'vite'; |
36
|
|
|
const CACHE_TAG = 'vite'; |
37
|
|
|
|
38
|
|
|
const DEVMODE_CACHE_DURATION = 30; |
39
|
|
|
|
40
|
|
|
const USER_AGENT_STRING = 'User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Return the contents of a local file (via path) or remote file (via URL), |
44
|
|
|
* or null if the file doesn't exist or couldn't be fetched |
45
|
|
|
* Yii2 aliases and/or environment variables may be used |
46
|
|
|
* |
47
|
|
|
* @param string $pathOrUrl |
|
|
|
|
48
|
|
|
* @param callable|null $callback |
|
|
|
|
49
|
|
|
* @param string $cacheKeySuffix |
|
|
|
|
50
|
|
|
* |
51
|
|
|
* @return string|array|null |
52
|
|
|
*/ |
53
|
|
|
public static function fetch(string $pathOrUrl, callable $callback = null, string $cacheKeySuffix = '') |
54
|
|
|
{ |
55
|
|
|
$pathOrUrl = (string)Craft::parseEnv($pathOrUrl); |
56
|
|
|
// Create the dependency tags |
57
|
|
|
$dependency = new TagDependency([ |
|
|
|
|
58
|
|
|
'tags' => [ |
59
|
|
|
self::CACHE_TAG . $cacheKeySuffix, |
60
|
|
|
self::CACHE_TAG . $cacheKeySuffix . $pathOrUrl, |
61
|
|
|
], |
62
|
|
|
]); |
|
|
|
|
63
|
|
|
// If this is a file path such as for the `manifest.json`, add a FileDependency so it's cache bust if the file changes |
64
|
|
|
if (!UrlHelper::isAbsoluteUrl($pathOrUrl)) { |
65
|
|
|
$dependency = new ChainedDependency([ |
|
|
|
|
66
|
|
|
'dependencies' => [ |
67
|
|
|
new FileDependency([ |
|
|
|
|
68
|
|
|
'fileName' => $pathOrUrl |
69
|
|
|
]), |
|
|
|
|
70
|
|
|
$dependency |
71
|
|
|
] |
72
|
|
|
]); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
// Set the cache duration based on devMode |
75
|
|
|
$cacheDuration = Craft::$app->getConfig()->getGeneral()->devMode |
76
|
|
|
? self::DEVMODE_CACHE_DURATION |
77
|
|
|
: null; |
78
|
|
|
// Get the result from the cache, or parse the file |
79
|
|
|
$cache = Craft::$app->getCache(); |
80
|
|
|
return $cache->getOrSet( |
81
|
|
|
self::CACHE_KEY . $cacheKeySuffix . $pathOrUrl, |
82
|
|
|
function () use ($pathOrUrl, $callback) { |
83
|
|
|
$contents = null; |
84
|
|
|
$result = null; |
85
|
|
|
if (UrlHelper::isAbsoluteUrl($pathOrUrl)) { |
86
|
|
|
// See if we can connect to the server |
87
|
|
|
$clientOptions = [ |
88
|
|
|
RequestOptions::HTTP_ERRORS => false, |
89
|
|
|
RequestOptions::CONNECT_TIMEOUT => 3, |
90
|
|
|
RequestOptions::VERIFY => false, |
91
|
|
|
RequestOptions::TIMEOUT => 5, |
92
|
|
|
]; |
93
|
|
|
$client = new Client($clientOptions); |
94
|
|
|
try { |
95
|
|
|
$response = $client->request('GET', $pathOrUrl, [ |
|
|
|
|
96
|
|
|
RequestOptions::HEADERS => [ |
97
|
|
|
'User-Agent' => self::USER_AGENT_STRING, |
98
|
|
|
'Accept' => '*/*', |
99
|
|
|
], |
100
|
|
|
]); |
|
|
|
|
101
|
|
|
if ($response->getStatusCode() === 200) { |
102
|
|
|
$contents = $response->getBody()->getContents(); |
103
|
|
|
} |
104
|
|
|
} catch (Throwable $e) { |
105
|
|
|
Craft::error($e, __METHOD__); |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
|
|
$contents = @file_get_contents($pathOrUrl); |
109
|
|
|
} |
110
|
|
|
if ($contents) { |
111
|
|
|
$result = $contents; |
112
|
|
|
if ($callback) { |
113
|
|
|
$result = $callback($result); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $result; |
118
|
|
|
}, |
119
|
|
|
$cacheDuration, |
120
|
|
|
$dependency |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|