Skip to content

Commit

Permalink
feature/chainable-template-methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Rens committed Apr 12, 2023
1 parent 8aa923e commit 4c5fd68
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
4 changes: 4 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ SilverStripe\Assets\Storage\DBFile:
SilverStripe\Assets\File:
allowed_extensions:
- webp

Silverstripe\Assets\Image:
extensions:
- WeDevelop\WebpImages\Asset\ImageExtension
23 changes: 23 additions & 0 deletions src/Asset/ImageExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace WeDevelop\WebpImages\Asset;

use SilverStripe\Core\Extension;
use WeDevelop\WebpImages\WebpGenerator;

class ImageExtension extends Extension
{
public function EnableWebP()
{
WebpGenerator::singleton()->setEnabledForNextGenerate(true);

return $this->getOwner();
}

public function DisableWebP()
{
WebpGenerator::singleton()->setEnabledForNextGenerate(false);

return $this->getOwner();
}
}
4 changes: 2 additions & 2 deletions src/Storage/DBFileExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
19 changes: 19 additions & 0 deletions src/WebpGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final class WebpGenerator
{
use Injectable;

private ?bool $enabledForNextGenerate = null;
public bool $enabled = true;
public int $quality = 80;

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 4c5fd68

Please sign in to comment.