diff --git a/CHANGELOG.md b/CHANGELOG.md index 635362d..f891702 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ All Notable changes to `laravel-feed` will be documented in this file - Fixed compatibility with short php tags +## 1.0.4 - 2016-03-17 + +- Make output more atom compliant + ## 1.0.3 - 2016-03-07 - Add compatibility with short php tags diff --git a/Exceptions/InvalidConfiguration.php b/Exceptions/InvalidConfiguration.php index 57c439c..715bc3b 100644 --- a/Exceptions/InvalidConfiguration.php +++ b/Exceptions/InvalidConfiguration.php @@ -3,6 +3,7 @@ namespace Spatie\Feed\Exceptions; use Exception; + class InvalidConfiguration extends Exception { public static function delimiterNotPresent($configValue) diff --git a/src/Feed.php b/src/Feed.php index 374927f..58231fb 100644 --- a/src/Feed.php +++ b/src/Feed.php @@ -10,6 +10,7 @@ class Feed { /** @var array */ protected $feedConfiguration; + public function __construct(array $feedConfiguration) { $this->feedConfiguration = $feedConfiguration; @@ -17,23 +18,29 @@ public function __construct(array $feedConfiguration) throw InvalidConfiguration::delimiterNotPresent($feedConfiguration['items']); } } + public function getFeedResponse() { return response($this->getFeedContent(), 200, array('Content-Type' => 'application/xml;charset=UTF-8')); } + public function getFeedContent() { list($class, $method) = explode('@', $this->feedConfiguration['items']); + $items = app($class)->{$method}(); + $meta = array('id' => url($this->feedConfiguration['url']), 'link' => url($this->feedConfiguration['url']), 'title' => $this->feedConfiguration['title'], 'updated' => $this->getLastUpdatedDate($items)); return view('laravel-feed::feed', compact('meta', 'items'))->render(); } + protected function getLastUpdatedDate(Collection $items) { if (!count($items)) { return ''; } + $lastItem = $items->sortBy(function (FeedItem $feedItem) { return $feedItem->getFeedItemUpdated()->format('YmdHis'); })->last(); diff --git a/src/FeedItem.php b/src/FeedItem.php index 178925e..581b671 100644 --- a/src/FeedItem.php +++ b/src/FeedItem.php @@ -5,9 +5,14 @@ interface FeedItem { public function getFeedItemId(); + public function getFeedItemTitle(); + public function getFeedItemUpdated(); + public function getFeedItemSummary(); + public function getFeedItemLink(); + public function getFeedItemAuthor(); } diff --git a/src/FeedServiceProvider.php b/src/FeedServiceProvider.php index c87c268..e698b5b 100644 --- a/src/FeedServiceProvider.php +++ b/src/FeedServiceProvider.php @@ -14,33 +14,42 @@ public function boot() $this->loadViewsFrom(__DIR__.'/../resources/views', 'laravel-feed'); $this->bindFeedLinks(); } + public function register() { $this->mergeConfigFrom(__DIR__.'/../config/laravel-feed.php', 'laravel-feed'); $this->registerRouteMacro(); } + protected function registerRouteMacro() { $router = $this->app['router']; + $router->macro('feeds', function ($baseUrl = '') use ($router) { + foreach (config('laravel-feed.feeds') as $index => $feedConfiguration) { $separator = starts_with($feedConfiguration['url'], DIRECTORY_SEPARATOR) ? '' : DIRECTORY_SEPARATOR; $fullUrl = $baseUrl.$separator.$feedConfiguration['url']; $router->get($fullUrl, array('as' => "spatieLaravelFeed{$index}", function () use ($fullUrl, $feedConfiguration) { + $feedConfiguration['url'] = $fullUrl; $feed = new Feed($feedConfiguration); return $feed->getFeedResponse(); + })); } }); } + public function bindFeedLinks() { $feeds = array(); + foreach (config('laravel-feed.feeds') as $index => $feedConfig) { $feeds[] = array('title' => $feedConfig['title'], 'url' => $this->app['url']->route("spatieLaravelFeed{$index}")); } + $this->app->make(Dispatcher::class)->listen('composing: laravel-feed::feed-links', function (View $view) use ($feeds) { $view->with(compact('feeds')); });