ThemesServiceProvider::register()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\CMS\Themes;
4
5
use Illuminate\Support\ServiceProvider;
6
use Symfony\Component\Finder\Finder;
7
use Yajra\CMS\Themes\Repositories\CollectionRepository;
8
use Yajra\CMS\Themes\Repositories\Repository;
9
10
class ThemesServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * Bootstrap the application services.
14
     *
15
     * @return void
16
     */
17
    public function boot()
18
    {
19
        /** @var \Illuminate\View\Factory $view */
20
        $view = $this->app['view'];
21
        $finder = $view->getFinder();
22
        $hints = $finder->getHints();
23
        $view->setFinder($this->app['themes.view.finder']);
24
        foreach ($hints as $namespace => $path) {
25
            $view->addNamespace($namespace, $path);
26
        }
27
28
        $this->registerConfig();
29
        $this->registerViews();
30
        $this->registerLang();
31
        $this->registerRoute();
32
33
        $this->registerAdminTheme();
34
35
        /** @var \Yajra\CMS\Themes\Repositories\Repository $themes */
36
        $themes = $this->app['themes'];
37
        $themes->scan();
38
    }
39
40
    protected function registerConfig()
41
    {
42
        $this->publishes([
43
            __DIR__ . '/config/themes.php' => config_path('themes.php'),
44
        ], 'cms-themes');
45
        $this->mergeConfigFrom(__DIR__ . '/config/themes.php', 'themes');
46
    }
47
48
    /**
49
     * Register themes view namespace.
50
     */
51
    protected function registerViews()
52
    {
53
        $this->publishes([
54
            __DIR__ . '/resources/views' => resource_path('views/vendor/themes'),
55
        ], 'cms-themes');
56
        $this->loadViewsFrom(__DIR__ . '/resources/views', 'themes');
57
    }
58
59
    /**
60
     * Register translations.
61
     */
62
    protected function registerLang()
63
    {
64
        $this->loadTranslationsFrom(__DIR__ . '/resources/lang', 'themes');
65
    }
66
67
    /**
68
     * Register administrator/themes routes.
69
     */
70
    protected function registerRoute()
71
    {
72
        /** @var \Illuminate\Routing\Router $router */
73
        $router = $this->app['router'];
74
        $router->group(['prefix' => admin_prefix(), 'middleware' => 'administrator'], function () use ($router) {
75
            $router->get('themes', ThemesController::class . '@index')->name('administrator.themes.index');
76
            $router->get('themes/create', ThemesController::class . '@create')->name('administrator.themes.create');
77
            $router->post('themes', ThemesController::class . '@store')->name('administrator.themes.store');
78
            $router->get('themes/{theme}', ThemesController::class . '@edit')->name('administrator.themes.edit');
79
            $router->put('themes/{theme}', ThemesController::class . '@update')->name('administrator.themes.update');
80
            $router->delete('themes/{theme}', ThemesController::class . '@destroy')
81
                   ->name('administrator.themes.destroy');
82
        });
83
    }
84
85
    /**
86
     * Register admin view namespace.
87
     */
88
    protected function registerAdminTheme()
89
    {
90
        $adminTheme = config('themes.backend', 'default');
91
        $basePath   = config('themes.path.backend', base_path('themes/backend'));
92
        $this->loadViewsFrom($basePath . DIRECTORY_SEPARATOR . $adminTheme, 'admin');
93
    }
94
95
    /**
96
     * Register the application services.
97
     *
98
     * @return void
99
     */
100
    public function register()
101
    {
102
        $this->app->singleton('themes.view.finder', function ($app) {
103
            $finder   = new ThemeViewFinder($app['files'], $app['config']['view.paths']);
104
            $basePath = config('themes.path.frontend', base_path('themes/frontend'));
105
            $theme    = config('themes.frontend', 'default');
106
            $finder->setBasePath($basePath . DIRECTORY_SEPARATOR . $theme);
107
108
            return $finder;
109
        });
110
111
        $this->app->singleton('themes', function () {
112
            return new CollectionRepository(new Finder, $this->app['config']);
113
        });
114
115
        $this->app->alias('themes', Repository::class);
116
    }
117
}
118