Skip to content

Commit e05ef40

Browse files
committed
✨ added ignore-missing-auto setting, thanks @elyukai, jhercher
closes #32 closes #47 Signed-off-by: Bruno Meilick <[email protected]>
1 parent b74ea2e commit e05ef40

File tree

9 files changed

+914
-811
lines changed

9 files changed

+914
-811
lines changed

classes/Fingerprint.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function __construct(array $options = [])
2525
'integrity' => option('bnomei.fingerprint.integrity'),
2626
'digest' => option('bnomei.fingerprint.digest'),
2727
'https' => option('bnomei.fingerprint.https'),
28+
'ignore-missing-auto' => option('bnomei.fingerprint.ignore-missing-auto'),
2829
];
2930
$this->options = array_merge($defaults, $options);
3031

@@ -140,6 +141,8 @@ public function helper(string $extension, File|FileVersion|string $url, array $a
140141
$assetUrl = Url::toTemplateAsset($extension.'/templates', $extension);
141142
if ($assetUrl) {
142143
$url = $assetUrl;
144+
} elseif (! $assetUrl && $this->option('ignore-missing-auto')) {
145+
return null;
143146
}
144147
}
145148

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bnomei/kirby3-fingerprint",
33
"type": "kirby-plugin",
4-
"version": "5.0.0",
4+
"version": "5.1.0",
55
"description": "File Method and css/js helper to add cache-busting hash and optional Subresource Integrity to file",
66
"license": "MIT",
77
"authors": [

composer.lock

Lines changed: 854 additions & 790 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function url_f(File|FileVersion|string $url): string
4040
'integrity' => function ($file, ?string $digest = null, ?string $manifest = null) {
4141
return (new \Bnomei\FingerprintFile($file))->integrity($digest, $manifest);
4242
},
43+
'ignore-missing-auto' => true,
4344
],
4445
'fileMethods' => [
4546
'fingerprint' => function () {

readme.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,14 @@ The following plugins can do cache busting, but they do not cache the modified t
9999
100100
## Settings
101101
102-
| bnomei.fingerprint. | Default | Description |
103-
|---------------------------|----------------|-------------------------------------------------------------------------------------|
104-
| hash | `callback` | will lead to the hashing logic |
105-
| integrity | `callback` | use it to set option `'integrity' => null,` |
106-
| digest | `'sha384'` | Cryptographic digest to be used for SRI hashes either `'sha256'`, `'sha384'` or `'sha512'`. |
107-
| https | `true` | boolean value or callback to force *https* scheme on all but localhost enviroments. |
108-
| query | `true` or `string` or `callback` | `myfile.js?v={HASH}`, `myfile.{HASH}.js` or loaded from manifest file |
102+
| bnomei.fingerprint. | Default | Description |
103+
|---------------------|----------------------------------|---------------------------------------------------------------------------------------------|
104+
| hash | `callback` | will lead to the hashing logic |
105+
| integrity | `callback` | use it to set option `'integrity' => null,` |
106+
| digest | `'sha384'` | Cryptographic digest to be used for SRI hashes either `'sha256'`, `'sha384'` or `'sha512'`. |
107+
| https | `true` | boolean value or callback to force *https* scheme on all but localhost enviroments. |
108+
| query | `true` or `string` or `callback` | `myfile.js?v={HASH}`, `myfile.{HASH}.js` or loaded from manifest file |
109+
| ignore-missing-auto | `true` or `false` | silently ignore if an asset requested by @auto rule does not exist |
109110
110111
111112
### Query option: true (default)

tests/site/templates/default.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

3-
//echo \Bnomei\Fingerprint::css('/assets/css/style.css', ['async' => true, 'defer' => true, 'data-something' => 'my-value']);
3+
// echo \Bnomei\Fingerprint::css('/assets/css/style.css', ['async' => true, 'defer' => true, 'data-something' => 'my-value']);
44

55
// echo Bnomei\Fingerprint::css('/assets/css/print.css', 'print');

vendor/autoload.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
echo $err;
1515
}
1616
}
17-
trigger_error(
18-
$err,
19-
E_USER_ERROR
20-
);
17+
throw new RuntimeException($err);
2118
}
2219

