AbstractMedia.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * @package Grav\Common\Page
  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\Page\Medium;
  9. use Grav\Common\Grav;
  10. use Grav\Common\Media\Interfaces\MediaCollectionInterface;
  11. use Grav\Common\Media\Interfaces\MediaObjectInterface;
  12. use Grav\Common\Page\Page;
  13. use Grav\Common\Utils;
  14. use RocketTheme\Toolbox\ArrayTraits\ArrayAccess;
  15. use RocketTheme\Toolbox\ArrayTraits\Countable;
  16. use RocketTheme\Toolbox\ArrayTraits\Export;
  17. use RocketTheme\Toolbox\ArrayTraits\ExportInterface;
  18. use RocketTheme\Toolbox\ArrayTraits\Iterator;
  19. abstract class AbstractMedia implements ExportInterface, MediaCollectionInterface
  20. {
  21. use ArrayAccess;
  22. use Countable;
  23. use Iterator;
  24. use Export;
  25. protected $items = [];
  26. protected $path;
  27. protected $images = [];
  28. protected $videos = [];
  29. protected $audios = [];
  30. protected $files = [];
  31. protected $media_order;
  32. /**
  33. * Return media path.
  34. *
  35. * @return string
  36. */
  37. public function getPath()
  38. {
  39. return $this->path;
  40. }
  41. public function setPath(?string $path)
  42. {
  43. $this->path = $path;
  44. }
  45. /**
  46. * Get medium by filename.
  47. *
  48. * @param string $filename
  49. * @return Medium|null
  50. */
  51. public function get($filename)
  52. {
  53. return $this->offsetGet($filename);
  54. }
  55. /**
  56. * Call object as function to get medium by filename.
  57. *
  58. * @param string $filename
  59. * @return mixed
  60. */
  61. public function __invoke($filename)
  62. {
  63. return $this->offsetGet($filename);
  64. }
  65. /**
  66. * Set file modification timestamps (query params) for all the media files.
  67. *
  68. * @param string|int|null $timestamp
  69. * @return $this
  70. */
  71. public function setTimestamps($timestamp = null)
  72. {
  73. /** @var Medium $instance */
  74. foreach ($this->items as $instance) {
  75. $instance->setTimestamp($timestamp);
  76. }
  77. return $this;
  78. }
  79. /**
  80. * Get a list of all media.
  81. *
  82. * @return MediaObjectInterface[]
  83. */
  84. public function all()
  85. {
  86. $this->items = $this->orderMedia($this->items);
  87. return $this->items;
  88. }
  89. /**
  90. * Get a list of all image media.
  91. *
  92. * @return MediaObjectInterface[]
  93. */
  94. public function images()
  95. {
  96. $this->images = $this->orderMedia($this->images);
  97. return $this->images;
  98. }
  99. /**
  100. * Get a list of all video media.
  101. *
  102. * @return MediaObjectInterface[]
  103. */
  104. public function videos()
  105. {
  106. $this->videos = $this->orderMedia($this->videos);
  107. return $this->videos;
  108. }
  109. /**
  110. * Get a list of all audio media.
  111. *
  112. * @return MediaObjectInterface[]
  113. */
  114. public function audios()
  115. {
  116. $this->audios = $this->orderMedia($this->audios);
  117. return $this->audios;
  118. }
  119. /**
  120. * Get a list of all file media.
  121. *
  122. * @return MediaObjectInterface[]
  123. */
  124. public function files()
  125. {
  126. $this->files = $this->orderMedia($this->files);
  127. return $this->files;
  128. }
  129. /**
  130. * @param string $name
  131. * @param MediaObjectInterface $file
  132. */
  133. public function add($name, $file)
  134. {
  135. if (!$file) {
  136. return;
  137. }
  138. $this->offsetSet($name, $file);
  139. switch ($file->type) {
  140. case 'image':
  141. $this->images[$name] = $file;
  142. break;
  143. case 'video':
  144. $this->videos[$name] = $file;
  145. break;
  146. case 'audio':
  147. $this->audios[$name] = $file;
  148. break;
  149. default:
  150. $this->files[$name] = $file;
  151. }
  152. }
  153. /**
  154. * Order the media based on the page's media_order
  155. *
  156. * @param array $media
  157. * @return array
  158. */
  159. protected function orderMedia($media)
  160. {
  161. if (null === $this->media_order) {
  162. /** @var Page $page */
  163. $page = Grav::instance()['pages']->get($this->getPath());
  164. if ($page && isset($page->header()->media_order)) {
  165. $this->media_order = array_map('trim', explode(',', $page->header()->media_order));
  166. }
  167. }
  168. if (!empty($this->media_order) && is_array($this->media_order)) {
  169. $media = Utils::sortArrayByArray($media, $this->media_order);
  170. } else {
  171. ksort($media, SORT_NATURAL | SORT_FLAG_CASE);
  172. }
  173. return $media;
  174. }
  175. /**
  176. * Get filename, extension and meta part.
  177. *
  178. * @param string $filename
  179. * @return array
  180. */
  181. protected function getFileParts($filename)
  182. {
  183. if (preg_match('/(.*)@(\d+)x\.(.*)$/', $filename, $matches)) {
  184. $name = $matches[1];
  185. $extension = $matches[3];
  186. $extra = (int) $matches[2];
  187. $type = 'alternative';
  188. if ($extra === 1) {
  189. $type = 'base';
  190. $extra = null;
  191. }
  192. } else {
  193. $fileParts = explode('.', $filename);
  194. $name = array_shift($fileParts);
  195. $extension = null;
  196. $extra = null;
  197. $type = 'base';
  198. while (($part = array_shift($fileParts)) !== null) {
  199. if ($part !== 'meta' && $part !== 'thumb') {
  200. if (null !== $extension) {
  201. $name .= '.' . $extension;
  202. }
  203. $extension = $part;
  204. } else {
  205. $type = $part;
  206. $extra = '.' . $part . '.' . implode('.', $fileParts);
  207. break;
  208. }
  209. }
  210. }
  211. return array($name, $extension, $type, $extra);
  212. }
  213. }