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