Completed
Push — master ( 809eb4...34764a )
by Jolita
11:58
created

FeedServiceProvider::bindFeedsLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Feed;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\Events\Dispatcher;
7
use Illuminate\View\View;
8
9
class FeedServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Bootstrap the application services.
13
     */
14
    public function boot()
15
    {
16
        $this->publishes([
17
            __DIR__.'/../config/laravel-feed.php' => config_path('laravel-feed.php'),
18
            __DIR__.'/../config/feed-link.php' => config_path('feed-link    .php'),
19
        ], 'config');
20
21
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'laravel-feed');
22
23
        $this->registerFeeds();
24
25
        $this->bindFeedsLinks();
26
    }
27
28
    /**
29
     * Register the application services.
30
     */
31
    public function register()
32
    {
33
        $this->mergeConfigFrom(__DIR__.'/../config/laravel-feed.php', 'laravel-feed');
34
35
        $this->app->singleton(Feed::class);
36
    }
37
38
    /**
39
     * Gets feeds routes and generates feeds.
40
     */
41
    protected function registerFeeds()
42
    {
43
        collect(config('laravel-feed.feeds'))->each(function ($feedConfiguration) {
44
            if (!$feedConfiguration['url']) {
45
                return;
46
            }
47
            $this->registerRoute($feedConfiguration);
48
        });
49
    }
50
51
    protected function registerRoute($feedConfiguration)
52
    {
53
        $this->app['router']->get($feedConfiguration['url'], function () use ($feedConfiguration) {
54
55
            return $this->app->make(Feed::class)->getFeedResponse($feedConfiguration);
56
57
        });
58
    }
59
60
    public function bindFeedsLinks()
61
    {
62
        $this->app->make(Dispatcher::class)->listen("composing: laravel-feed::feed-links", function (View $view) {
63
            $view->with(['feeds' => config('laravel-feed.feeds')]);
64
        });
65
    }
66
}
67