Skip to content

Commit

Permalink
Re-use core components as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Vandeputte committed Mar 21, 2019
1 parent 040a799 commit 448b5a8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 97 deletions.
25 changes: 10 additions & 15 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
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);
},
]
]);
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <link href="//localhost:3000/assets/css/styles.db5796ea5bf253bb7be3526eb083e068.css" rel="stylesheet">
jsfingerprint("assets/scripts.js");
js("assets/scripts.js");
// Output: <script src="//localhost:3000/assets/js/scripts.1e9dd0c95e7b12ce96729501c7585deb.js"></script>
```

Expand Down
80 changes: 0 additions & 80 deletions src/Fingerprint.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

namespace bvdputte;
use Kirby\Cms\App;
use Kirby\Cms\Url;

class Fingerprint
Expand All @@ -20,83 +19,4 @@ public static function addHash($path)

return $pathinfo['dirname'] . DS . $basename;
}

private static function toTemplateAsset(string $assetPath, string $extension)
{
$kirby = App::instance();
$page = $kirby->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 '<link ' . attr($attr) . '>';
}

// 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 '<script ' . attr($attr) . '></script>';
}
}

0 comments on commit 448b5a8

Please sign in to comment.