CoreServiceProvider::getConfigFilename()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 1
eloc 4
nc 1
nop 2
1
<?php namespace Modules\Core\Providers;
2
3
use Illuminate\Contracts\Bus\Dispatcher;
4
use Illuminate\Routing\Router;
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Support\Facades\Schema;
7
use Illuminate\Support\ServiceProvider;
8
use Maatwebsite\Sidebar\SidebarManager;
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\Sidebar\AdminSidebar;
14
use Pingpong\Modules\Module;
15
16
class CoreServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * Indicates if loading of the provider is deferred.
20
     *
21
     * @var bool
22
     */
23
    protected $defer = false;
24
25
    /**
26
     * @var string
27
     */
28
    protected $prefix = 'asgard';
29
30
    /**
31
     * The filters base class name.
32
     *
33
     * @var array
34
     */
35
    protected $middleware = [
36
        'Core' => [
37
            'permissions'           => 'PermissionMiddleware',
38
            'auth.admin'            => 'AdminMiddleware',
39
            'public.checkLocale'    => 'PublicMiddleware',
40
            'localizationRedirect'  => 'LocalizationMiddleware',
41
        ],
42
    ];
43
44
    public function boot(Dispatcher $dispatcher, SidebarManager $manager)
45
    {
46
        $dispatcher->mapUsing(function ($command) {
47
            $command = str_replace('Commands\\', 'Commands\\Handlers\\', get_class($command));
48
49
            return trim($command, '\\') . 'Handler@handle';
50
        });
51
52
        $manager->register(AdminSidebar::class);
53
54
        $this->registerMiddleware($this->app['router']);
55
        $this->registerModuleResourceNamespaces();
56
        $this->setLocalesConfigurations();
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 ($app) {
67
            try {
68
                $hasTable = Schema::hasTable('setting__settings');
69
            } catch (\Exception $e) {
70
                $hasTable = false;
71
            }
72
73
            return $app['files']->isFile(base_path('.env')) && $hasTable;
74
        });
75
76
        $this->registerCommands();
77
        $this->registerServices();
78
    }
79
80
    /**
81
     * Get the services provided by the provider.
82
     *
83
     * @return array
84
     */
85
    public function provides()
86
    {
87
        return array();
88
    }
89
90
    /**
91
     * Register the filters.
92
     *
93
     * @param  Router $router
94
     * @return void
95
     */
96
    public function registerMiddleware(Router $router)
97
    {
98
        foreach ($this->middleware as $module => $middlewares) {
99
            foreach ($middlewares as $name => $middleware) {
100
                $class = "Modules\\{$module}\\Http\\Middleware\\{$middleware}";
101
102
                $router->middleware($name, $class);
103
            }
104
        }
105
    }
106
107
    /**
108
     * Register the console commands
109
     */
110
    private function registerCommands()
111
    {
112
        $this->commands([
113
            InstallCommand::class,
114
            PublishThemeAssetsCommand::class,
115
            PublishModuleAssetsCommand::class,
116
        ]);
117
    }
118
119
    private function registerServices()
120
    {
121
        $this->app->bindShared(ThemeManager::class, function ($app) {
122
            $path = $app['config']->get('asgard.core.core.themes_path');
123
124
            return new ThemeManager($app, $path);
125
        });
126
    }
127
128
    /**
129
     * Register the modules aliases
130
     */
131
    private function registerModuleResourceNamespaces()
132
    {
133
        foreach ($this->app['modules']->getOrdered() as $module) {
134
            $this->registerViewNamespace($module);
135
            $this->registerLanguageNamespace($module);
136
            $this->registerConfigNamespace($module);
137
        }
138
    }
139
140
    /**
141
     * Register the view namespaces for the modules
142
     * @param Module $module
143
     */
144
    protected function registerViewNamespace(Module $module)
145
    {
146
        if ($module->getName() == 'user') {
147
            return;
148
        }
149
        $this->app['view']->addNamespace(
150
            $module->getName(),
151
            $module->getPath() . '/Resources/views'
152
        );
153
    }
154
155
    /**
156
     * Register the language namespaces for the modules
157
     * @param Module $module
158
     */
159
    protected function registerLanguageNamespace(Module $module)
