MediaTrait.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @package Grav\Common\Media
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Media\Traits;
  9. use Grav\Common\Cache;
  10. use Grav\Common\Grav;
  11. use Grav\Common\Media\Interfaces\MediaCollectionInterface;
  12. use Grav\Common\Page\Media;
  13. use Psr\SimpleCache\CacheInterface;
  14. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  15. trait MediaTrait
  16. {
  17. protected $media;
  18. /**
  19. * Get filesystem path to the associated media.
  20. *
  21. * @return string|null
  22. */
  23. abstract public function getMediaFolder();
  24. /**
  25. * Get display order for the associated media.
  26. *
  27. * @return array Empty array means default ordering.
  28. */
  29. public function getMediaOrder()
  30. {
  31. return [];
  32. }
  33. /**
  34. * Get URI ot the associated media. Method will return null if path isn't URI.
  35. *
  36. * @return null|string
  37. */
  38. public function getMediaUri()
  39. {
  40. $folder = $this->getMediaFolder();
  41. if (strpos($folder, '://')) {
  42. return $folder;
  43. }
  44. /** @var UniformResourceLocator $locator */
  45. $locator = Grav::instance()['locator'];
  46. $user = $locator->findResource('user://');
  47. if (strpos($folder, $user) === 0) {
  48. return 'user://' . substr($folder, \strlen($user)+1);
  49. }
  50. return null;
  51. }
  52. /**
  53. * Gets the associated media collection.
  54. *
  55. * @return MediaCollectionInterface Representation of associated media.
  56. */
  57. public function getMedia()
  58. {
  59. if ($this->media === null) {
  60. $cache = $this->getMediaCache();
  61. // Use cached media if possible.
  62. $cacheKey = md5('media' . $this->getCacheKey());
  63. if (!$media = $cache->get($cacheKey)) {
  64. $media = new Media($this->getMediaFolder(), $this->getMediaOrder());
  65. $cache->set($cacheKey, $media);
  66. }
  67. $this->media = $media;
  68. }
  69. return $this->media;
  70. }
  71. /**
  72. * Sets the associated media collection.
  73. *
  74. * @param MediaCollectionInterface $media Representation of associated media.
  75. * @return $this
  76. */
  77. protected function setMedia(MediaCollectionInterface $media)
  78. {
  79. $cache = $this->getMediaCache();
  80. $cacheKey = md5('media' . $this->getCacheKey());
  81. $cache->set($cacheKey, $media);
  82. $this->media = $media;
  83. return $this;
  84. }
  85. /**
  86. * Clear media cache.
  87. */
  88. protected function clearMediaCache()
  89. {
  90. $cache = $this->getMediaCache();
  91. $cacheKey = md5('media' . $this->getCacheKey());
  92. $cache->delete($cacheKey);
  93. $this->media = null;
  94. }
  95. /**
  96. * @return CacheInterface
  97. */
  98. protected function getMediaCache()
  99. {
  100. /** @var Cache $cache */
  101. $cache = Grav::instance()['cache'];
  102. return $cache->getSimpleCache();
  103. }
  104. /**
  105. * @return string
  106. */
  107. abstract protected function getCacheKey(): string;
  108. }