getMediaFolder(); if (strpos($folder, '://')) { return $folder; } /** @var UniformResourceLocator $locator */ $locator = Grav::instance()['locator']; $user = $locator->findResource('user://'); if (strpos($folder, $user) === 0) { return 'user://' . substr($folder, \strlen($user)+1); } return null; } /** * Gets the associated media collection. * * @return MediaCollectionInterface Representation of associated media. */ public function getMedia() { if ($this->media === null) { $cache = $this->getMediaCache(); // Use cached media if possible. $cacheKey = md5('media' . $this->getCacheKey()); if (!$media = $cache->get($cacheKey)) { $media = new Media($this->getMediaFolder(), $this->getMediaOrder()); $cache->set($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) { $cache = $this->getMediaCache(); $cacheKey = md5('media' . $this->getCacheKey()); $cache->set($cacheKey, $media); $this->media = $media; return $this; } /** * Clear media cache. */ protected function clearMediaCache() { $cache = $this->getMediaCache(); $cacheKey = md5('media' . $this->getCacheKey()); $cache->delete($cacheKey); $this->media = null; } /** * @return CacheInterface */ protected function getMediaCache() { /** @var Cache $cache */ $cache = Grav::instance()['cache']; return $cache->getSimpleCache(); } /** * @return string */ abstract protected function getCacheKey(): string; }