160
    {
161
        $moduleName = $module->getName();
162
163
        $langPath = base_path("resources/lang/$moduleName");
164
        $secondPath = base_path("resources/lang/translation/$moduleName");
165
166
        if ($moduleName !== 'translation' && $this->hasPublishedTranslations($langPath)) {
167
            return $this->loadTranslationsFrom($langPath, $moduleName);
168
        }
169
        if ($this->hasPublishedTranslations($secondPath)) {
170
            if ($moduleName === 'translation') {
171
                return $this->loadTranslationsFrom($secondPath, $moduleName);
172
            }
173
174
            return $this->loadTranslationsFrom($secondPath, $moduleName);
175
        }
176
        if ($this->moduleHasCentralisedTranslations($module)) {
177
            return $this->loadTranslationsFrom($this->getCentralisedTranslationPath($module), $moduleName);
178
        }
179
180
        return $this->loadTranslationsFrom($module->getPath() . '/Resources/lang', $moduleName);
181
    }
182
183
    /**
184
     * Register the config namespace
185
     * @param Module $module
186
     */
187
    private function registerConfigNamespace(Module $module)
188
    {
189
        $files = $this->app['files']->files($module->getPath() . '/Config');
190
191
        $package = $module->getName();
192
193
        foreach ($files as $file) {
194
            $filename = $this->getConfigFilename($file, $package);
195
196
            $this->mergeConfigFrom($file, $filename);
197
198
            $this->publishes([$file => config_path($filename . '.php'), ], 'config');
199
        }
200
    }
201
202
    /**
203
     * @param $file
204
     * @param $package
205
     * @return string
206
     */
207
    private function getConfigFilename($file, $package)
208
    {
209
        $name = preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($file));
210
211
        $filename = $this->prefix . '.' . $package . '.' . $name;
212
213
        return $filename;
214
    }
215
216
    /**
217
     * Set the locale configuration for
218
     * - laravel localization
219
     * - laravel translatable
220
     */
221
    private function setLocalesConfigurations()
222
    {
223
        if (! $this->app['asgard.isInstalled']) {
224
            return;
225
        }
226
227
        $localeConfig = $this->app['cache']
228
            ->tags('setting.settings', 'global')
229
            ->remember("asgard.locales", 120,
230
                function () {
231
                    return DB::table('setting__settings')->whereName('core::locales')->first();
232
                }
233
            );
234
235
        if ($localeConfig) {
236
            $locales = json_decode($localeConfig->plainValue);
237
            $availableLocales = [];
238
            foreach ($locales as $locale) {
239
                $availableLocales = array_merge($availableLocales, [$locale => config("asgard.core.available-locales.$locale")]);
240
            }
241
242
            $laravelDefaultLocale = $this->app->config->get('app.locale');
0 ignored issues
show
Bug introduced by
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
243
            if (! in_array($laravelDefaultLocale, array_keys($availableLocales))) {
244
                $this->app->config->set('app.locale', array_keys($availableLocales)[0]);
0 ignored issues
show
Bug introduced by
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
245
            }
246
            $this->app->config->set('laravellocalization.supportedLocales', $availableLocales);
0 ignored issues
show
Bug introduced by
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
247
            $this->app->config->set('translatable.locales', $locales);
0 ignored issues
show
Bug introduced by
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
248
        }
249
    }
250
251
    /**
252
     * @param string $path
253
     * @return bool
254
     */
255
    private function hasPublishedTranslations($path)
256
    {
257
        return is_dir($path);
258
    }
259
260
    /**
261
     * Does a Module have it's Translations centralised in the Translation module?
262
     * @param Module $module
263
     * @return bool
264
     */
265
    private function moduleHasCentralisedTranslations(Module $module)
266
    {
267
        if (! array_has($this->app['modules']->enabled(), 'Translation')) {
268
            return false;
269
        }
270
271
        return is_dir($this->getCentralisedTranslationPath($module));
272
    }
273
274
    /**
275
     * Get the absolute path to the Centralised Translations for a Module (via the Translations module)
276
     * @param Module $module
277
     * @return string
278
     */
279
    private function getCentralisedTranslationPath(Module $module)
280
    {
281
        return $this->app['modules']->find('Translation')->getPath() . "/Resources/lang/{$module->getName()}";
282
    }
283
}
284