Skip to content

Commit

Permalink
Merge pull request #47 from sitegeist/feature/version3
Browse files Browse the repository at this point in the history
FEATURE: Introduce new imageSourceInterfaces with consistent immutability and deprecate the old ones
  • Loading branch information
mficzel committed Apr 14, 2022
2 parents 4402870 + cc57272 commit b87c182
Show file tree
Hide file tree
Showing 24 changed files with 771 additions and 479 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

declare(strict_types=1);

namespace Sitegeist\Kaleidoscope\EelHelpers;
namespace Sitegeist\Kaleidoscope\Domain;

use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Utility\Arrays;
use Psr\Log\LoggerInterface;
use Sitegeist\Kaleidoscope\EelHelpers\ImageSourceHelperInterface;

/**
* Class AbstractImageSourceHelper.
*/
abstract class AbstractImageSourceHelper implements ImageSourceHelperInterface
abstract class AbstractImageSource implements ImageSourceInterface, ProtectedContextAwareInterface, ImageSourceHelperInterface
{
/**
* @var int|null
Expand Down Expand Up @@ -63,13 +65,21 @@ abstract class AbstractImageSourceHelper implements ImageSourceHelperInterface
*/
protected $alt;

public function __construct(?string $title = null, ?string $alt = null)
{
$this->title = $title;
$this->alt = $alt;
}

/**
* @param int $targetWidth
* @param bool $preserveAspect
*
* @return ImageSourceHelperInterface
* @deprecated
*/
public function setWidth(int $targetWidth, bool $preserveAspect = false): ImageSourceHelperInterface
public function setWidth(int $targetWidth, bool $preserveAspect = false): ImageSourceInterface
{
return $this->withWidth($targetWidth, $preserveAspect);
}

public function withWidth(int $targetWidth, bool $preserveAspect = false): ImageSourceInterface
{
$newSource = clone $this;
$newSource->targetWidth = $targetWidth;
Expand All @@ -78,12 +88,14 @@ public function setWidth(int $targetWidth, bool $preserveAspect = false): ImageS
}

/**
* @param int $targetHeight
* @param bool $preserveAspect
*
* @return ImageSourceHelperInterface
* @deprecated
*/
public function setHeight(int $targetHeight, bool $preserveAspect = false): ImageSourceHelperInterface
public function setHeight(int $targetHeight, bool $preserveAspect = false): ImageSourceInterface
{
return $this->withHeight($targetHeight, $preserveAspect);
}

public function withHeight(int $targetHeight, bool $preserveAspect = false): ImageSourceInterface
{
$newSource = clone $this;
$newSource->targetHeight = $targetHeight;
Expand All @@ -92,11 +104,14 @@ public function setHeight(int $targetHeight, bool $preserveAspect = false): Imag
}

/**
* @param string $format
*
* @return ImageSourceHelperInterface
* @deprecated
*/
public function setFormat(string $format): ImageSourceHelperInterface
public function setFormat(string $format): ImageSourceInterface
{
return $this->withFormat($format);
}

public function withFormat(string $format): ImageSourceInterface
{
$newSource = clone $this;
$newSource->targetFormat = $format;
Expand All @@ -105,12 +120,14 @@ public function setFormat(string $format): ImageSourceHelperInterface
}

/**
* @param int $targetWidth
* @param int $targetHeight
*
* @return ImageSourceHelperInterface
* @deprecated
*/
public function setDimensions(int $targetWidth, int $targetHeight): ImageSourceHelperInterface
public function setDimensions(int $targetWidth, int $targetHeight): ImageSourceInterface
{
return $this->withDimensions($targetWidth, $targetHeight);
}

public function withDimensions(int $targetWidth, int $targetHeight): ImageSourceInterface
{
$newSource = clone $this;
$newSource->targetWidth = $targetWidth;
Expand All @@ -120,40 +137,35 @@ public function setDimensions(int $targetWidth, int $targetHeight): ImageSourceH
}

/**
* DEPRECATED Apply definitions from a thumbnail preset to this image source.
*
* @param string $name
*
* @deprecated use applyThumbnailPreset
*
* @return ImageSourceHelperInterface
*/
public function applyPreset(string $name): ImageSourceHelperInterface
public function applyPreset(string $name): ImageSourceInterface
{
return $this->applyThumbnailPreset($name);
return $this->withThumbnailPreset($name);
}

/**
* Apply definitions from a thumbnail preset to this image source.
*
* @param string $name
*
* @return ImageSourceHelperInterface
* @deprecated
*/
public function applyThumbnailPreset(string $name): ImageSourceHelperInterface
public function applyThumbnailPreset(string $name): ImageSourceInterface
{
return $this->withThumbnailPreset($name);
}

public function withThumbnailPreset(string $name): ImageSourceInterface
{
$newSource = clone $this;
if (isset($this->thumbnailPresets[$name])) {
$preset = $this->thumbnailPresets[$name];
if ($width = $preset['width'] ?? null) {
$newSource = $newSource->setWidth($width);
$newSource = $newSource->withWidth($width);
} elseif ($width = $preset['maximumWidth'] ?? null) {
$newSource = $newSource->setWidth($width);
$newSource = $newSource->withWidth($width);
}
if ($height = $preset['height'] ?? null) {
$newSource = $newSource->setHeight($height);
$newSource = $newSource->withHeight($height);
} elseif ($height = $preset['maximumHeight'] ?? null) {
$newSource = $newSource->setHeight($height);
$newSource = $newSource->withHeight($height);
}
} else {
$this->logger->warning(sprintf('Thumbnail preset "%s" is not configured', $name), LogEnvironment::fromMethodName(__METHOD__));
Expand All @@ -163,14 +175,14 @@ public function applyThumbnailPreset(string $name): ImageSourceHelperInterface
}

/**
* Use the variant generated from the given variant preset in this image source.
*
* @param string $presetIdentifier
* @param string $presetVariantName
*
* @return ImageSourceHelperInterface
* @deprecated
*/
public function useVariantPreset(string $presetIdentifier, string $presetVariantName): ImageSourceHelperInterface
public function useVariantPreset(string $presetIdentifier, string $presetVariantName): ImageSourceInterface
{
return $this->withVariantPreset($presetIdentifier, $presetVariantName);
}

public function withVariantPreset(string $presetIdentifier, string $presetVariantName): ImageSourceInterface
{
if (!isset($this->variantPresets[$presetIdentifier]['variants'][$presetVariantName])) {
$this->logger->warning(sprintf('Variant "%s" of preset "%s" is not configured', $presetVariantName, $presetIdentifier), LogEnvironment::fromMethodName(__METHOD__));
Expand All @@ -191,7 +203,7 @@ public function useVariantPreset(string $presetIdentifier, string $presetVariant
*/
public function srcset($mediaDescriptors): string
{
if ($this instanceof ScalableImageSourceHelperInterface) {
if ($this instanceof ScalableImageSourceInterface) {
$srcsetArray = [];

if (is_array($mediaDescriptors) || $mediaDescriptors instanceof \Traversable) {
Expand All @@ -203,7 +215,7 @@ public function srcset($mediaDescriptors): string
foreach ($descriptors as $descriptor) {
if (preg_match('/^(?<width>[0-9]+)w$/u', $descriptor, $matches)) {
$width = (int) $matches['width'];
$scaleFactor = $width / $this->getCurrentWidth();
$scaleFactor = $width / $this->width();
$scaled = $this->scale($scaleFactor);
$srcsetArray[] = $scaled->src().' '.$width.'w';
} elseif (preg_match('/^(?<factor>[0-9\\.]+)x$/u', $descriptor, $matches)) {
Expand All @@ -220,21 +232,37 @@ public function srcset($mediaDescriptors): string
}

/**
* @param string|null $title
* @deprecated use withTitle
*/
public function setTitle(?string $title): void
{
$this->title = $title;
}

public function withTitle(?string $title): ImageSourceInterface
{
$newSource = clone $this;
$newSource->title = $title;

return $newSource;
}

/**
* @param string|null $alt
* @deprecated use withAlt
*/
public function setAlt(?string $alt): void
{
$this->alt = $alt;
}

public function withAlt(?string $alt): ImageSourceInterface
{
$newSource = clone $this;
$newSource->alt = $alt;

return $newSource;
}

public function title(): ?string
{
return $this->title;
Expand All @@ -245,6 +273,16 @@ public function alt(): ?string
return $this->alt;
}

public function width(): ?int
{
return null;
}

public function height(): ?int
{
return null;
}

/**
* Define which methods are available in the Eel context.
*
Expand All @@ -254,7 +292,35 @@ public function alt(): ?string
*/
public function allowsCallOfMethod($methodName)
{
if (in_array($methodName, ['setWidth', 'setHeight', 'setDimensions', 'setFormat', 'applyPreset', 'applyThumbnailPreset', 'useVariantPreset', 'src', 'srcset', 'title', 'alt'])) {
if (in_array(
$methodName,
[
'withAlt',
'withTitle',
'withDimensions',
'withFormat',
'withWidth',
'withHeight',
'withThumbnailPreset',
'withVariantPreset',

'setWidth',
'setHeight',
'setDimensions',
'setFormat',
'applyPreset',
'applyThumbnailPreset',
'useVariantPreset',

'src',
'srcset',
'title',
'alt',
'width',
'height',
]
)
) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Sitegeist\Kaleidoscope\EelHelpers;
namespace Sitegeist\Kaleidoscope\Domain;

use Imagine\Image\Box;
use Imagine\Image\ImagineInterface;
Expand All @@ -13,8 +13,9 @@
use Neos\Media\Domain\ValueObject\Configuration\Adjustment;
use Neos\Media\Domain\ValueObject\Configuration\VariantPreset;
use Neos\Utility\ObjectAccess;
use Sitegeist\Kaleidoscope\EelHelpers\ScalableImageSourceHelperInterface;

abstract class AbstractScalableImageSourceHelper extends AbstractImageSourceHelper implements ScalableImageSourceHelperInterface
abstract class AbstractScalableImageSource extends AbstractImageSource implements ScalableImageSourceInterface, ScalableImageSourceHelperInterface
{
/**
* @var ImagineInterface
Expand All @@ -36,9 +37,9 @@ abstract class AbstractScalableImageSourceHelper extends AbstractImageSourceHelp
* @param int|null $targetWidth
* @param bool $preserveAspect
*
* @return ImageSourceHelperInterface
* @return ImageSourceInterface
*/
public function setWidth(int $targetWidth = null, bool $preserveAspect = false): ImageSourceHelperInterface
public function withWidth(int $targetWidth = null, bool $preserveAspect = false): ImageSourceInterface
{
$newSource = clone $this;
$newSource->targetWidth = $targetWidth;
Expand All @@ -54,9 +55,9 @@ public function setWidth(int $targetWidth = null, bool $preserveAspect = false):
* @param int|null $targetHeight
* @param bool $preserveAspect
*
* @return ImageSourceHelperInterface
* @return ImageSourceInterface
*/
public function setHeight(int $targetHeight = null, bool $preserveAspect = false): ImageSourceHelperInterface
public function withHeight(int $targetHeight = null, bool $preserveAspect = false): ImageSourceInterface
{
$newSource = clone $this;
$newSource->targetHeight = $targetHeight;
Expand All @@ -71,29 +72,34 @@ public function setHeight(int $targetHeight = null, bool $preserveAspect = false
/**
* @param float $factor
*
* @return ImageSourceHelperInterface
* @return ImageSourceInterface
*/
public function scale(float $factor): ImageSourceHelperInterface
public function scale(float $factor): ImageSourceInterface
{
$scaledHelper = clone $this;

if ($this->targetWidth && $this->targetHeight) {
$scaledHelper = $scaledHelper->setDimensions((int) round($factor * $this->targetWidth), (int) round($factor * $this->targetHeight));
$scaledHelper = $scaledHelper->withDimensions((int) round($factor * $this->targetWidth), (int) round($factor * $this->targetHeight));
} elseif ($this->targetWidth) {
$scaledHelper = $scaledHelper->setWidth((int) round($factor * $this->targetWidth));
$scaledHelper = $scaledHelper->withWidth((int) round($factor * $this->targetWidth));
} elseif ($this->targetHeight) {
$scaledHelper = $scaledHelper->setHeight((int) round($factor * $this->targetHeight));
$scaledHelper = $scaledHelper->withHeight((int) round($factor * $this->targetHeight));
} else {
$scaledHelper = $scaledHelper->setWidth((int) round($factor * $this->baseWidth));
$scaledHelper = $scaledHelper->withWidth((int) round($factor * $this->baseWidth));
}

return $scaledHelper;
}

/**
* @return int
* @deprecated use width()
*/
public function getCurrentWidth(): int
public function getCurrentWidth(): ?int
{
return $this->width();
}

public function width(): ?int
{
if ($this->targetWidth) {
return $this->targetWidth;
Expand All @@ -107,9 +113,14 @@ public function getCurrentWidth(): int
}

/**
* @return int
* @deprecated use height()
*/
public function getCurrentHeight(): int
public function getCurrentHeight(): ?int
{
return $this->height();
}

public function height(): ?int
{
if ($this->targetHeight) {
return $this->targetHeight;
Expand Down
Loading

0 comments on commit b87c182

Please sign in to comment.