Skip to content

Commit

Permalink
Merge pull request #2 from rasteiner/main
Browse files Browse the repository at this point in the history
  • Loading branch information
tobimori authored Oct 28, 2024
2 parents e8063f6 + 594674f commit e55f68e
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 23 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,18 @@ If you still want to use variables, that e.g. come from the CMS directly, you ca

## Options

| Option | Default | Description |
| -------- | ------- | -------------------------------------- |
| `prefix` | `` | Set a prefix for your tailwind classes |
| Option | Default | Description |
| -------- | ------- | ----------------------------------------------------- |
| `prefix` | `''` | Set a prefix for your tailwind classes |
| `cache` | `false` | Enable caching for tailwind merge using a Kirby Cache |

Options allow you to fine tune the behaviour of the plugin. You can set them in your `config.php` file:

```php
return [
'tobimori.tailwind-merge' => [
'prefix' => 'tw-',
'cache' => true
],
];
```
Expand Down
68 changes: 68 additions & 0 deletions classes/KirbyCacheAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace tobimori;

use DateInterval;
use Kirby\Cache\Cache;
use Psr\SimpleCache\CacheInterface;

class KirbyCacheAdapter implements CacheInterface {
protected Cache $cache;

public function __construct(string $name)
{
$this->cache = kirby()->cache($name);
}

public function get(string $key, mixed $default = null): mixed {
return $this->cache->get($key) ?? $default;
}

public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool {
if($ttl instanceof DateInterval) {
$ttl = ceil($ttl->s / 60);
} elseif($ttl === null) {
$ttl = 0;
} elseif($ttl < 0) {
$ttl = 0;
} elseif(is_int($ttl)) {
$ttl = ceil($ttl / 60);
}

return $this->cache->set($key, $value, $ttl);
}

public function delete(string $key): bool {
return $this->cache->remove($key);
}

public function clear(): bool {
return $this->cache->flush();
}

public function getMultiple(iterable $keys, mixed $default = null): iterable {
$values = [];
foreach($keys as $key) {
$values[$key] = $this->cache->get($key) ?? $default;
}
return $values;
}

public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool {
foreach($values as $key => $value) {
$this->set($key, $value, $ttl);
}
return true;
}

public function deleteMultiple(iterable $keys): bool {
foreach($keys as $key) {
$this->delete($key);
}
return true;
}

public function has(string $key): bool {
return $this->cache->exists($key);
}
}
18 changes: 15 additions & 3 deletions classes/TwMerge.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@

