From 4c5fd6821009a4418cf6d48737a8420d7e3c6bf5 Mon Sep 17 00:00:00 2001 From: Rens Date: Wed, 12 Apr 2023 12:56:06 +0200 Subject: [PATCH] feature/chainable-template-methods --- _config/config.yml | 4 ++++ src/Asset/ImageExtension.php | 23 +++++++++++++++++++++++ src/Storage/DBFileExtension.php | 4 ++-- src/WebpGenerator.php | 19 +++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/Asset/ImageExtension.php diff --git a/_config/config.yml b/_config/config.yml index d722299..8c75bdf 100755 --- a/_config/config.yml +++ b/_config/config.yml @@ -14,3 +14,7 @@ SilverStripe\Assets\Storage\DBFile: SilverStripe\Assets\File: allowed_extensions: - webp + +Silverstripe\Assets\Image: + extensions: + - WeDevelop\WebpImages\Asset\ImageExtension diff --git a/src/Asset/ImageExtension.php b/src/Asset/ImageExtension.php new file mode 100644 index 0000000..1eff503 --- /dev/null +++ b/src/Asset/ImageExtension.php @@ -0,0 +1,23 @@ +setEnabledForNextGenerate(true); + + return $this->getOwner(); + } + + public function DisableWebP() + { + WebpGenerator::singleton()->setEnabledForNextGenerate(false); + + return $this->getOwner(); + } +} diff --git a/src/Storage/DBFileExtension.php b/src/Storage/DBFileExtension.php index e69bbee..3719749 100644 --- a/src/Storage/DBFileExtension.php +++ b/src/Storage/DBFileExtension.php @@ -16,10 +16,10 @@ class DBFileExtension extends Extension { public function updateURL(&$url): void { - if (!$this->owner->getIsImage() || $this->owner->getVisibility() !== AssetStore::VISIBILITY_PUBLIC) { + if (!$this->getOwner()->getIsImage() || $this->getOwner()->getVisibility() !== AssetStore::VISIBILITY_PUBLIC) { return; } - $url = WebpGenerator::singleton()->generate($url , $this->owner->getMimeType()); + $url = WebpGenerator::singleton()->generate($url, $this->getOwner()->getMimeType()); } } diff --git a/src/WebpGenerator.php b/src/WebpGenerator.php index 7fe5b5e..0650e82 100644 --- a/src/WebpGenerator.php +++ b/src/WebpGenerator.php @@ -10,6 +10,7 @@ final class WebpGenerator { use Injectable; + private ?bool $enabledForNextGenerate = null; public bool $enabled = true; public int $quality = 80; @@ -61,6 +62,24 @@ public function setEnabled(bool $enabled): void public function getEnabled(): bool { + if (is_bool($this->getEnabledForNextGenerate())) { + $state = $this->getEnabledForNextGenerate(); + + $this->enabledForNextGenerate = null; + + return $state; + } + return $this->enabled; } + + public function setEnabledForNextGenerate(bool $enabled): void + { + $this->enabledForNextGenerate = $enabled; + } + + public function getEnabledForNextGenerate(): ?bool + { + return $this->enabledForNextGenerate; + } }