Skip to content

Commit b747701

Browse files
authored
Merge pull request #41 from S1SYPHOS/master
Support for renamed assets, configurable digests (SRI) etc pp
2 parents 06c146e + ad76462 commit b747701

File tree

8 files changed

+184
-73
lines changed

8 files changed

+184
-73
lines changed

classes/Fingerprint.php

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,29 @@
55
namespace Bnomei;
66

77
use Exception;
8+
use Kirby\Cms\File;
9+
use Kirby\Cms\FileVersion;
810
use Kirby\Cms\Url;
911
use Kirby\Exception\InvalidArgumentException;
1012
use Kirby\Toolkit\A;
13+
1114
use function array_key_exists;
1215

1316
final class Fingerprint
1417
{
1518
/**
19+
* Plugin options
20+
*
1621
* @var array
1722
*/
1823
private $options;
1924

25+
2026
/**
2127
* Fingerprint constructor.
22-
* @param array $options
28+
*
29+
* @param array $options Plugin options
30+
* @return void
2331
*/
2432
public function __construct(array $options = [])
2533
{
@@ -28,6 +36,7 @@ public function __construct(array $options = [])
2836
'query' => option('bnomei.fingerprint.query'),
2937
'hash' => option('bnomei.fingerprint.hash'),
3038
'integrity' => option('bnomei.fingerprint.integrity'),
39+
'digest' => option('bnomei.fingerprint.digest'),
3140
'https' => option('bnomei.fingerprint.https'),
3241
];
3342
$this->options = array_merge($defaults, $options);
@@ -47,9 +56,12 @@ public function __construct(array $options = [])
4756
}
4857
}
4958

59+
5060
/**
51-
* @param string|null $key
52-
* @return array|mixed
61+
* Gets plugin option by its name
62+
*
63+
* @param string|null $key Option name
64+
* @return mixed
5365
*/
5466
public function option(?string $key = null)
5567
{
@@ -59,18 +71,21 @@ public function option(?string $key = null)
5971
return $this->options;
6072
}
6173

74+
6275
/**
63-
* @param string $option
64-
* @param $file
65-
* @return mixed|null
76+
* Applies hash/integrity functions
77+
*
78+
* @param string $option Plugin option/function name
79+
* @param $file File|FileVersion|string Input file
80+
* @return mixed
6681
*/
6782
public function apply(string $option, $file)
6883
{
6984
$callback = $this->option($option);
7085

7186
if ($callback && is_callable($callback)) {
7287
if ($option === 'integrity') {
73-
return call_user_func_array($callback, [$file]);
88+
return call_user_func_array($callback, [$file, $this->option('digest'), $this->option('query')]);
7489
} elseif ($option === 'hash') {
7590
return call_user_func_array($callback, [$file, $this->option('query')]);
7691
}
@@ -79,10 +94,12 @@ public function apply(string $option, $file)
7994
}
8095

8196
/**
82-
* @param $url
83-
* @return mixed
97+
* Formats URL to use 'https'
98+
*
99+
* @param string $url URL part
100+
* @return string
84101
*/
85-
public function https($url)
102+
public function https(string $url): string
86103
{
87104
if ($this->option('https') && !kirby()->system()->isLocal()) {
88105
$url = str_replace('http://', 'https://', $url);
@@ -91,15 +108,17 @@ public function https($url)
91108
}
92109

93110
/**
94-
* @param $file
111+
* Processes input file
112+
*
113+
* @param File|FileVersion|string $file Input file
95114
* @return mixed
96115
* @throws InvalidArgumentException
97116
*/
98117
public function process($file)
99118
{
100119
$needsPush = false;
101120
$lookup = $this->read();
102-
if (! $lookup) {
121+
if (!$lookup) {
103122
$lookup = [];
104123
$needsPush = true;
105124
}
@@ -108,7 +127,7 @@ public function process($file)
108127
$id = $finFile->id();
109128
$mod = $finFile->modified();
110129

111-
if (! array_key_exists($id, $lookup)) {
130+
if (!array_key_exists($id, $lookup)) {
112131
$needsPush = true;
113132
} elseif ($mod && $lookup[$id]['modified'] < $mod) {
114133
$needsPush = true;
@@ -142,7 +161,7 @@ public function attrs(array $attrs, array $lookup)
142161
if ($sri && strlen($sri) > 0) {
143162
$attrs['integrity'] = $sri;
144163
$attrs['crossorigin'] = A::get($attrs, 'crossorigin', 'anonymous');
145-
} elseif (! $sri) {
164+
} elseif (!$sri) {
146165
if (array_key_exists('integrity', $attrs)) {
147166
unset($attrs['integrity']);
148167
}
@@ -154,14 +173,16 @@ public function attrs(array $attrs, array $lookup)
154173
}
155174

156175
/**
157-
* @param string $extension
158-
* @param string $url
159-
* @param array $attrs
176+
* Helper shorthand
177+
*
178+
* @param string $extension Helper name
179+
* @param string $url URL part
180+
* @param array $attrs HTML attributes
160181
* @return string|null
161182
*/
162183
public function helper(string $extension, string $url, array $attrs = []): ?string
163184
{
164-
if (! is_callable($extension)) {
185+
if (!is_callable($extension)) {
165186
return null;
166187
}
167188

@@ -179,6 +200,8 @@ public function helper(string $extension, string $url, array $attrs = []): ?stri
179200
}
180201

181202
/**
203+
* Retrieves cache key
204+
*
182205
* @return string
183206
*/
184207
public function cacheKey(): string
@@ -191,6 +214,8 @@ public function cacheKey(): string
191214
}
192215

193216
/**
217+
* Retrieves cached data
218+
*
194219
* @return array|null
195220
*/
196221
public function read(): ?array
@@ -202,6 +227,8 @@ public function read(): ?array
202227
}
203228

204229
/**
230+
* Stores data in cache
231+
*
205232
* @param array $lookup
206233
* @return bool
207234
*/
@@ -214,9 +241,11 @@ private function write(array $lookup): bool
214241
}
215242

216243
/**
217-
* @param $url
244+
* Stylesheet helper
245+
*
246+
* @param $url URL part
218247
* @param array $attrs
219-
* @return mixed
248+
* @return string
220249
*/
221250
public static function css($url, $attrs = []): string
222251
{
@@ -228,19 +257,23 @@ public static function css($url, $attrs = []): string
228257
}
229258

230259
/**
231-
* @param $url
232-
* @param array $attrs
233-
* @return mixed
260+
* JavaScript helper
261+
*
262+
* @param $url URL part
263+
* @param array $attrs HTML attributes
264+
* @return string
234265
*/
235266
public static function js($url, $attrs = []): string
236267
{
237268
return (new Fingerprint())->helper('js', $url, $attrs);
238269
}
239270

240271
/**
241-
* @param $url
242-
* @param array $attrs
243-
* @return mixed
272+
* Builds full URL
273+
*
274+
* @param $url URL part
275+
* @param array $attrs HTML attributes
276+
* @return string
244277
*/
245278
public static function url($url): string
246279
{

0 commit comments

Comments
 (0)