Completed
Pull Request — master (#24)
by Sebastian
02:45 queued 48s
created

FeedServiceProvider::mergePaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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