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) { |
|
|
|
|
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
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.