2320
require_once __DIR__ . '/composer/autoload_real.php';

vendor/composer/InstalledVersions.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,23 @@
2626
*/
2727
class InstalledVersions
2828
{
29+
/**
30+
* @var string|null if set (by reflection by Composer), this should be set to the path where this class is being copied to
31+
* @internal
32+
*/
33+
private static $selfDir = null;
34+
2935
/**
3036
* @var mixed[]|null
3137
* @psalm-var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}|array{}|null
3238
*/
3339
private static $installed;
3440

41+
/**
42+
* @var bool
43+
*/
44+
private static $installedIsLocalDir;
45+
3546
/**
3647
* @var bool|null
3748
*/
@@ -309,6 +320,24 @@ public static function reload($data)
309320
{
310321
self::$installed = $data;
311322
self::$installedByVendor = array();
323+
324+
// when using reload, we disable the duplicate protection to ensure that self::$installed data is
325+
// always returned, but we cannot know whether it comes from the installed.php in __DIR__ or not,
326+
// so we have to assume it does not, and that may result in duplicate data being returned when listing
327+
// all installed packages for example
328+
self::$installedIsLocalDir = false;
329+
}
330+
331+
/**
332+
* @return string
333+
*/
334+
private static function getSelfDir()
335+
{
336+
if (self::$selfDir === null) {
337+
self::$selfDir = strtr(__DIR__, '\\', '/');
338+
}
339+
340+
return self::$selfDir;
312341
}
313342

314343
/**
@@ -322,19 +351,27 @@ private static function getInstalled()
322351
}
323352

324353
$installed = array();
354+
$copiedLocalDir = false;
325355

326356
if (self::$canGetVendors) {
357+
$selfDir = self::getSelfDir();
327358
foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
359+
$vendorDir = strtr($vendorDir, '\\', '/');
328360
if (isset(self::$installedByVendor[$vendorDir])) {
329361
$installed[] = self::$installedByVendor[$vendorDir];
330362
} elseif (is_file($vendorDir.'/composer/installed.php')) {
331363
/** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $required */
332364
$required = require $vendorDir.'/composer/installed.php';
333-
$installed[] = self::$installedByVendor[$vendorDir] = $required;
334-
if (null === self::$installed && strtr($vendorDir.'/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) {
335-
self::$installed = $installed[count($installed) - 1];
365+
self::$installedByVendor[$vendorDir] = $required;
366+
$installed[] = $required;
367+
if (self::$installed === null && $vendorDir.'/composer' === $selfDir) {
368+
self::$installed = $required;
369+
self::$installedIsLocalDir = true;
336370
}
337371
}
372+
if (self::$installedIsLocalDir && $vendorDir.'/composer' === $selfDir) {
373+
$copiedLocalDir = true;
374+
}
338375
}
339376
}
340377

@@ -350,7 +387,7 @@ private static function getInstalled()
350387
}
351388
}
352389

353-
if (self::$installed !== array()) {
390+
if (self::$installed !== array() && !$copiedLocalDir) {
354391
$installed[] = self::$installed;
355392
}
356393

vendor/composer/installed.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php return array(
22
'root' => array(
33
'name' => 'bnomei/kirby3-fingerprint',
4-
'pretty_version' => '5.0.0',
5-
'version' => '5.0.0.0',
4+
'pretty_version' => '5.1.0',
5+
'version' => '5.1.0.0',
66
'reference' => null,
77
'type' => 'kirby-plugin',
88
'install_path' => __DIR__ . '/../../',
@@ -11,8 +11,8 @@
1111
),
1212
'versions' => array(
1313
'bnomei/kirby3-fingerprint' => array(
14-
'pretty_version' => '5.0.0',
15-
'version' => '5.0.0.0',
14+
'pretty_version' => '5.1.0',
15+
'version' => '5.1.0.0',
1616
'reference' => null,
1717
'type' => 'kirby-plugin',
1818
'install_path' => __DIR__ . '/../../',

0 commit comments

Comments
 (0)