Completed
Pull Request — master (#12)
by Philippe
12:17
created

SquantoServiceProvider::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 2
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Squanto;
4
5
use League\Flysystem\Filesystem;
6
use Thinktomorrow\Squanto\Services\CachedTranslationFile;
7
use Thinktomorrow\Squanto\Import\ImportTranslationsCommand;
8
use Illuminate\Translation\TranslationServiceProvider as BaseServiceProvider;
9
use League\Flysystem\Adapter\Local;
10
use Thinktomorrow\Squanto\Services\CacheTranslationsCommand;
11
use Thinktomorrow\Squanto\Services\LaravelTranslationsReader;
12
use Thinktomorrow\Squanto\Translators\SquantoTranslator;
13
14
class SquantoServiceProvider extends BaseServiceProvider
15
{
16
    protected $defer = true;
17
18
    /**
19
     * @return array
20
     */
21 1
    public function provides()
22
    {
23
        return [
24 1
            'translator',
25
            'translation.loader',
26
            'Thinktomorrow\\Squanto\\Handlers\\ClearCacheTranslations',
27
            'Thinktomorrow\\Squanto\\Handlers\\WriteTranslationLineToDisk',
28
            'Thinktomorrow\\Squanto\\Services\\LaravelTranslationsReader',
29
        ];
30
    }
31
32
    /**
33
     * Bootstrap any application services.
34
     *
35
     * @return void
36
     */
37 62
    public function boot()
38
    {
39 62
        $this->loadTranslationsFrom($this->getSquantoCachePath(), 'squanto');
40
41 62
        if ($this->app->runningInConsole()) {
42 62
            $this->publishes([
43 62
                __DIR__.'/../config/squanto.php' => config_path('squanto.php')
44 62
            ], 'config');
45
46 62
            $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
47
48 62
            $this->commands([
49 62
                ImportTranslationsCommand::class,
50
                CacheTranslationsCommand::class,
51
            ]);
52
        }
53 62
    }
54
55
    /**
56
     * Register our translator
57
     *
58
     * @return void
59
     */
60 62
    public function register()
61
    {
62 62
        $this->app['squanto.cache_path'] = $this->getSquantoCachePath();
63 62
        $this->app['squanto.lang_path'] = $this->getSquantoLangPath();
64
65 62
        $this->registerTranslator();
66
67
        $this->app->bind(CachedTranslationFile::class, function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68 5
            return new CachedTranslationFile(
69 5
                new Filesystem(new Local($this->getSquantoCachePath()))
70
            );
71 62
        });
72
73
        $this->app->bind(LaravelTranslationsReader::class, function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74 5
            return new LaravelTranslationsReader(
75 5
                new Filesystem(new Local($this->getSquantoLangPath()))
76
            );
77 62
        });
78
79 62
        $this->mergeConfigFrom(__DIR__.'/../config/squanto.php', 'squanto');
80 62
    }
81
82 62
    private function registerTranslator()
83
    {
84 62
        $this->registerLoader();
85
86
        $this->app->singleton('translator', function ($app) {
87
88 62
            $loader = $app['translation.loader'];
89 62
            $locale = $app['config']['app.locale'];
90
91 62
            $trans = new SquantoTranslator($loader, $locale);
92
93 62
            $trans->setFallback($app['config']['app.fallback_locale']);
94
95
            // Custom Squanto option to display key or null when translation is not found
96 62
            $trans->setKeyAsDefault($app['config']['squanto.key_as_default']);
97
98 62
            return $trans;
99 62
        });
100 62
    }
101
102 62
    private function getSquantoCachePath()
103
    {
104 62
        $path = config('squanto.cache_path');
105 62
        return is_null($path) ? storage_path('app/trans') : $path;
106
    }
107
108 62
    private function getSquantoLangPath()
109
    {
110 62
        $path = config('squanto.lang_path');
111 62
        return is_null($path) ? app('path.lang') : $path;
112
    }
113
}
114