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