|
1
|
|
|
<?php namespace Modules\Translation\Providers; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\Validator; |
|
4
|
|
|
use Illuminate\Support\ServiceProvider; |
|
5
|
|
|
use Modules\Core\Composers\CurrentUserViewComposer; |
|
6
|
|
|
use Modules\Translation\Console\BuildTranslationsCacheCommand; |
|
7
|
|
|
use Modules\Translation\Entities\Translation; |
|
8
|
|
|
use Modules\Translation\Repositories\Cache\CacheTranslationDecorator; |
|
9
|
|
|
use Modules\Translation\Repositories\Eloquent\EloquentTranslationRepository; |
|
10
|
|
|
use Modules\Translation\Repositories\File\FileTranslationRepository as FileDiskTranslationRepository; |
|
11
|
|
|
use Modules\Translation\Repositories\FileTranslationRepository; |
|
12
|
|
|
use Modules\Translation\Repositories\TranslationRepository; |
|
13
|
|
|
use Modules\Translation\Services\TranslationLoader; |
|
14
|
|
|
use Modules\Translation\Services\Translator; |
|
15
|
|
|
use Schema; |
|
16
|
|
|
|
|
17
|
|
|
class TranslationServiceProvider extends ServiceProvider |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Indicates if loading of the provider is deferred. |
|
21
|
|
|
* |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $defer = false; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Register the service provider. |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public function register() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->registerBindings(); |
|
34
|
|
|
$this->registerConsoleCommands(); |
|
35
|
|
|
|
|
36
|
|
|
view()->composer('translation::admin.translations.index', CurrentUserViewComposer::class); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function boot() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->registerValidators(); |
|
42
|
|
|
|
|
43
|
|
|
if ($this->shouldRegisterCustomTranslator()) { |
|
44
|
|
|
$this->registerCustomTranslator(); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Should we register the Custom Translator? |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function shouldRegisterCustomTranslator() |
|
53
|
|
|
{ |
|
54
|
|
|
if (false === config('app.translations-gui', true)) { |
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (false === app('asgard.isInstalled')) { |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (false === Schema::hasTable((new Translation)->getTable())) { |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the services provided by the provider. |
|
71
|
|
|
* |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
|
|
public function provides() |
|
75
|
|
|
{ |
|
76
|
|
|
return array(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function registerBindings() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->app->bind(TranslationRepository::class, function () { |
|
82
|
|
|
$repository = new EloquentTranslationRepository(new Translation()); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
if (! config('app.cache')) { |
|
85
|
|
|
return $repository; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return new CacheTranslationDecorator($repository); |
|
89
|
|
|
}); |
|
90
|
|
|
|
|
91
|
|
|
$this->app->bind(FileTranslationRepository::class, function ($app) { |
|
92
|
|
|
return new FileDiskTranslationRepository($app['files'], $app['translation.loader']); |
|
93
|
|
|
}); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
private function registerConsoleCommands() |
|
97
|
|
|
{ |
|
98
|
|
|
$this->commands([ |
|
99
|
|
|
BuildTranslationsCacheCommand::class, |
|
100
|
|
|
]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function registerCustomTranslator() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->app->offsetUnset('translation.loader'); |
|
106
|
|
|
$this->app->offsetUnset('translator'); |
|
107
|
|
|
|
|
108
|
|
|
$this->app->singleton('translation.loader', function ($app) { |
|
109
|
|
|
return new TranslationLoader($app['files'], $app['path.lang']); |
|
110
|
|
|
}); |
|
111
|
|
|
$this->app->singleton('translator', function ($app) { |
|
112
|
|
|
$loader = $app['translation.loader']; |
|
113
|
|
|
|
|
114
|
|
|
$locale = $app['config']['app.locale']; |
|
115
|
|
|
|
|
116
|
|
|
$trans = new Translator($loader, $locale); |
|
117
|
|
|
|
|
118
|
|
|
$trans->setFallback($app['config']['app.fallback_locale']); |
|
119
|
|
|
|
|
120
|
|
|
return $trans; |
|
121
|
|
|
}); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
private function registerValidators() |
|
125
|
|
|
{ |
|
126
|
|
|
Validator::extend('extensions', function ($attribute, $value, $parameters) { |
|
127
|
|
|
return in_array($value->getClientOriginalExtension(), $parameters); |
|
128
|
|
|
}); |
|
129
|
|
|
|
|
130
|
|
|
Validator::replacer('extensions', function ($message, $attribute, $rule, $parameters) { |
|
131
|
|
|
return str_replace([':attribute', ':values'], [$attribute, implode(',', $parameters)], $message); |
|
132
|
|
|
}); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: