From 914d0b0cfdd01c8828ca0ed61b6bfceec00ea3c9 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Thu, 5 May 2022 19:20:54 +0300 Subject: [PATCH] Prepareations to make medium immutable --- .../Common/Media/Traits/AudioMediaTrait.php | 12 +++++- .../Common/Media/Traits/ImageLoadingTrait.php | 14 +++++- .../Common/Media/Traits/MediaObjectTrait.php | 43 +++++++++++++++---- .../Common/Media/Traits/MediaPlayerTrait.php | 26 +++++++++++ .../Common/Media/Traits/StaticResizeTrait.php | 26 +++++++---- .../Common/Media/Traits/VideoMediaTrait.php | 16 ++++++- .../Grav/Common/Page/Medium/AbstractMedia.php | 2 +- .../Grav/Common/Page/Medium/ImageMedium.php | 16 ++++++- system/src/Grav/Common/Page/Medium/Link.php | 2 +- system/src/Grav/Common/Page/Medium/Medium.php | 4 -- .../Common/Page/Medium/StaticImageMedium.php | 13 ++++++ .../Common/Page/Medium/VectorImageMedium.php | 13 ++++++ .../Grav/Common/Page/Medium/VideoMedium.php | 13 ++++++ .../Framework/Flex/Traits/FlexMediaTrait.php | 4 +- 14 files changed, 175 insertions(+), 29 deletions(-) diff --git a/system/src/Grav/Common/Media/Traits/AudioMediaTrait.php b/system/src/Grav/Common/Media/Traits/AudioMediaTrait.php index 68fb737c4..d69d31398 100644 --- a/system/src/Grav/Common/Media/Traits/AudioMediaTrait.php +++ b/system/src/Grav/Common/Media/Traits/AudioMediaTrait.php @@ -28,7 +28,17 @@ trait AudioMediaTrait public function controlsList(string $controlsList) { $controlsList = str_replace('-', ' ', $controlsList); - $this->attributes['controlsList'] = $controlsList; + $currentList = $this->attributes['controlsList'] ?? null; + + if ($currentList === $controlsList) { + return $this; + } + + if ($controlsList) { + $this->attributes['controlsList'] = $controlsList; + } else { + unset($this->attributes['controlsList']); + } return $this; } diff --git a/system/src/Grav/Common/Media/Traits/ImageLoadingTrait.php b/system/src/Grav/Common/Media/Traits/ImageLoadingTrait.php index 6fbc3036b..044c1d292 100644 --- a/system/src/Grav/Common/Media/Traits/ImageLoadingTrait.php +++ b/system/src/Grav/Common/Media/Traits/ImageLoadingTrait.php @@ -27,8 +27,20 @@ trait ImageLoadingTrait if (null === $value) { $value = Grav::instance()['config']->get('system.images.defaults.loading', 'auto'); } - if ($value && $value !== 'auto') { + + if ($value === 'auto') { + $value = null; + } + + $currentValue = $this->attributes['loading'] ?? null; + if ($currentValue === $value) { + return $this; + } + + if ($value) { $this->attributes['loading'] = $value; + } else { + unset($this->attributes['loading']); } return $this; diff --git a/system/src/Grav/Common/Media/Traits/MediaObjectTrait.php b/system/src/Grav/Common/Media/Traits/MediaObjectTrait.php index 3f34d5877..f8582e574 100644 --- a/system/src/Grav/Common/Media/Traits/MediaObjectTrait.php +++ b/system/src/Grav/Common/Media/Traits/MediaObjectTrait.php @@ -81,13 +81,19 @@ trait MediaObjectTrait public function setTimestamp($timestamp = null) { if (null !== $timestamp) { - $this->timestamp = (string)($timestamp); + $timestamp = (string)$timestamp; } elseif ($this instanceof MediaFileInterface) { - $this->timestamp = (string)$this->modified(); + $timestamp = (string)$this->modified(); } else { - $this->timestamp = ''; + $timestamp = ''; } + if ($timestamp === $this->timestamp) { + return $this; + } + + $this->timestamp = $timestamp; + return $this; } @@ -326,9 +332,21 @@ trait MediaObjectTrait */ public function attribute(string $attribute = '', ?string $value = '') { - if (!empty($attribute)) { - $this->attributes[$attribute] = $value; + if ('' === $attribute) { + return $this; } + + $currentValue = $this->attributes[$attribute] ?? null; + if ($currentValue === $value) { + return $this; + } + + if (null !== $value) { + $this->attributes[$attribute] = $value; + } else { + unset($this->attributes[$attribute]); + } + return $this; } @@ -377,12 +395,12 @@ trait MediaObjectTrait if ($type !== 'auto' && !in_array($type, $this->thumbnailTypes, true)) { return $this; } - - if ($this->thumbnailType !== $type) { - $this->_thumbnail = null; + if ($this->thumbnailType === $type) { + return $this; } $this->thumbnailType = $type; + $this->_thumbnail = null; return $this; } @@ -475,8 +493,15 @@ trait MediaObjectTrait */ public function id(string $id = null) { - if (is_string($id)) { + $currentId = $this->attributes['id'] ?? null; + if ($currentId === $id) { + return $this; + } + + if ($id) { $this->attributes['id'] = trim($id); + } else { + unset($this->attributes['id']); } return $this; diff --git a/system/src/Grav/Common/Media/Traits/MediaPlayerTrait.php b/system/src/Grav/Common/Media/Traits/MediaPlayerTrait.php index b4e8860a2..5914923f8 100644 --- a/system/src/Grav/Common/Media/Traits/MediaPlayerTrait.php +++ b/system/src/Grav/Common/Media/Traits/MediaPlayerTrait.php @@ -24,6 +24,11 @@ trait MediaPlayerTrait */ public function controls(bool $status = true) { + $currentStatus = (bool)($this->attributes['controls'] ?? null); + if ($currentStatus === $status) { + return $this; + } + if ($status) { $this->attributes['controls'] = 'controls'; } else { @@ -41,6 +46,11 @@ trait MediaPlayerTrait */ public function loop(bool $status = false) { + $currentStatus = (bool)($this->attributes['loop'] ?? null); + if ($currentStatus === $status) { + return $this; + } + if ($status) { $this->attributes['loop'] = 'loop'; } else { @@ -58,6 +68,11 @@ trait MediaPlayerTrait */ public function autoplay(bool $status = false) { + $currentStatus = (bool)($this->attributes['autoplay'] ?? null); + if ($currentStatus === $status) { + return $this; + } + if ($status) { $this->attributes['autoplay'] = 'autoplay'; } else { @@ -75,6 +90,11 @@ trait MediaPlayerTrait */ public function muted(bool $status = false) { + $currentStatus = (bool)($this->attributes['muted'] ?? null); + if ($currentStatus === $status) { + return $this; + } + if ($status) { $this->attributes['muted'] = 'muted'; } else { @@ -92,6 +112,11 @@ trait MediaPlayerTrait */ public function preload(string $preload = null) { + $currentPreload = $this->attributes['preload'] ?? null; + if ($currentPreload === $preload) { + return $this; + } + $validPreloadAttrs = ['auto', 'metadata', 'none']; if (null === $preload) { @@ -111,5 +136,6 @@ trait MediaPlayerTrait public function resetPlayer(): void { $this->attributes['controls'] = 'controls'; + unset($this->attributes['loop'], $this->attributes['autoplay'], $this->attributes['muted'], $this->attributes['preload']); } } diff --git a/system/src/Grav/Common/Media/Traits/StaticResizeTrait.php b/system/src/Grav/Common/Media/Traits/StaticResizeTrait.php index 7118fd6ce..3de956fbc 100644 --- a/system/src/Grav/Common/Media/Traits/StaticResizeTrait.php +++ b/system/src/Grav/Common/Media/Traits/StaticResizeTrait.php @@ -24,15 +24,25 @@ trait StaticResizeTrait */ public function resize(int $width = null, int $height = null) { - if ($width) { - $this->styleAttributes['width'] = (int)$width . 'px'; - } else { - unset($this->styleAttributes['width']); + $currentWidth = $this->styleAttributes['width'] ?? null; + $currentHeight = $this->styleAttributes['height'] ?? null; + if ($currentWidth === $width && $currentHeight === $height) { + return $this; } - if ($height) { - $this->styleAttributes['height'] = (int)$height . 'px'; - } else { - unset($this->styleAttributes['height']); + + if ($currentWidth !== $width) { + if ($width) { + $this->styleAttributes['width'] = $width . 'px'; + } else { + unset($this->styleAttributes['width']); + } + } + if ($currentHeight !== $height) { + if ($height) { + $this->styleAttributes['height'] = $height . 'px'; + } else { + unset($this->styleAttributes['height']); + } } return $this; diff --git a/system/src/Grav/Common/Media/Traits/VideoMediaTrait.php b/system/src/Grav/Common/Media/Traits/VideoMediaTrait.php index 67857a496..759e2c449 100644 --- a/system/src/Grav/Common/Media/Traits/VideoMediaTrait.php +++ b/system/src/Grav/Common/Media/Traits/VideoMediaTrait.php @@ -26,7 +26,16 @@ trait VideoMediaTrait */ public function poster(string $poster = null) { - $this->attributes['poster'] = $urlImage; + $currentPoster = $this->attributes['poster'] ?? null; + if ($currentPoster !== $poster) { + return $this; + } + + if ($poster) { + $this->attributes['poster'] = $poster; + } else { + unset($this->attributes['poster']); + } return $this; } @@ -39,6 +48,11 @@ trait VideoMediaTrait */ public function playsinline(bool $status = false) { + $currentStatus = (bool)($this->attributes['playsinline'] ?? null); + if ($currentStatus === $status) { + return $this; + } + if ($status) { $this->attributes['playsinline'] = 'playsinline'; } else { diff --git a/system/src/Grav/Common/Page/Medium/AbstractMedia.php b/system/src/Grav/Common/Page/Medium/AbstractMedia.php index c6442f9d4..622ee387b 100644 --- a/system/src/Grav/Common/Page/Medium/AbstractMedia.php +++ b/system/src/Grav/Common/Page/Medium/AbstractMedia.php @@ -856,7 +856,7 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac } else { /** @var Debugger $debugger */ $debugger = Grav::instance()['debugger']; - $debugger->addMessage(sprintf('Could not create alternative image for %s: %s', $medium->filename, $e->getMessage()), 'warning'); + $debugger->addMessage(sprintf('Could not create alternative image for %s', $medium->filename), 'warning'); } } } diff --git a/system/src/Grav/Common/Page/Medium/ImageMedium.php b/system/src/Grav/Common/Page/Medium/ImageMedium.php index 8161e3aee..354b94b7a 100644 --- a/system/src/Grav/Common/Page/Medium/ImageMedium.php +++ b/system/src/Grav/Common/Page/Medium/ImageMedium.php @@ -87,6 +87,20 @@ class ImageMedium extends Medium implements ImageMediaInterface, ImageManipulate } } + /** + * Get basic file info. + * + * @return array + */ + public function getInfo(): array + { + return [ + 'width' => $this->width, + 'height' => $this->height, + 'orientation' => $this->orientation, + ] + parent::getInfo(); + } + /** * @return array */ @@ -110,7 +124,7 @@ class ImageMedium extends Medium implements ImageMediaInterface, ImageManipulate $this->format = 'guess'; $this->imageSettings = $this->defaults; - $this->quality = $this->defaults['quality']; + $this->quality = $this->defaults['quality'] ?? 80; $this->resetImage(); diff --git a/system/src/Grav/Common/Page/Medium/Link.php b/system/src/Grav/Common/Page/Medium/Link.php index 58db5e12e..47ec52884 100644 --- a/system/src/Grav/Common/Page/Medium/Link.php +++ b/system/src/Grav/Common/Page/Medium/Link.php @@ -67,7 +67,7 @@ class Link implements RenderableInterface, MediaLinkInterface return [ 'name' => 'a', 'attributes' => $this->attributes, - 'handler' => is_array($innerElement) ? 'element' : 'line', + 'handler' => 'element', 'text' => $innerElement ]; } diff --git a/system/src/Grav/Common/Page/Medium/Medium.php b/system/src/Grav/Common/Page/Medium/Medium.php index 67c446bd8..86679cf50 100644 --- a/system/src/Grav/Common/Page/Medium/Medium.php +++ b/system/src/Grav/Common/Page/Medium/Medium.php @@ -134,14 +134,10 @@ class Medium implements RenderableInterface, MediaFileInterface, JsonSerializabl */ public function getInfo(): array { - // TODO: this may require some tweaking, works for now. $info = [ 'modified' => $this->modified, 'size' => $this->size, 'mime' => $this->mime, - 'width' => $this->width, - 'height' => $this->height, - 'orientation' => $this->orientation, 'meta' => $this->meta ?? [], ]; diff --git a/system/src/Grav/Common/Page/Medium/StaticImageMedium.php b/system/src/Grav/Common/Page/Medium/StaticImageMedium.php index e089b053d..f07bbfaa4 100644 --- a/system/src/Grav/Common/Page/Medium/StaticImageMedium.php +++ b/system/src/Grav/Common/Page/Medium/StaticImageMedium.php @@ -25,6 +25,19 @@ class StaticImageMedium extends Medium implements ImageMediaInterface use StaticResizeTrait; use ImageLoadingTrait; + /** + * Get basic file info. + * + * @return array + */ + public function getInfo(): array + { + return [ + 'width' => $this->width, + 'height' => $this->height, + ] + parent::getInfo(); + } + /** * Parsedown element for source display mode * diff --git a/system/src/Grav/Common/Page/Medium/VectorImageMedium.php b/system/src/Grav/Common/Page/Medium/VectorImageMedium.php index db095b7f1..76163909a 100644 --- a/system/src/Grav/Common/Page/Medium/VectorImageMedium.php +++ b/system/src/Grav/Common/Page/Medium/VectorImageMedium.php @@ -70,6 +70,19 @@ class VectorImageMedium extends StaticImageMedium } } + /** + * Get basic file info. + * + * @return array + */ + public function getInfo(): array + { + return [ + 'width' => $this->width, + 'height' => $this->height, + ] + parent::getInfo(); + } + /** * @return array */ diff --git a/system/src/Grav/Common/Page/Medium/VideoMedium.php b/system/src/Grav/Common/Page/Medium/VideoMedium.php index d121da36f..44822263f 100644 --- a/system/src/Grav/Common/Page/Medium/VideoMedium.php +++ b/system/src/Grav/Common/Page/Medium/VideoMedium.php @@ -36,4 +36,17 @@ class VideoMedium extends Medium implements VideoMediaInterface return $this; } + + /** + * Get basic file info. + * + * @return array + */ + public function getInfo(): array + { + return [ + 'width' => $this->width, + 'height' => $this->height, + ] + parent::getInfo(); + } } diff --git a/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php b/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php index 356a7570a..a6e6298e3 100644 --- a/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php +++ b/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php @@ -83,7 +83,7 @@ trait FlexMediaTrait { // Load settings for the field. $schema = $this->getBlueprint()->schema(); - $settings = $schema ? $schema->getProperty($field) : null; + $settings = $schema->getProperty($field); return $this->parseMediaFieldSettings($field, $settings) ?? $settings; } @@ -97,7 +97,7 @@ trait FlexMediaTrait { // Load settings for the field. $schema = $this->getBlueprint()->schema(); - $settings = $schema ? $schema->getProperty($field) : null; + $settings = $schema->getProperty($field); $settings = $this->parseMediaFieldSettings($field, $settings); if ($settings && !isset($settings['media']['order'])) {