Completed
Push — master ( 380d73...522216 )
by Ben
02:40
created

SquantoServiceProvider::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Thinktomorrow\Squanto;
4
5
use League\Flysystem\Filesystem;
6
use Thinktomorrow\Squanto\Import\ImportTranslationsCommand;
7
use Thinktomorrow\Squanto\Handlers\ClearCacheTranslations;
8
use Thinktomorrow\Squanto\Handlers\WriteTranslationLineToDisk;
9
use Illuminate\Translation\TranslationServiceProvider as BaseServiceProvider;
10
use League\Flysystem\Adapter\Local;
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
    public function provides()
22
    {
23
        return [
24
            '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 69
    public function boot()
38
    {
39 69
        $this->app['translator']->addNamespace('squanto', $this->getSquantoCachePath());
40
41 69
        if ($this->app->runningInConsole()) {
42 69
            $this->publishes([
43 69
                __DIR__.'/../config/squanto.php' => config_path('squanto.php')
44 69
            ], 'config');
45
46 69
            $this->publishes([
47 69
                __DIR__.'/../database/migrations/create_squanto_tables.php' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_squanto_tables.php'),
48 69
            ], 'migrations');
49
        }
50 69
    }
51
52
    /**
53
     * Register our translator
54
     *
55
     * @return void
56
     */
57 69
    public function register()
58
    {
59 69
        $this->app['squanto.cache_path'] = $this->getSquantoCachePath();
60 69
        $this->app['squanto.lang_path'] = $this->getSquantoLangPath();
61
62 69
        $this->registerTranslator();
63
64
        $this->app->bind(ClearCacheTranslations::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...
65 3
            return new ClearCacheTranslations(
66 3
                new Filesystem(new Local($this->getSquantoCachePath()))
67
            );
68 69
        });
69
70
        $this->app->bind(WriteTranslationLineToDisk::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...
71
            return new WriteTranslationLineToDisk(
72
                new Filesystem(new Local($this->getSquantoCachePath()))
73
            );
74 69
        });
75
76
        $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...
77 27
            return new LaravelTranslationsReader(
78 27
                new Filesystem(new Local($this->getSquantoLangPath()))
79
            );
80 69
        });
81
82 69
        $this->commands([
83 69
            ImportTranslationsCommand::class,
84
        ]);
85
86 69
        $this->mergeConfigFrom(__DIR__.'/../config/squanto.php', 'squanto');
87 69
    }
88
89 69
    private function registerTranslator()
90
    {
91 69
        $this->registerLoader();
92
93 69
        $this->app->singleton('translator', function ($app) {
94
95 69
            $loader = $app['translation.loader'];
96 69
            $locale = $app['config']['app.locale'];
97
98 69
            $trans = new SquantoTranslator($loader, $locale);
99
100 69
            $trans->setFallback($app['config']['app.fallback_locale']);
101
102
            // Custom Squanto option to display key or null when translation is not found
103 69
            $trans->setKeyAsDefault($app['config']['squanto.key_as_default']);
104
105 69
            return $trans;
106 69
        });
107 69
    }
108
109 69
    private function getSquantoCachePath()
110
    {
111 69
        $path = config('squanto.cache_path');
112 69
        return is_null($path) ? storage_path('app/trans') : $path;
113
    }
114
115 69
    private function getSquantoLangPath()
116
    {
117 69
        $path = config('squanto.lang_path');
118 69
        return is_null($path) ? app('path.lang') : $path;
119
    }
120
}
121