MediaTrait.php 2.7 KB

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