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
|
|
|
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->loadTranslationsFrom($this->getSquantoCachePath(), 'squanto'); |
40
|
|
|
// $this->app['translator']->addNamespace('squanto', $this->getSquantoCachePath()); |
|
|
|
|
41
|
69 |
|
|
42
|
69 |
|
if ($this->app->runningInConsole()) { |
43
|
69 |
|
$this->publishes([ |
44
|
69 |
|
__DIR__.'/../config/squanto.php' => config_path('squanto.php') |
45
|
|
|
], 'config'); |
46
|
69 |
|
|
47
|
69 |
|
if (!class_exists('CreateSquantoTables')) { |
48
|
69 |
|
$this->publishes([ |
49
|
|
|
__DIR__.'/../database/migrations/create_squanto_tables.php' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_squanto_tables.php'), |
50
|
69 |
|
], 'migrations'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->commands([ |
54
|
|
|
ImportTranslationsCommand::class, |
55
|
|
|
CacheTranslationsCommand::class, |
56
|
|
|
]); |
57
|
69 |
|
} |
58
|
|
|
} |
59
|
69 |
|
|
60
|
69 |
|
/** |
61
|
|
|
* Register our translator |
62
|
69 |
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
3 |
|
public function register() |
66
|
3 |
|
{ |
67
|
|
|
$this->app['squanto.cache_path'] = $this->getSquantoCachePath(); |
68
|
69 |
|
$this->app['squanto.lang_path'] = $this->getSquantoLangPath(); |
69
|
|
|
|
70
|
|
|
$this->registerTranslator(); |
71
|
|
|
|
72
|
|
|
$this->app->bind(CachedTranslationFile::class, function ($app) { |
|
|
|
|
73
|
|
|
return new CachedTranslationFile( |
74
|
69 |
|
new Filesystem(new Local($this->getSquantoCachePath())) |
75
|
|
|
); |
76
|
|
|
}); |
77
|
27 |
|
|
78
|
27 |
|
$this->app->bind(LaravelTranslationsReader::class, function ($app) { |
|
|
|
|
79
|
|
|
return new LaravelTranslationsReader( |
80
|
69 |
|
new Filesystem(new Local($this->getSquantoLangPath())) |
81
|
|
|
); |
82
|
69 |
|
}); |
83
|
69 |
|
|
84
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/squanto.php', 'squanto'); |
85
|
|
|
} |
86
|
69 |
|
|
87
|
69 |
|
private function registerTranslator() |
88
|
|
|
{ |
89
|
69 |
|
$this->registerLoader(); |
90
|
|
|
|
91
|
69 |
|
$this->app->singleton('translator', function ($app) { |
92
|
|
|
|
93
|
69 |
|
$loader = $app['translation.loader']; |
94
|
|
|
$locale = $app['config']['app.locale']; |
95
|
69 |
|
|
96
|
69 |
|
$trans = new SquantoTranslator($loader, $locale); |
97
|
|
|
|
98
|
69 |
|
$trans->setFallback($app['config']['app.fallback_locale']); |
99
|
|
|
|
100
|
69 |
|
// Custom Squanto option to display key or null when translation is not found |
101
|
|
|
$trans->setKeyAsDefault($app['config']['squanto.key_as_default']); |
102
|
|
|
|
103
|
69 |
|
return $trans; |
104
|
|
|
}); |
105
|
69 |
|
} |
106
|
69 |
|
|
107
|
69 |
|
private function getSquantoCachePath() |
108
|
|
|
{ |
109
|
69 |
|
$path = config('squanto.cache_path'); |
110
|
|
|
return is_null($path) ? storage_path('app/trans') : $path; |
111
|
69 |
|
} |
112
|
69 |
|
|
113
|
|
|
private function getSquantoLangPath() |
114
|
|
|
{ |
115
|
69 |
|
$path = config('squanto.lang_path'); |
116
|
|
|
return is_null($path) ? app('path.lang') : $path; |
117
|
69 |
|
} |
118
|
|
|
} |
119
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.