From 1cef2a182a8abca1f4c7f52f989a34d9953a92e2 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 27 Apr 2018 20:38:57 +0300 Subject: [PATCH] Added `Grav\Common\Media` interfaces and trait; use those in `Page` and `Media` classes Added `Grav\Common\Page` interface to allow custom page types in the future --- CHANGELOG.md | 2 + .../Interfaces/MediaCollectionInterface.php | 9 +++ .../Media/Interfaces/MediaInterface.php | 22 +++++++ .../Media/Interfaces/MediaObjectInterface.php | 9 +++ .../Grav/Common/Media/Traits/MediaTrait.php | 60 +++++++++++++++++++ .../Common/Page/Interfaces/PageInterface.php | 9 +++ .../Grav/Common/Page/Medium/AbstractMedia.php | 16 ++--- system/src/Grav/Common/Page/Medium/Medium.php | 6 +- system/src/Grav/Common/Page/Page.php | 40 ++++++++----- 9 files changed, 147 insertions(+), 26 deletions(-) create mode 100644 system/src/Grav/Common/Media/Interfaces/MediaCollectionInterface.php create mode 100644 system/src/Grav/Common/Media/Interfaces/MediaInterface.php create mode 100644 system/src/Grav/Common/Media/Interfaces/MediaObjectInterface.php create mode 100644 system/src/Grav/Common/Media/Traits/MediaTrait.php create mode 100644 system/src/Grav/Common/Page/Interfaces/PageInterface.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 5148febfe..a994d54fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ * Updated Doctrine Collections to 1.4 * Updated Symfony Components to 3.4 (with compatibility mode to fall back to Symfony YAML 2.8) * Added new `Grav\Framework\File\Formatter` classes for encoding/decoding YAML, Markdown, JSON, INI and PHP serialized strings + * Added `Grav\Common\Media` interfaces and trait; use those in `Page` and `Media` classes + * Added `Grav\Common\Page` interface to allow custom page types in the future # v1.4.4 ## 04/12/2018 diff --git a/system/src/Grav/Common/Media/Interfaces/MediaCollectionInterface.php b/system/src/Grav/Common/Media/Interfaces/MediaCollectionInterface.php new file mode 100644 index 000000000..06f71a5ec --- /dev/null +++ b/system/src/Grav/Common/Media/Interfaces/MediaCollectionInterface.php @@ -0,0 +1,9 @@ +media === null) { + // Use cached media if possible. + $cacheKey = md5('media' . $this->getCacheKey()); + if (!$media = $cache->fetch($cacheKey)) { + $media = new Media($this->getMediaFolder()); + $cache->save($cacheKey, $media); + } + $this->media = $media; + } + + return $this->media; + } + + /** + * Sets the associated media collection. + * + * @param MediaCollectionInterface $media Representation of associated media. + * @return $this + */ + protected function setMedia(MediaCollectionInterface $media) + { + $this->media = $media; + + return $this; + } + + /** + * @return string + */ + abstract protected function getCacheKey(); +} diff --git a/system/src/Grav/Common/Page/Interfaces/PageInterface.php b/system/src/Grav/Common/Page/Interfaces/PageInterface.php new file mode 100644 index 000000000..14d6cdc1e --- /dev/null +++ b/system/src/Grav/Common/Page/Interfaces/PageInterface.php @@ -0,0 +1,9 @@ +get('filepath')); + $output = preg_replace('|^' . preg_quote(GRAV_ROOT, '|') . '|', '', $this->get('filepath')); if ($reset) { $this->reset(); diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 072f747ab..f696d33e9 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -18,6 +18,8 @@ use Grav\Common\Grav; use Grav\Common\Language\Language; use Grav\Common\Markdown\Parsedown; use Grav\Common\Markdown\ParsedownExtra; +use Grav\Common\Page\Interfaces\PageInterface; +use Grav\Common\Media\Traits\MediaTrait; use Grav\Common\Taxonomy; use Grav\Common\Uri; use Grav\Common\Utils; @@ -28,8 +30,10 @@ use Symfony\Component\Yaml\Yaml; define('PAGE_ORDER_PREFIX_REGEX', '/^[0-9]+\./u'); -class Page +class Page implements PageInterface { + use MediaTrait; + /** * @var string Filename. Leave as null if page is folder. */ @@ -66,7 +70,6 @@ class Page protected $summary; protected $raw_content; protected $pagination; - protected $media; protected $metadata; protected $title; protected $max_count; @@ -1122,6 +1125,14 @@ class Page return json_encode($this->toArray()); } + /** + * @return string + */ + protected function getCacheKey() + { + return $this->id(); + } + /** * Gets and sets the associated media as found in the page folder. * @@ -1131,25 +1142,22 @@ class Page */ public function media($var = null) { - /** @var Cache $cache */ - $cache = Grav::instance()['cache']; - if ($var) { - $this->media = $var; - } - if ($this->media === null) { - // Use cached media if possible. - $media_cache_id = md5('media' . $this->id()); - if (!$media = $cache->fetch($media_cache_id)) { - $media = new Media($this->path()); - $cache->save($media_cache_id, $media); - } - $this->media = $media; + $this->setMedia($var); } - return $this->media; + return $this->getMedia(); } + public function getMediaFolder() + { + return $this->path(); + } + + + + + /** * Gets and sets the name field. If no name field is set, it will return 'default.md'. *