Fixed media serialization

This commit is contained in:
Matias Griese
2022-05-06 13:40:10 +03:00
parent 4312df9eaf
commit 52f2488281
8 changed files with 191 additions and 62 deletions

View File

@@ -32,15 +32,6 @@ use function in_array;
*/
trait ImageMediaTrait
{
/** @var Image|null */
protected $image;
/** @var string */
protected $format = 'guess';
/** @var int */
protected $quality;
/** @var bool */
protected $watermark;
/** @var array */
public static $magic_actions = [
'resize', 'forceResize', 'cropResize', 'crop', 'zoomCrop',
@@ -58,9 +49,44 @@ trait ImageMediaTrait
'zoomCrop' => [0, 1]
];
/** @var Image|null */
protected $image;
/** @var string */
protected $format = 'guess';
/** @var int */
protected $quality;
/** @var bool */
protected $watermark;
/** @var string */
protected $sizes = '100vw';
/**
* @return array
*/
private function serializeImageMediaTrait(): array
{
return [
'image' => $this->image,
'format' => $this->format,
'quality' => $this->quality,
'watermark' => $this->watermark,
'sizes' => $this->sizes,
];
}
/**
* @param array $data
* @return void
*/
private function unserializeImageMediaTrait(array $data): void
{
$this->image = $data['image'];
$this->format = $data['format'];
$this->quality = $data['quality'];
$this->watermark = $data['watermark'];
$this->sizes = $data['sizes'];
}
/**
* @param string $path
* @return array|null
@@ -767,7 +793,7 @@ trait ImageMediaTrait
/**
* @param string $filepath
* @return JsonFile
* @phpstan-pure
* @phpstan-pure
*/
protected static function getCacheMetaFile(string $filepath): JsonFile
{

View File

@@ -31,6 +31,11 @@ use function is_string;
*/
trait MediaObjectTrait
{
/** @var string|int */
public $timestamp;
/** @var array */
public $metadata = [];
/** @var string */
protected $mode = 'source';
/** @var MediaObjectInterface|null */
@@ -46,11 +51,42 @@ trait MediaObjectTrait
/** @var array */
protected $styleAttributes = [];
/** @var array */
protected $metadata = [];
/** @var array */
protected $medium_querystring = [];
/** @var string */
protected $timestamp;
/**
* @return array
*/
private function serializeMediaObjectTrait(): array
{
return [
'mode' => $this->mode,
'thumbnailTypes' => $this->thumbnailTypes,
'thumbnailType' => $this->thumbnailType,
'alternatives' => $this->alternatives,
'attributes' => $this->attributes,
'styleAttributes' => $this->styleAttributes,
'metadata' => $this->metadata,
'medium_querystring' => $this->medium_querystring,
'timestamp' => $this->timestamp,
];
}
/**
* @param array $data
* @return void
*/
private function unserializeMediaObjectTrait(array $data): void
{
$this->mode = $data['mode'];
$this->thumbnailTypes = $data['thumbnailTypes'];
$this->thumbnailType = $data['thumbnailType'];
$this->alternatives = $data['alternatives'];
$this->attributes = $data['attributes'];
$this->styleAttributes = $data['styleAttributes'];
$this->metadata = $data['metadata'];
$this->medium_querystring = $data['medium_querystring'];
$this->timestamp = $data['timestamp'];
}
/**
* Create a copy of this media object

View File

@@ -90,6 +90,34 @@ class ImageMedium extends Medium implements ImageMediaInterface, ImageManipulate
}
}
/**
* @return array
*/
public function __serialize(): array
{
return parent::__serialize() +
$this->serializeImageMediaTrait() +
[
'defaults' => $this->defaults,
'imageSettings' => $this->imageSettings,
'saved_image_path' => $this->saved_image_path
];
}
/**
* @param array $data
* @return void
*/
public function __unserialize(array $data): void
{
parent::__unserialize($data);
$this->unserializeImageMediaTrait($data);
$this->defaults = $data['defaults'];
$this->imageSettings = $data['imageSettings'];
$this->saved_image_path = $data['saved_image_path'];
}
/**
* Get basic file info.
*

View File

@@ -27,7 +27,7 @@ class Link implements RenderableInterface, MediaLinkInterface
use ParsedownHtmlTrait;
/** @var array */
protected $attributes = [];
protected array $attributes;
/** @var MediaObjectInterface|MediaLinkInterface */
protected $source;
@@ -50,6 +50,27 @@ class Link implements RenderableInterface, MediaLinkInterface
$this->source = $source;
}
/**
* @return array
*/
public function __serialize(): array
{
return [
'attributes' => $this->attributes,
'source' => $this->source
];
}
/**
* @param array $data
* @return void
*/
public function __unserialize(array $data): void
{
$this->attributes = $data['attributes'];
$this->source = $data['source'];
}
/**
* Get an element (is array) that can be rendered by the Parsedown engine
*

View File

@@ -35,9 +35,7 @@ use RuntimeException;
* @property string $mime
* @property int $size
* @property int $modified
* @property array $metadata
* @property array|null $meta
* @property int|string $timestamp
* @property bool|null $uploaded
*/
class Medium implements RenderableInterface, MediaFileInterface, JsonSerializable, \Countable, ExportInterface
@@ -50,7 +48,7 @@ class Medium implements RenderableInterface, MediaFileInterface, JsonSerializabl
use ParsedownHtmlTrait;
/** @var string[] */
protected $items;
protected array $items;
/**
* Construct.
@@ -65,6 +63,8 @@ class Medium implements RenderableInterface, MediaFileInterface, JsonSerializabl
$size = $items['size'] ?? null;
$modified = $items['modified'] ?? null;
if (null === $size || null === $modified) {
user_error(__METHOD__ . '() missing size and modified properties are deprecated since Grav 1.8, pass those in $items array', E_USER_DEPRECATED);
$path = $items['filepath'];
if ($path && file_exists($path)) {
$items['size'] = $size ?? filesize($path);
@@ -74,7 +74,7 @@ class Medium implements RenderableInterface, MediaFileInterface, JsonSerializabl
$this->items = $items;
if (Grav::instance()['config']->get('system.media.enable_media_timestamp', true)) {
if ($this->getGrav()['config']->get('system.media.enable_media_timestamp', true)) {
$this->timestamp = Grav::instance()['cache']->getKey();
}
@@ -91,6 +91,29 @@ class Medium implements RenderableInterface, MediaFileInterface, JsonSerializabl
/**
* @return array
*/
public function __serialize(): array
{
return [
'items' => $this->items,
'nestedSeparator' => $this->nestedSeparator,
] + $this->serializeMediaObjectTrait();
}
/**
* @param array $data
* @return void
*/
public function __unserialize(array $data): void
{
$this->items = $data['items'];
$this->nestedSeparator = $data['nestedSeparator'];
$this->unserializeMediaObjectTrait($data);
}
/**
* @return array
* @phpstan-pure
*/
public function jsonSerialize(): array
{
return $this->items;

View File

@@ -39,6 +39,18 @@ class StaticImageMedium extends Medium implements ImageMediaInterface
] + parent::getInfo();
}
/**
* @return array
* @phpstan-pure
*/
public function getMeta(): array
{
return [
'width' => $this->width,
'height' => $this->height,
] + parent::getMeta();
}
/**
* Parsedown element for source display mode
*

View File

@@ -15,9 +15,6 @@ use Grav\Common\Data\Blueprint;
/**
* Class StaticImageMedium
* @package Grav\Common\Page\Medium
*
* @property int $width
* @property int $height
*/
class VectorImageMedium extends StaticImageMedium
{
@@ -69,30 +66,4 @@ class VectorImageMedium extends StaticImageMedium
$this->def('height', (int)$height);
}
}
/**
* Get basic file info.
*
* @return array
* @phpstan-pure
*/
public function getInfo(): array
{
return [
'width' => $this->width,
'height' => $this->height,
] + parent::getInfo();
}
/**
* @return array
* @phpstan-pure
*/
public function getMeta(): array
{
return [
'width' => $this->width,
'height' => $this->height,
] + parent::getMeta();
}
}

View File

@@ -23,6 +23,32 @@ class VideoMedium extends Medium implements VideoMediaInterface
{
use VideoMediaTrait;
/**
* Get basic file info.
*
* @return array
* @phpstan-pure
*/
public function getInfo(): array
{
return [
'width' => $this->width,
'height' => $this->height,
] + parent::getInfo();
}
/**
* @return array
* @phpstan-pure
*/
public function getMeta(): array
{
return [
'width' => $this->width,
'height' => $this->height,
] + parent::getMeta();
}
/**
* Reset medium.
*
@@ -37,18 +63,4 @@ class VideoMedium extends Medium implements VideoMediaInterface
return $this;
}
/**
* Get basic file info.
*
* @return array
* @phpstan-pure
*/
public function getInfo(): array
{
return [
'width' => $this->width,
'height' => $this->height,
] + parent::getInfo();
}
}