Skip to content

Commit a41992b

Browse files
committed
Refactor
1 parent 2519ca7 commit a41992b

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This plugin is free but if you use it in a commercial project please consider to
1111

1212
## Requirements
1313

14-
- PHP 7.3+
14+
- PHP 8.0+
1515
- Kirby 3
1616

1717
## Installation
@@ -74,7 +74,7 @@ mix
7474
| --------------------- | ------------------- | --------------------------------------------------- |
7575
| `mrfd.mix.enable` | `true` | Activated or deactivated the Kirby Mix plugin. |
7676
| `mrfd.mix.manifest` | `mix-manifest.json` | File name including extension of the manifest file. |
77-
| `mrfd.mix.publicpath` | `assets` | The public path to the assets folder. |
77+
| `mrfd.mix.publicPath` | `assets` | The public path to the assets folder. |
7878

7979
## Disclaimer
8080

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "mrfd/kirby-mix",
33
"description": "Laravel Mix integration for Kirby 3, which hooks into the existing helper functions.",
44
"type": "kirby-plugin",
5-
"version": "1.0.2",
5+
"version": "1.0.3",
66
"support": {
77
"issues": "https://github.com/mrfd/kirby-mix/issues",
88
"source": "https://github.com/mrfd/kirby-mix",

index.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,66 @@
11
<?php
22

33
use Kirby\Cms\Url;
4+
use Kirby\Data\Json;
5+
use Kirby\Toolkit\A;
6+
use Kirby\Toolkit\Str;
7+
use Kirby\Filesystem\F;
8+
use Kirby\Cms\App as Kirby;
49

510
function mixManifest(Kirby $kirby): array
611
{
7-
$manifestFile = $kirby->root('index') . DS . option('mrfd.mix.publicpath') . DS . option('mrfd.mix.manifest');
12+
$manifestFile = $kirby->root('index') . '/' . option('mrfd.mix.publicPath') . '/' . option('mrfd.mix.manifest');
813
$manifest = [];
914

10-
if (file_exists($manifestFile)) {
11-
$manifest = json_decode(file_get_contents($manifestFile, true), true);
15+
if ($file = F::read($manifestFile)) {
16+
$manifest = Json::decode($file);
1217
}
1318

1419
return $manifest;
1520
}
1621

1722
function getFromManifest(Kirby $kirby, string $url): string
1823
{
19-
$publicPath = option('mrfd.mix.publicpath');
20-
$url = str_replace($publicPath, '', Url::path($url, false));
24+
$publicPath = option('mrfd.mix.publicPath');
2125
$manifest = mixManifest($kirby);
2226

23-
return DS . $publicPath . ($manifest[$url] ?? $url);
27+
$file = Str::replace(Url::path($url, false), $publicPath, '');
28+
$fileVersion = A::get($manifest, $file, false);
29+
30+
if ($fileVersion === false) {
31+
return $url;
32+
}
33+
34+
return '/' . $publicPath . $fileVersion;
2435
}
2536

2637
function isInternalUrl(Kirby $kirby, string $url): bool
2738
{
2839
$url = Url::to($url);
2940

30-
return strpos($url, $kirby->site()->url()) !== false || strpos($url, "/") === '0';
41+
return \strpos($url, $kirby->site()->url()) !== false || \strpos($url, "/") === '0';
3142
}
3243

3344

3445
Kirby::plugin('mrfd/mix', [
3546
'components' => [
3647
'css' => function (Kirby $kirby, string $url, $options = null): string {
37-
if (!option('mrfd.mix.enable')) {
48+
if (option('mrfd.mix.enable') === false) {
3849
return $url;
3950
}
4051

41-
if (!isInternalUrl($kirby, $url)) {
52+
if (isInternalUrl($kirby, $url) === false) {
4253
return $url;
4354
}
4455

4556
return getFromManifest($kirby, $url);
4657
},
4758
'js' => function (Kirby $kirby, string $url, $options = null): string {
48-
if (!option('mrfd.mix.enable')) {
59+
if (option('mrfd.mix.enable') === false) {
4960
return $url;
5061
}
5162

52-
if (!isInternalUrl($kirby, $url)) {
63+
if (isInternalUrl($kirby, $url) === false) {
5364
return $url;
5465
}
5566

@@ -59,6 +70,6 @@ function isInternalUrl(Kirby $kirby, string $url): bool
5970
'options' => [
6071
'enable' => true,
6172
'manifest' => 'mix-manifest.json',
62-
'publicpath' => 'assets'
73+
'publicPath' => 'assets'
6374
]
6475
]);

0 commit comments

Comments
 (0)