1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Longman\LaravelMultiLang; |
6
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Support\DeferrableProvider; |
8
|
|
|
use Illuminate\Foundation\Events\LocaleUpdated; |
9
|
|
|
use Illuminate\Routing\Events\RouteMatched; |
10
|
|
|
use Illuminate\Support\ServiceProvider; |
11
|
|
|
use Illuminate\View\Compilers\BladeCompiler; |
12
|
|
|
use Longman\LaravelMultiLang\Console\ExportCommand; |
13
|
|
|
use Longman\LaravelMultiLang\Console\ImportCommand; |
14
|
|
|
use Longman\LaravelMultiLang\Console\MigrationCommand; |
15
|
|
|
use Longman\LaravelMultiLang\Console\TextsCommand; |
16
|
|
|
|
17
|
|
|
class MultiLangServiceProvider extends ServiceProvider implements DeferrableProvider |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Bootstrap any application services. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function boot() |
25
|
|
|
{ |
26
|
|
|
// Publish config files |
27
|
|
|
$this->publishes( |
28
|
|
|
[ |
29
|
|
|
__DIR__ . '/../config/config.php' => config_path('multilang.php'), |
30
|
|
|
__DIR__ . '/../views' => base_path('resources/views/vendor/multilang'), |
31
|
|
|
], |
32
|
|
|
); |
|
|
|
|
33
|
|
|
|
34
|
|
|
// Append the country settings |
35
|
|
|
$this->mergeConfigFrom( |
36
|
|
|
__DIR__ . '/../config/config.php', |
37
|
52 |
|
'multilang', |
38
|
|
|
); |
39
|
|
|
|
40
|
52 |
|
// Register blade directives |
41
|
|
|
$this->getBlade()->directive('t', static function ($expression) { |
42
|
52 |
|
return "<?php echo e(t({$expression})); ?>"; |
43
|
52 |
|
}); |
44
|
|
|
|
45
|
|
|
$this->app['events']->listen(RouteMatched::class, function () { |
46
|
|
|
$scope = $this->app['config']->get('app.scope'); |
47
|
|
|
if ($scope && $scope !== 'global') { |
48
|
52 |
|
$this->app['multilang']->setScope($scope); |
49
|
52 |
|
} |
50
|
52 |
|
}); |
51
|
|
|
|
52
|
|
|
$this->app['events']->listen(LocaleUpdated::class, function ($event) { |
53
|
|
|
$this->app['multilang']->setLocale($event->locale); |
54
|
52 |
|
}); |
55
|
|
|
|
56
|
52 |
|
$this->loadViewsFrom(__DIR__ . '/../views', 'multilang'); |
57
|
|
|
} |
58
|
52 |
|
|
59
|
|
|
/** |
60
|
|
|
* Register any application services. |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
52 |
|
*/ |
64
|
|
|
public function register() |
65
|
52 |
|
{ |
66
|
1 |
|
$configPath = __DIR__ . '/../config/config.php'; |
67
|
52 |
|
$this->mergeConfigFrom($configPath, 'multilang'); |
68
|
|
|
|
69
|
52 |
|
$this->app->singleton('multilang', function ($app) { |
70
|
52 |
|
$environment = $app->environment(); |
71
|
|
|
$config = $app['config']->get('multilang'); |
72
|
|
|
|
73
|
|
|
$multilang = new MultiLang( |
74
|
|
|
$environment, |
75
|
|
|
$config, |
76
|
|
|
$app['cache'], |
77
|
52 |
|
$app['db'], |
78
|
|
|
); |
79
|
52 |
|
|
80
|
52 |
|
if ($multilang->autoSaveIsAllowed()) { |
81
|
|
|
$app->terminating(function () use ($multilang) { |
82
|
52 |
|
$scope = $this->app['config']->get('app.scope'); |
83
|
6 |
|
if ($scope && $scope !== 'global') { |
84
|
6 |
|
$multilang->setScope($scope); |
85
|
|
|
} |
86
|
6 |
|
|
87
|
6 |
|
return $multilang->saveTexts(); |
88
|
6 |
|
}); |
89
|
6 |
|
} |
90
|
6 |
|
|
91
|
|
|
return $multilang; |
92
|
|
|
}); |
93
|
6 |
|
|
94
|
|
|
$this->app->alias('multilang', MultiLang::class); |
95
|
|
|
|
96
|
|
|
$this->app->singleton( |
97
|
|
|
'command.multilang.migration', |
98
|
|
|
static function () { |
99
|
|
|
return new MigrationCommand(); |
100
|
|
|
}, |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$this->app->singleton( |
104
|
6 |
|
'command.multilang.texts', |
105
|
52 |
|
static function () { |
106
|
|
|
return new TextsCommand(); |
107
|
52 |
|
}, |
108
|
|
|
); |
109
|
52 |
|
|
110
|
52 |
|
$this->app->singleton( |
111
|
52 |
|
'command.multilang.import', |
112
|
5 |
|
static function () { |
113
|
52 |
|
return new ImportCommand(); |
114
|
|
|
}, |
115
|
|
|
); |
116
|
52 |
|
|
117
|
52 |
|
$this->app->singleton( |
118
|
52 |
|
'command.multilang.export', |
119
|
5 |
|
static function () { |
120
|
52 |
|
return new ExportCommand(); |
121
|
|
|
}, |
122
|
|
|
); |
123
|
52 |
|
|
124
|
52 |
|
$this->commands( |
125
|
52 |
|
[ |
126
|
5 |
|
'command.multilang.migration', |
127
|
52 |
|
'command.multilang.texts', |
128
|
|
|
'command.multilang.import', |
129
|
|
|
'command.multilang.export', |
130
|
52 |
|
], |
131
|
52 |
|
); |
132
|
52 |
|
|
133
|
5 |
|
$this->app->make('request')->macro('locale', static function () { |
134
|
52 |
|
return app('multilang')->getLocale(); |
135
|
|
|
}); |
136
|
|
|
} |
137
|
52 |
|
|
138
|
|
|
/** |
139
|
52 |
|
* Get the services provided by the provider. |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
public function provides() |
144
|
|
|
{ |
145
|
|
|
return [ |
146
|
52 |
|
'multilang', |
147
|
|
|
MultiLang::class, |
148
|
52 |
|
'command.multilang.migration', |
149
|
52 |
|
'command.multilang.texts', |
150
|
|
|
'command.multilang.import', |
151
|
|
|
'command.multilang.export', |
152
|
|
|
]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
private function getBlade(): BladeCompiler |
156
|
3 |
|
{ |
157
|
|
|
return $this->app->make('view')->getEngineResolver()->resolve('blade')->getCompiler(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|