Completed
Push — master ( 46da09...e34c89 )
by Peter
05:14
created

ThemesServiceProvider::loadRouteFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace PeterColes\Themes;
4
5
use Illuminate\Support\Facades\Route;
6
use Illuminate\Support\ServiceProvider;
7
8
class ThemesServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * Register any application services.
12
     *
13
     * @return void
14
     */
15
    public function register()
16
    {
17
        $this->registerViewFinder();
18
19
        $this->registerThemes();
20
    }
21
22
    /**
23
     * Perform post-registration booting of services.
24
     *
25
     * @return void
26
     */
27
    public function boot()
28
    {
29
        $this->publishes([
30
            __DIR__.'/../config/themes.php' => config_path('themes.php'),
31
        ]);
32
33
        $this->app['themes']->setTheme($this->app['request']);
34
35
        $this->mapWebRoutes();
36
37
        $this->mapApiRoutes();
38
    }
39
40
    /**
41
     * Re-register the view finder to use use local copy.
42
     *
43
     * @return void
44
     */
45
    protected function registerViewFinder()
46
    {
47
        $this->app['view.finder'] = $this->app->share(function($app) {
48
            $paths = $app['config']['view.paths'];
49
50
            return new FileViewFinder($app['files'], $paths);
51
        });
52
53
        // Apply this finder to the already-registered view factory
54
        $this->app['view']->setFinder($this->app['view.finder']);
55
    }
56
57
    /**
58
     * Register the themes service.
59
     *
60
     * @return void
61
     */
62
    protected function registerThemes()
63
    {
64
        $this->app->singleton('themes', function() {
65
            return new Themes;
66
        });
67
    }
68
69
    /**
70
     * Define the "web" routes for the theme.
71
     *
72
     * These routes all receive session state, CSRF protection, etc.
73
     *
74
     * @return void
75
     */
76
    protected function mapWebRoutes()
77
    {
78
        Route::group([
79
            'middleware' => 'web',
80
            'namespace' => 'App\Http\Controllers',
81
        ], function ($router) {
0 ignored issues
show
Unused Code introduced by
The parameter $router is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
            $this->loadRouteFile('web');
83
        });
84
    }
85
86
    /**
87
     * Define the "api" routes for the theme. Routes are in two groups, those requiring
88
     * a user to be authenticated and another for routes that don't require any authentication.
89
     *
90
     * These routes are typically stateless.
91
     *
92
     * @return void
93
     */
94
    protected function mapApiRoutes()
95
    {
96
        Route::group([
97
            'middleware' => ['api', 'auth:api'],
98
            'namespace' => 'App\Http\Controllers',
99
            'prefix' => 'api',
100
        ], function ($router) {
0 ignored issues
show
Unused Code introduced by
The parameter $router is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
            $this->loadRouteFile('api');
102
        });
103
104
        Route::group([
105
            'middleware' => ['api'],
106
            'namespace' => 'App\Http\Controllers',
107
            'prefix' => 'api',
108
        ], function ($router) {
0 ignored issues
show
Unused Code introduced by
The parameter $router is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
            $this->loadRouteFile('api-no-auth');
110
        });
111
    }
112
113
    /**
114
     * Load route file, but only after checking whether it exists in the theme.
115
     *
116
     * @return void
117
     */
118
    protected function loadRouteFile($routeFile)
119
    {
120
        $routeFile = base_path('themes/'.$this->app['themes']->getTheme().'/routes/'.$routeFile.'.php');
121
        if (file_exists($routeFile)) {
122
            require $routeFile;
123
        }
124
    }
125
}
126