Completed
Push — master ( fd6048...954cdd )
by Peter
11:26
created

ThemesServiceProvider::mapWebRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 1
b 0
f 1
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) {
82
            require base_path('themes/'.$this->app['themes']->getTheme().'/routes/web.php');
83
        });
84
    }
85
86
    /**
87
     * Define the "api" routes for the theme.
88
     *
89
     * These routes are typically stateless.
90
     *
91
     * @return void
92
     */
93
    protected function mapApiRoutes()
94
    {
95
        Route::group([
96
            'middleware' => ['api', 'auth:api'],
97
            'namespace' => $this->namespace,
0 ignored issues
show
Bug introduced by
The property namespace does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
98
            'prefix' => 'api',
99
        ], function ($router) {
100
            require basepath('themes/'.$this->app['themes']->getTheme().'routes/api.php');
101
        });
102
    }
103
}
104