Skip to content

Commit

Permalink
Add filemethod
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Vandeputte committed Mar 12, 2019
1 parent d574b2d commit aaf6e5a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
14 changes: 14 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
}
}
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -28,6 +29,17 @@ jsfingerprint("assets/scripts.js");
// Output: <script src="//localhost:3000/assets/js/scripts.1e9dd0c95e7b12ce96729501c7585deb.js"></script>
```

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).
Expand Down

0 comments on commit aaf6e5a

Please sign in to comment.