1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Core\Providers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Router; |
6
|
|
|
use Illuminate\Support\Facades\Blade; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use Illuminate\Support\ServiceProvider; |
9
|
|
|
use Modules\Core\Console\InstallCommand; |
10
|
|
|
use Modules\Core\Console\PublishModuleAssetsCommand; |
11
|
|
|
use Modules\Core\Console\PublishThemeAssetsCommand; |
12
|
|
|
use Modules\Core\Foundation\Theme\ThemeManager; |
13
|
|
|
use Modules\Core\Traits\CanPublishConfiguration; |
14
|
|
|
use Nwidart\Modules\Module; |
15
|
|
|
|
16
|
|
|
class CoreServiceProvider extends ServiceProvider |
17
|
|
|
{ |
18
|
|
|
use CanPublishConfiguration; |
19
|
|
|
/** |
20
|
|
|
* Indicates if loading of the provider is deferred. |
21
|
|
|
* |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $defer = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $prefix = 'asgard'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The filters base class name. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $middleware = [ |
37
|
|
|
'Core' => [ |
38
|
|
|
'permissions' => 'PermissionMiddleware', |
39
|
|
|
'auth.admin' => 'AdminMiddleware', |
40
|
|
|
'public.checkLocale' => 'PublicMiddleware', |
41
|
|
|
'localizationRedirect' => 'LocalizationMiddleware', |
42
|
|
|
'can' => 'Authorization', |
43
|
|
|
], |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
public function boot() |
47
|
|
|
{ |
48
|
|
|
$this->registerMiddleware($this->app['router']); |
49
|
|
|
$this->registerModuleResourceNamespaces(); |
50
|
|
|
|
51
|
|
|
$this->publishConfig('core', 'available-locales'); |
52
|
|
|
$this->publishConfig('core', 'config'); |
53
|
|
|
$this->publishConfig('core', 'core'); |
54
|
|
|
$this->publishConfig('core', 'settings'); |
55
|
|
|
|
56
|
|
|
$this->bladeDirectives(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Register the service provider. |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function register() |
65
|
|
|
{ |
66
|
|
|
$this->app->singleton('asgard.isInstalled', function () { |
67
|
|
|
return true === env('INSTALLED', false); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
$this->registerCommands(); |
71
|
|
|
$this->registerServices(); |
72
|
|
|
$this->setLocalesConfigurations(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the services provided by the provider. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function provides() |
81
|
|
|
{ |
82
|
|
|
return array(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Register the filters. |
87
|
|
|
* |
88
|
|
|
* @param Router $router |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function registerMiddleware(Router $router) |
92
|
|
|
{ |
93
|
|
|
foreach ($this->middleware as $module => $middlewares) { |
94
|
|
|
foreach ($middlewares as $name => $middleware) { |
95
|
|
|
$class = "Modules\\{$module}\\Http\\Middleware\\{$middleware}"; |
96
|
|
|
|
97
|
|
|
$router->middleware($name, $class); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Register the console commands |
104
|
|
|
*/ |
105
|
|
|
private function registerCommands() |
106
|
|
|
{ |
107
|
|
|
$this->commands([ |
108
|
|
|
InstallCommand::class, |
109
|
|
|
PublishThemeAssetsCommand::class, |
110
|
|
|
PublishModuleAssetsCommand::class, |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function registerServices() |
115
|
|
|
{ |
116
|
|
|
$this->app->singleton(ThemeManager::class, function ($app) { |
117
|
|
|
$path = $app['config']->get('asgard.core.core.themes_path'); |
118
|
|
|
|
119
|
|
|
return new ThemeManager($app, $path); |
120
|
|
|
}); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Register the modules aliases |
125
|
|
|
*/ |
126
|
|
|
private function registerModuleResourceNamespaces() |
127
|
|
|
{ |
128
|
|
|
foreach ($this->app['modules']->getOrdered() as $module) { |
129
|
|
|
$this->registerViewNamespace($module); |
130
|
|
|
$this->registerLanguageNamespace($module); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Register the view namespaces for the modules |
136
|
|
|
* @param Module $module |
137
|
|
|
*/ |
138
|
|
|
protected function registerViewNamespace(Module $module) |
139
|
|
|
{ |
140
|
|
|
if ($module->getLowerName() == 'user') { |
141
|
|
|
return; |
142
|
|
|
} |
143
|
|
|
$this->app['view']->addNamespace( |
144
|
|
|
$module->getLowerName(), |
145
|
|
|
$module->getPath() . '/Resources/views' |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Register the language namespaces for the modules |
151
|
|
|
* @param Module $module |
152
|
|
|
*/ |
153
|
|
|
protected function registerLanguageNamespace(Module $module) |
154
|
|
|
{ |
155
|
|
|
$moduleName = $module->getLowerName(); |
156
|
|
|
|
157
|
|
|
$langPath = base_path("resources/lang/$moduleName"); |
158
|
|
|
$secondPath = base_path("resources/lang/translation/$moduleName"); |
159
|
|
|
|
160
|
|
|
if ($moduleName !== 'translation' && $this->hasPublishedTranslations($langPath)) { |
161
|
|
|
return $this->loadTranslationsFrom($langPath, $moduleName); |
162
|
|
|
} |
163
|
|
|
if ($this->hasPublishedTranslations($secondPath)) { |
164
|
|
|
return $this->loadTranslationsFrom($secondPath, $moduleName); |
165
|
|
|
} |
166
|
|
|
if ($this->moduleHasCentralisedTranslations($module)) { |
167
|
|
|
return $this->loadTranslationsFrom($this->getCentralisedTranslationPath($module), $moduleName); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $this->loadTranslationsFrom($module->getPath() . '/Resources/lang', $moduleName); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param $file |
175
|
|
|
* @param $package |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
private function getConfigFilename($file) |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
return preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($file)); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set the locale configuration for |
185
|
|
|
* - laravel localization |
186
|
|
|
* - laravel translatable |
187
|
|
|
*/ |
188
|
|
|
private function setLocalesConfigurations() |
189
|
|
|
{ |
190
|
|
|
if (! $this->app['asgard.isInstalled']) { |
191
|
|
|
return; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$localeConfig = $this->app['cache'] |
195
|
|
|
->tags('setting.settings', 'global') |
196
|
|
|
->remember("asgard.locales", 120, |
197
|
|
|
function () { |
198
|
|
|
return DB::table('setting__settings')->whereName('core::locales')->first(); |
199
|
|
|
} |
200
|
|
|
); |
201
|
|
|
if ($localeConfig) { |
202
|
|
|
$locales = json_decode($localeConfig->plainValue); |
203
|
|
|
$availableLocales = []; |
204
|
|
|
foreach ($locales as $locale) { |
205
|
|
|
$availableLocales = array_merge($availableLocales, [$locale => config("available-locales.$locale")]); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$laravelDefaultLocale = $this->app->config->get('app.locale'); |
|
|
|
|
209
|
|
|
|
210
|
|
|
if (! in_array($laravelDefaultLocale, array_keys($availableLocales))) { |
211
|
|
|
$this->app->config->set('app.locale', array_keys($availableLocales)[0]); |
|
|
|
|
212
|
|
|
} |
213
|
|
|
$this->app->config->set('laravellocalization.supportedLocales', $availableLocales); |
|
|
|
|
214
|
|
|
$this->app->config->set('translatable.locales', $locales); |
|
|
|
|
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param string $path |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
|
|
private function hasPublishedTranslations($path) |
223
|
|
|
{ |
224
|
|
|
return is_dir($path); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Does a Module have it's Translations centralised in the Translation module? |
229
|
|
|
* @param Module $module |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
private function moduleHasCentralisedTranslations(Module $module) |
233
|
|
|
{ |
234
|
|
|
if (! array_has($this->app['modules']->enabled(), 'Translation')) { |
235
|
|
|
return false; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return is_dir($this->getCentralisedTranslationPath($module)); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Get the absolute path to the Centralised Translations for a Module (via the Translations module) |
243
|
|
|
* @param Module $module |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
private function getCentralisedTranslationPath(Module $module) |
247
|
|
|
{ |
248
|
|
|
return $this->app['modules']->find('Translation')->getPath() . "/Resources/lang/{$module->getLowerName()}"; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* List of Custom Blade Directives |
253
|
|
|
*/ |
254
|
|
|
public function bladeDirectives() |
255
|
|
|
{ |
256
|
|
|
/** |
257
|
|
|
* Set variable. |
258
|
|
|
* Usage: @set($variable, value) |
259
|
|
|
*/ |
260
|
|
|
Blade::directive('set', function ($expression) { |
261
|
|
|
list($variable, $value) = $this->getArguments($expression); |
262
|
|
|
|
263
|
|
|
return "<?php {$variable} = {$value}; ?>"; |
264
|
|
|
}); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Get argument array from argument string. |
269
|
|
|
* @param $argumentString |
270
|
|
|
* @return array |
271
|
|
|
*/ |
272
|
|
|
private function getArguments($argumentString) |
273
|
|
|
{ |
274
|
|
|
return str_getcsv(mb_substr($argumentString, 1, mb_strlen($argumentString) - 2), ',', "'"); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|