From 448b5a801b509edc7e61fc05148701657a107009 Mon Sep 17 00:00:00 2001 From: Bart Vandeputte Date: Thu, 21 Mar 2019 10:58:32 +0100 Subject: [PATCH] Re-use core components as much as possible as per https://github.com/getkirby/ideas/issues/197 --- index.php | 25 ++++++-------- readme.md | 4 +-- src/Fingerprint.php | 80 --------------------------------------------- 3 files changed, 12 insertions(+), 97 deletions(-) diff --git a/index.php b/index.php index ece55ca..72cc6aa 100644 --- a/index.php +++ b/index.php @@ -2,18 +2,13 @@ require __DIR__ . DS . "src" . DS . "Fingerprint.php"; -/* - A little Kirby helper functions -*/ -if (! function_exists("cssfingerprint")) { - function cssfingerprint($url, $options = null) - { - return bvdputte\Fingerprint::css($url, $options = null); - } -} -if (! function_exists("jsfingerprint")) { - function jsfingerprint($url, $options = null) - { - return bvdputte\Fingerprint::js($url, $options = null); - } -} \ No newline at end of file +Kirby::plugin('bvdputte/fingerprint', [ + 'components' => [ + 'css' => function ($kirby, $url, $options) { + return bvdputte\Fingerprint::addHash($url); + }, + 'js' => function ($kirby, $url, $options) { + return bvdputte\Fingerprint::addHash($url); + }, + ] +]); \ No newline at end of file diff --git a/readme.md b/readme.md index 8902bf8..b34f299 100644 --- a/readme.md +++ b/readme.md @@ -23,9 +23,9 @@ RewriteRule ^(.+)\.([0-9a-z]{32})\.(js|css|png|jpe?g|gif|svg|ico)$ $1.$3 [L] ## Usage ```php -cssfingerprint("assets/styles.css"); +css("assets/styles.css"); // Output: -jsfingerprint("assets/scripts.js"); +js("assets/scripts.js"); // Output: ``` diff --git a/src/Fingerprint.php b/src/Fingerprint.php index 0dc79b5..1797ff2 100644 --- a/src/Fingerprint.php +++ b/src/Fingerprint.php @@ -1,7 +1,6 @@ site()->page(); - $path = $assetPath . '/' . $page->template() . '.' . $extension; - $file = $kirby->root('assets') . '/' . $path; - - return file_exists($file) === true ? $file : null; - } - - // Re-write the `css()`-helper - // kirby/config/helpers.php - public static function css($url, $options = null) - { - if (is_array($url) === true) { - $links = array_map(function ($url) use ($options) { - return self::css($url, $options); - }, $url); - return implode(PHP_EOL, $links); - } - - if (is_string($options) === true) { - $options = ['media' => $options]; - } - - $kirby = App::instance(); - - if ($component = $kirby->component('css')) { - $url = $component($kirby, $url, $options); - } - - if ($url === '@auto') { - if (!$url = self::toTemplateAsset('css/templates', 'css')) { - return null; - } - } - - $url = Url::to(self::addHash($url)); - $attr = array_merge((array)$options, [ - 'href' => $url, - 'rel' => 'stylesheet' - ]); - - return ''; - } - - // Re-write the `js()`-helper - // kirby/config/helpers.php - public static function js($url, $options = null) - { - if (is_array($url) === true) { - $scripts = array_map(function ($url) use ($options) { - return self::js($url, $options); - }, $url); - return implode(PHP_EOL, $scripts); - } - - if (is_bool($options) === true) { - $options = ['async' => $options]; - } - - $kirby = App::instance(); - - if ($component = $kirby->component('js')) { - $url = $component($kirby, $url, $options); - } - - if ($url === '@auto') { - if (!$url = self::toTemplateAsset('js/templates', 'js')) { - return null; - } - } - - $url = Url::to(self::addHash($url)); - $attr = array_merge((array)$options, ['src' => $url]); - - return ''; - } } \ No newline at end of file