class TwMerge
{
private static TailwindMerge $_instance;

public static function instance()
{
return TailwindMerge::factory()->withConfiguration([
'prefix' => option('tobimori.tailwind-merge.prefix', '')
])->make();
if (!isset(self::$_instance)) {
$factory = TailwindMerge::factory()->withConfiguration([
'prefix' => option('tobimori.tailwind-merge.prefix', '')
]);

if(option('tobimori.tailwind-merge.cache', false)) {
$factory = $factory->withCache(new KirbyCacheAdapter('tobimori.tailwind-merge'));
}

self::$_instance = $factory->make();
}

return self::$_instance;
}

public static function buildClassAttr(string|array $classes): string
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tobimori/kirby-tailwind-merge",
"description": "Tailwind Merge for Kirby CMS",
"version": "3.0.0",
"version": "3.1.0",
"type": "kirby-plugin",
"keywords": [
"kirby",
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit312d406bef150e9579cbbf902b985c58::getLoader();
return ComposerAutoloaderInit123f65cc68c0e5e85ce76d8ba69d9843::getLoader();
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@
'TailwindMerge\\ValueObjects\\ClassValidatorObject' => $vendorDir . '/gehrisandro/tailwind-merge-php/src/ValueObjects/ClassValidatorObject.php',
'TailwindMerge\\ValueObjects\\ParsedClass' => $vendorDir . '/gehrisandro/tailwind-merge-php/src/ValueObjects/ParsedClass.php',
'TailwindMerge\\ValueObjects\\ThemeGetter' => $vendorDir . '/gehrisandro/tailwind-merge-php/src/ValueObjects/ThemeGetter.php',
'tobimori\\KirbyCacheAdapter' => $baseDir . '/classes/KirbyCacheAdapter.php',
'tobimori\\TwMerge' => $baseDir . '/classes/TwMerge.php',
);
10 changes: 5 additions & 5 deletions vendor/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// autoload_real.php @generated by Composer

class ComposerAutoloaderInit312d406bef150e9579cbbf902b985c58
class ComposerAutoloaderInit123f65cc68c0e5e85ce76d8ba69d9843
{
private static $loader;

Expand All @@ -24,16 +24,16 @@ public static function getLoader()

require __DIR__ . '/platform_check.php';

spl_autoload_register(array('ComposerAutoloaderInit312d406bef150e9579cbbf902b985c58', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit123f65cc68c0e5e85ce76d8ba69d9843', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit312d406bef150e9579cbbf902b985c58', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit123f65cc68c0e5e85ce76d8ba69d9843', 'loadClassLoader'));

require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit312d406bef150e9579cbbf902b985c58::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit123f65cc68c0e5e85ce76d8ba69d9843::getInitializer($loader));

$loader->register(true);

$filesToLoad = \Composer\Autoload\ComposerStaticInit312d406bef150e9579cbbf902b985c58::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit123f65cc68c0e5e85ce76d8ba69d9843::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
Expand Down
9 changes: 5 additions & 4 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Composer\Autoload;

class ComposerStaticInit312d406bef150e9579cbbf902b985c58
class ComposerStaticInit123f65cc68c0e5e85ce76d8ba69d9843
{
public static $files = array (
'56980320311550930ae06096cc814eaf' => __DIR__ . '/..' . '/gehrisandro/tailwind-merge-php/src/TailwindMerge.php',
Expand Down Expand Up @@ -87,15 +87,16 @@ class ComposerStaticInit312d406bef150e9579cbbf902b985c58
'TailwindMerge\\ValueObjects\\ClassValidatorObject' => __DIR__ . '/..' . '/gehrisandro/tailwind-merge-php/src/ValueObjects/ClassValidatorObject.php',
'TailwindMerge\\ValueObjects\\ParsedClass' => __DIR__ . '/..' . '/gehrisandro/tailwind-merge-php/src/ValueObjects/ParsedClass.php',
'TailwindMerge\\ValueObjects\\ThemeGetter' => __DIR__ . '/..' . '/gehrisandro/tailwind-merge-php/src/ValueObjects/ThemeGetter.php',
'tobimori\\KirbyCacheAdapter' => __DIR__ . '/../..' . '/classes/KirbyCacheAdapter.php',
'tobimori\\TwMerge' => __DIR__ . '/../..' . '/classes/TwMerge.php',
);

public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit312d406bef150e9579cbbf902b985c58::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit312d406bef150e9579cbbf902b985c58::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit312d406bef150e9579cbbf902b985c58::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit123f65cc68c0e5e85ce76d8ba69d9843::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit123f65cc68c0e5e85ce76d8ba69d9843::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit123f65cc68c0e5e85ce76d8ba69d9843::$classMap;

}, null, ClassLoader::class);
}
Expand Down
12 changes: 6 additions & 6 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php return array(
'root' => array(
'name' => 'tobimori/kirby-tailwind-merge',
'pretty_version' => '2.0.0',
'version' => '2.0.0.0',
'reference' => NULL,
'pretty_version' => '3.1.0',
'version' => '3.1.0.0',
'reference' => null,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down Expand Up @@ -38,9 +38,9 @@
'dev_requirement' => false,
),
'tobimori/kirby-tailwind-merge' => array(
'pretty_version' => '2.0.0',
'version' => '2.0.0.0',
'reference' => NULL,
'pretty_version' => '3.1.0',
'version' => '3.1.0.0',
'reference' => null,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down

0 comments on commit e55f68e

Please sign in to comment.