Completed
Push — master ( 9038ff...55d9a6 )
by Sebastian
03:06 queued 11s
created

FeedServiceProvider::feeds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Feed;
4
5
use Spatie\Feed\Helpers\Path;
6
use Illuminate\Support\Facades\View;
7
use Spatie\Feed\Http\FeedController;
8
use Illuminate\Support\ServiceProvider;
9
10
class FeedServiceProvider extends ServiceProvider
11
{
12
    public function boot()
13
    {
14
        $this->publishes([
15
            __DIR__.'/../config/feed.php' => config_path('feed.php'),
16
        ], 'config');
17
18
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'feed');
19
20
        $this->publishes([
21
            __DIR__.'/../resources/views' => resource_path('views/vendor/feed'),
22
        ], 'views');
23
24
        $this->registerLinksComposer();
25
    }
26
27
    public function register()
28
    {
29
        $this->mergeConfigFrom(__DIR__.'/../config/feed.php', 'feed');
30
31
        $this->registerRouteMacro();
32
    }
33
34
    protected function registerRouteMacro()
35
    {
36
        $router = $this->app['router'];
37
38
        $router->macro('feeds', function ($baseUrl = '') use ($router) {
39
            foreach (config('feed.feeds') as $name => $configuration) {
40
                $url = Path::merge($baseUrl, $configuration['url']);
41
42
                $router->get($url, '\\'.FeedController::class)->name("feeds.{$name}");
43
            }
44
        });
45
    }
46
47
    public function registerLinksComposer()
48
    {
49
        View::composer('feed::links', function ($view) {
50
            $view->with('feeds', $this->feeds());
51
        });
52
    }
53
54
    protected function feeds()
55
    {
56
        return collect(config('feed.feeds'))->mapWithKeys(function ($feed, $name) {
57
            return [$name => $feed['title']];
58
        });
59
    }
60
}
61