From aaf6e5aa91c60ff85aa3d0be00606c5921239b17 Mon Sep 17 00:00:00 2001 From: Bart Vandeputte Date: Tue, 12 Mar 2019 09:55:53 +0100 Subject: [PATCH] Add filemethod --- index.php | 14 ++++++++++++++ readme.md | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/index.php b/index.php index ece55ca..9c48950 100644 --- a/index.php +++ b/index.php @@ -2,6 +2,14 @@ require __DIR__ . DS . "src" . DS . "Fingerprint.php"; +Kirby::plugin('bvdputte/fingerprint', [ + 'fileMethods' => [ + 'fingerprint' => function () { + return bvdputte\Fingerprint::addHash($this->root()); + } + ] +]); + /* A little Kirby helper functions */ @@ -16,4 +24,10 @@ function jsfingerprint($url, $options = null) { return bvdputte\Fingerprint::js($url, $options = null); } +} +if (! function_exists("fingerprint")) { + function fingerprint($path) + { + return bvdputte\Fingerprint::addHash($path); + } } \ No newline at end of file diff --git a/readme.md b/readme.md index 64d7b86..2c27e82 100644 --- a/readme.md +++ b/readme.md @@ -9,6 +9,7 @@ When the files are updated, new hashes are added to the filenames automatically; - unzip [master.zip](https://github.com/bvdputte/kirby-fingerprint/archive/master.zip) as folder `site/plugins/kirby-fingerprint` or - `git submodule add https://github.com/bvdputte/kirby-fingerprint.git site/plugins/kirby-fingerprint` +- `composer require bvdputte/kirby-fingerprint` 💡 Add the following to your `.htaccess` file: @@ -28,6 +29,17 @@ jsfingerprint("assets/scripts.js"); // Output: ``` +You can also use this on a `path` or `$file` objects: + +```php +echo fingerprint($page->some_imagefield()->toFile()->root()); +// Output: /var/www/mysite/content/home/testimage.0d859e73e897635c53e59407be9b32aa.jpg +echo $page->some_imagefield()->toFile()->fingerprint(); +// Output: /var/www/mysite/content/home/testimage.0d859e73e897635c53e59407be9b32aa.jpg +``` + +💡 Always test if the file exists first! + ## Advanced features For more advanced features, such as subresource integrity, please checkout [bnomei's kirby3-fingerprint plugin](https://github.com/bnomei/kirby3-fingerprint).