FoundationServiceProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 103
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 6 1
A boot() 0 4 1
A detectScope() 0 15 2
A registerCommands() 0 10 2
A provides() 0 8 2
1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\Foundation;
12
13
use Illuminate\Routing\Events\RouteMatched;
14
use Illuminate\Support\ServiceProvider;
15
use Longman\Platfourm\Foundation\Console\ClearCommand;
16
use Longman\Platfourm\Foundation\Console\CompileCommand;
17
use Longman\Platfourm\Foundation\Console\DbClear;
18
use Longman\Platfourm\Foundation\Console\DbDump;
19
use Longman\Platfourm\Foundation\Console\DbRestore;
20
use Longman\Platfourm\Foundation\Console\LogClear;
21
use Longman\Platfourm\Foundation\Events\ApplicationScopeMatched;
22
23
class FoundationServiceProvider extends ServiceProvider
24
{
25
26
    /**
27
     * Indicates if loading of the provider is deferred.
28
     *
29
     * @var bool
30
     */
31
    protected $defer = false;
32
33
    /**
34
     * The commands to be registered.
35
     *
36
     * @var array
37
     */
38
    protected $commands = [
39
        'command.clear'      => ClearCommand::class,
40
        'command.compile'    => CompileCommand::class,
41
        'command.db.clear'   => DbClear::class,
42
        'command.db.dump'    => DbDump::class,
43
        'command.db.restore' => DbRestore::class,
44
        'command.log.clear'  => LogClear::class,
45
    ];
46
47
    /**
48
     * The commands to be registered.
49
     *
50
     * @var array
51
     */
52
    protected $devCommands = [
53
54
    ];
55
56
    /**
57
     * Register the service provider.
58
     *
59
     * @return void
60
     */
61
    public function register()
62
    {
63
        $this->registerCommands($this->commands);
64
65
        $this->registerCommands($this->devCommands);
66
    }
67
68
    /**
69
     * Bootstrap the application services.
70
     *
71
     * @return void
72
     */
73
    public function boot()
74
    {
75
        $this->detectScope();
76
    }
77
78
    protected function detectScope()
79
    {
80
        // set global scope if no routing involved
81
        $this->app['config']->set('app.scope', 'global');
82
83
        // set application scope
84
        $this->app['events']->listen(RouteMatched::class, function ($match) {
85
            $routeName    = $match->route->getName();
86
            $admin_prefix = $this->app['config']->get('cms.admin_prefix', 'admin');
87
            $isAdmin      = strpos($routeName, $admin_prefix . '.') !== false;
88
            $scope        = $isAdmin ? 'admin' : 'site';
89
            $this->app['config']->set('app.scope', $scope);
90
            $this->app['events']->fire(new ApplicationScopeMatched($scope));
91
        });
92
    }
93
94
    /**
95
     * Register the given commands.
96
     *
97
     * @param  array $commands
98
     * @return void
99
     */
100
    protected function registerCommands(array $commands)
101
    {
102
        foreach ($commands as $command => $class) {
103
            $this->app->singleton($command, function ($app) use ($class) {
0 ignored issues
show
Unused Code introduced by
The parameter $app 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...
104
                return new $class;
105
            });
106
        }
107
108
        $this->commands(array_keys($commands));
109
    }
110
111
    /**
112
     * Get the services provided by the provider.
113
     *
114
     * @return array
115
     */
116
    public function provides()
117
    {
118
        if ($this->app->environment('production')) {
119
            return array_keys($this->commands);
120
        } else {
121
            return array_merge(array_keys($this->commands), array_keys($this->devCommands));
122
        }
123
    }
124
125
}
126