Пример #1
0
 public function renderLatestBlogPosts()
 {
     $cacheId = $this->blogOptions->getCacheKey();
     $posts = $this->cache->contains($cacheId) ? $this->cache->fetch($cacheId) : [];
     $elements = [];
     foreach ($posts as $post) {
         $elements[] = sprintf('<li><a target="_blank" href="%s">%s</a></li>', $post['link'], $post['title']);
     }
     return sprintf('<ul class="fh5co-links blog-posts">%s</ul>', implode('', $elements));
 }
 /**
  * @test
  */
 public function cacheIsDeletedWhenFeedHasChyanged()
 {
     $this->templatesCache->save('foo', 'bar');
     $this->feedCache->save($this->options->getCacheKey(), [['link' => 'something else']]);
     $this->service->refreshFeed();
     $this->assertFalse($this->templatesCache->contains('foo'));
 }
 public function refreshFeed() : array
 {
     $cacheId = $this->blogOptions->getCacheKey();
     $feed = Reader::import($this->blogOptions->getFeed());
     $feed = $this->processFeed($feed);
     // If no feed has been cached yet, cache current one and return
     if (!$this->feedCache->contains($cacheId)) {
         $this->templatesCache->deleteAll();
         $this->feedCache->save($cacheId, $feed);
         return $feed;
     }
     // Check if the last feed has changed, otherwise, return
     $cachedFeed = $this->feedCache->fetch($cacheId);
     if ($cachedFeed[0]['link'] === $feed[0]['link']) {
         return $feed;
     }
     // If the feed has changed, clear all cached elements so that views are refreshed, and cache feed too
     $this->templatesCache->deleteAll();
     $this->feedCache->save($cacheId, $feed);
     return $feed;
 }