Skip to content

Commit

Permalink
Added Grav\Common\Media interfaces and trait; use those in Page a…
Browse files Browse the repository at this point in the history
…nd `Media` classes

Added `Grav\Common\Page` interface to allow custom page types in the future
  • Loading branch information
mahagr committed Apr 27, 2018
1 parent ad87648 commit 1cef2a1
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Grav\Common\Media\Interfaces;

/**
* Class implements media collection interface.
*/
interface MediaCollectionInterface
{
}
22 changes: 22 additions & 0 deletions system/src/Grav/Common/Media/Interfaces/MediaInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Grav\Common\Media\Interfaces;

/**
* Class implements media interface.
*/
interface MediaInterface
{
/**
* Gets the associated media collection.
*
* @return MediaCollectionInterface Collection of associated media.
*/
public function getMedia();

/**
* Get filesystem path to the associated media.
*
* @return string|null Media path or null if the object doesn't have media folder.
*/
public function getMediaFolder();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Grav\Common\Media\Interfaces;

/**
* Class implements media object interface.
*/
interface MediaObjectInterface
{
}
60 changes: 60 additions & 0 deletions system/src/Grav/Common/Media/Traits/MediaTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace Grav\Common\Media\Traits;

use Grav\Common\Cache;
use Grav\Common\Grav;
use Grav\Common\Media\Interfaces\MediaCollectionInterface;
use Grav\Common\Page\Media;

trait MediaTrait
{
protected $media;

/**
* Get filesystem path to the associated media.
*
* @return string|null
*/
abstract public function getMediaFolder();

/**
* Gets the associated media collection.
*
* @return MediaCollectionInterface Representation of associated media.
*/
public function getMedia()
{
/** @var Cache $cache */
$cache = Grav::instance()['cache'];

if ($this->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();
}
9 changes: 9 additions & 0 deletions system/src/Grav/Common/Page/Interfaces/PageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Grav\Common\Page\Interfaces;

/**
* Class implements page interface.
*/
interface PageInterface
{
}
16 changes: 9 additions & 7 deletions system/src/Grav/Common/Page/Medium/AbstractMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

use Grav\Common\Getters;
use Grav\Common\Grav;
use Grav\Common\Media\Interfaces\MediaCollectionInterface;
use Grav\Common\Media\Interfaces\MediaObjectInterface;
use Grav\Common\Utils;

abstract class AbstractMedia extends Getters
abstract class AbstractMedia extends Getters implements MediaCollectionInterface
{
protected $gettersVariable = 'instances';

Expand Down Expand Up @@ -47,7 +49,7 @@ public function __invoke($filename)
/**
* Get a list of all media.
*
* @return array|Medium[]
* @return array|MediaObjectInterface[]
*/
public function all()
{
Expand All @@ -59,7 +61,7 @@ public function all()
/**
* Get a list of all image media.
*
* @return array|Medium[]
* @return array|MediaObjectInterface[]
*/
public function images()
{
Expand All @@ -70,7 +72,7 @@ public function images()
/**
* Get a list of all video media.
*
* @return array|Medium[]
* @return array|MediaObjectInterface[]
*/
public function videos()
{
Expand All @@ -81,7 +83,7 @@ public function videos()
/**
* Get a list of all audio media.
*
* @return array|Medium[]
* @return array|MediaObjectInterface[]
*/
public function audios()
{
Expand All @@ -92,7 +94,7 @@ public function audios()
/**
* Get a list of all file media.
*
* @return array|Medium[]
* @return array|MediaObjectInterface[]
*/
public function files()
{
Expand All @@ -102,7 +104,7 @@ public function files()

/**
* @param string $name
* @param Medium $file
* @param MediaObjectInterface $file
*/
protected function add($name, $file)
{
Expand Down
6 changes: 3 additions & 3 deletions system/src/Grav/Common/Page/Medium/Medium.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
use Grav\Common\Grav;
use Grav\Common\Data\Data;
use Grav\Common\Data\Blueprint;
use Grav\Common\Utils;
use Grav\Common\Media\Interfaces\MediaObjectInterface;

class Medium extends Data implements RenderableInterface
class Medium extends Data implements RenderableInterface, MediaObjectInterface
{
use ParsedownHtmlTrait;

Expand Down Expand Up @@ -194,7 +194,7 @@ public function relativePath($reset = true)
*/
public function url($reset = true)
{
$output = preg_replace('|^' . preg_quote(GRAV_ROOT) . '|', '', $this->get('filepath'));
$output = preg_replace('|^' . preg_quote(GRAV_ROOT, '|') . '|', '', $this->get('filepath'));

if ($reset) {
$this->reset();
Expand Down
40 changes: 24 additions & 16 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
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;
Expand All @@ -28,8 +30,10 @@

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.
*/
Expand Down Expand Up @@ -66,7 +70,6 @@ class Page
protected $summary;
protected $raw_content;
protected $pagination;
protected $media;
protected $metadata;
protected $title;
protected $max_count;
Expand Down Expand Up @@ -1122,6 +1125,14 @@ public function toJson()
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.
*
Expand All @@ -1131,25 +1142,22 @@ public function toJson()
*/
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'.
*
Expand Down

0 comments on commit 1cef2a1

Please sign in to comment.