Completed
Pull Request — master (#96)
by
unknown
03:45
created

CoreServiceProvider::hasPublishedTranslations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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
        if ($this->app->config['asgard']['core']['core']['custom-routes']) {
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...
49
            $this->app->booted(function () {
50
                include __DIR__ . '/../../../app/Http/routes.php';
51
            });
52
        }
53
54
        $this->registerMiddleware($this->app['router']);
55
        $this->registerModuleResourceNamespaces();
56
57
        $this->publishConfig('core', 'available-locales');
58
        $this->publishConfig('core', 'config');
59
        $this->publishConfig('core', 'core');
60
        $this->publishConfig('core', 'settings');
61
62
        $this->bladeDirectives();
63
    }
64
65
    /**
66
     * Register the service provider.
67
     *
68
     * @return void
69
     */
70
    public function register()
71
    {
72
        $this->app->singleton('asgard.isInstalled', function () {
73
            return true === env('INSTALLED', false);
74
        });
75
76
        $this->registerCommands();
77
        $this->registerServices();
78
        $this->setLocalesConfigurations();
79
    }
80
81
    /**
82
     * Get the services provided by the provider.
83
     *
84
     * @return array
85
     */
86
    public function provides()
87
    {
88
        return array();
89
    }
90
91
    /**
92
     * Register the filters.
93
     *
94
     * @param  Router $router
95
     * @return void
96
     */
97
    public function registerMiddleware(Router $router)
98
    {
99
        foreach ($this->middleware as $module => $middlewares) {
100
            foreach ($middlewares as $name => $middleware) {
101
                $class = "Modules\\{$module}\\Http\\Middleware\\{$middleware}";
102
103
                $router->middleware($name, $class);
104
            }
105
        }
106
    }
107
108
    /**
109
     * Register the console commands
110
     */
111
    private function registerCommands()
112
    {
113
        $this->commands([
114
            InstallCommand::class,
115
            PublishThemeAssetsCommand::class,
116
            PublishModuleAssetsCommand::class,
117
        ]);
118
    }
119
120
    private function registerServices()
121
    {
122
        $this->app->singleton(ThemeManager::class, function ($app) {
123
            $path = $app['config']->get('asgard.core.core.themes_path');
124
125
            return new ThemeManager($app, $path);
126
        });
127
    }
128
129
    /**
130
     * Register the modules aliases
131
     */
132
    private function registerModuleResourceNamespaces()
133
    {
134
        foreach ($this->app['modules']->getOrdered() as $module) {
135
            $this->registerViewNamespace($module);
136
            $this->registerLanguageNamespace($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->getLowerName() == 'user') {
147
            return;
148
        }
149
        $this->app['view']->addNamespace(
150
            $module->getLowerName(),
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->getLowerName();
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
            return $this->loadTranslationsFrom($secondPath, $moduleName);
171
        }
172
        if ($this->moduleHasCentralisedTranslations($module)) {
173
            return $this->loadTranslationsFrom($this->getCentralisedTranslationPath($module), $moduleName);
174
        }
175
176
        return $this->loadTranslationsFrom($module->getPath() . '/Resources/lang', $moduleName);
177
    }
178
179
    /**
180
     * @param $file
181
     * @param $package
182
     * @return string
183
     */
184
    private function getConfigFilename($file)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
185
    {
186
        return preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($file));
187
    }
188
189
    /**
190
     * Set the locale configuration for
191
     * - laravel localization
192
     * - laravel translatable
193
     */
194
    private function setLocalesConfigurations()
195
    {
196
        if (! $this->app['asgard.isInstalled']) {
197
            return;
198
        }
199
200
        $localeConfig = $this->app['cache']
201
            ->tags('setting.settings', 'global')
202
            ->remember("asgard.locales", 120,
203
                function () {
204
                    return DB::table('setting__settings')->whereName('core::locales')->first();
205
                }
206
            );
207
        if ($localeConfig) {
208
            $locales = json_decode($localeConfig->plainValue);
209
            $availableLocales = [];
210
            foreach ($locales as $locale) {
211
                $availableLocales = array_merge($availableLocales, [$locale => config("available-locales.$locale")]);
212
            }
213
214
            $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...
215
216
            if (! in_array($laravelDefaultLocale, array_keys($availableLocales))) {
217
                $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...
218
            }
219
            $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...
220
            $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...
221
        }
222
    }
223
224
    /**
225
     * @param string $path
226
     * @return bool
227
     */
228
    private function hasPublishedTranslations($path)
229
    {
230
        return is_dir($path);
231
    }
232
233
    /**
234
     * Does a Module have it's Translations centralised in the Translation module?
235
     * @param Module $module
236
     * @return bool
237
     */
238
    private function moduleHasCentralisedTranslations(Module $module)
239
    {
240
        if (! array_has($this->app['modules']->enabled(), 'Translation')) {
241
            return false;
242
        }
243
244
        return is_dir($this->getCentralisedTranslationPath($module));
245
    }
246
247
    /**
248
     * Get the absolute path to the Centralised Translations for a Module (via the Translations module)
249
     * @param Module $module
250
     * @return string
251
     */
252
    private function getCentralisedTranslationPath(Module $module)
253
    {
254
        return $this->app['modules']->find('Translation')->getPath() . "/Resources/lang/{$module->getLowerName()}";
255
    }
256
257
    /**
258
     * List of Custom Blade Directives
259
     */
260
    public function bladeDirectives()
261
    {
262
        /**
263
         * Set variable.
264
         * Usage: @set($variable, value)
265
         */
266
        Blade::directive('set', function ($expression) {
267
            list($variable, $value) = $this->getArguments($expression);
268
269
            return "<?php {$variable} = {$value}; ?>";
270
        });
271
    }
272
273
    /**
274
     * Get argument array from argument string.
275
     * @param $argumentString
276
     * @return array
277
     */
278
    private function getArguments($argumentString)
279
    {
280
        return str_getcsv(mb_substr($argumentString, 1, mb_strlen($argumentString) - 2), ',', "'");
281
    }
282
}
283