Prepareations to make medium immutable

This commit is contained in:
Matias Griese
2022-05-05 19:20:54 +03:00
parent 9c90a62b6c
commit 914d0b0cfd
14 changed files with 175 additions and 29 deletions

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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']);
}
}

View File

@@ -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;

View File

@@ -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 {

View File

@@ -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');
}
}
}

View File

@@ -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();

View File

@@ -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
];
}

View File

@@ -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 ?? [],
];

View File

@@ -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
*

View File

@@ -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
*/

View File

@@ -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();
}
}

View File

@@ -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'])) {