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