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 (config('app.translations-gui', true) === false) { |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (Schema::hasTable((new Translation)->getTable()) === false) { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the services provided by the provider. |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function provides() |
71
|
|
|
{ |
72
|
|
|
return array(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function registerBindings() |
76
|
|
|
{ |
77
|
|
|
$this->app->bind(TranslationRepository::class, function () { |
78
|
|
|
$repository = new EloquentTranslationRepository(new Translation()); |
|
|
|
|
79
|
|
|
|
80
|
|
|
return new CacheTranslationDecorator($repository); |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
$this->app->bind(FileTranslationRepository::class, function ($app) { |
84
|
|
|
return new FileDiskTranslationRepository($app['files'], $app['translation.loader']); |
85
|
|
|
}); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function registerConsoleCommands() |
89
|
|
|
{ |
90
|
|
|
$this->commands([ |
91
|
|
|
BuildTranslationsCacheCommand::class, |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function registerCustomTranslator() |
96
|
|
|
{ |
97
|
|
|
$this->app->offsetUnset('translation.loader'); |
98
|
|
|
$this->app->offsetUnset('translator'); |
99
|
|
|
|
100
|
|
|
$this->app->singleton('translation.loader', function ($app) { |
101
|
|
|
return new TranslationLoader($app['files'], $app['path.lang']); |
102
|
|
|
}); |
103
|
|
|
$this->app->singleton('translator', function ($app) { |
104
|
|
|
$loader = $app['translation.loader']; |
105
|
|
|
|
106
|
|
|
$locale = $app['config']['app.locale']; |
107
|
|
|
|
108
|
|
|
$trans = new Translator($loader, $locale); |
109
|
|
|
|
110
|
|
|
$trans->setFallback($app['config']['app.fallback_locale']); |
111
|
|
|
|
112
|
|
|
return $trans; |
113
|
|
|
}); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function registerValidators() |
117
|
|
|
{ |
118
|
|
|
Validator::extend('extensions', function ($attribute, $value, $parameters) { |
119
|
|
|
return in_array($value->getClientOriginalExtension(), $parameters); |
120
|
|
|
}); |
121
|
|
|
|
122
|
|
|
Validator::replacer('extensions', function ($message, $attribute, $rule, $parameters) { |
123
|
|
|
return str_replace([':attribute', ':values'], [$attribute, implode(',', $parameters)], $message); |
124
|
|
|
}); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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: