Issues (238)

src/TranslationServiceProvider.php (1 issue)

1
<?php
2
namespace Translation;
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\ClientInterface;
0 ignored issues
show
The type GuzzleHttp\ClientInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Contracts\Foundation\Application;
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Routing\Router;
9
use Illuminate\Support\Facades\Blade;
10
use Illuminate\Support\Facades\Route;
11
use Illuminate\Support\Facades\Schema;
12
use Illuminate\Support\ServiceProvider;
13
use Illuminate\Translation\FileLoader as LaravelFileLoader;
14
use Illuminate\Translation\TranslationServiceProvider as LaravelTranslationServiceProvider;
15
use Translation\Cache\RepositoryFactory as CacheRepositoryFactory;
16
use Translation\Commands\CacheFlushCommand;
17
use Translation\Commands\FileLoaderCommand;
18
19
use Muleta\Traits\Providers\ConsoleTools;
20
use Translation\Contracts\Translation as TranslationInterface;
21
use Translation\Http\Middleware\LocaleMiddleware;
22
use Translation\Loaders\CacheLoader;
23
use Translation\Loaders\DatabaseLoader;
24
use Translation\Loaders\FileLoader;
25
use Translation\Loaders\MixedLoader;
26
use Translation\Middleware\TranslationMiddleware;
27
use Translation\Models\Translation as TranslationModel;
28
use Translation\Repositories\LanguageRepository;
29
30
use Translation\Repositories\TranslationRepository;
31
use Translation\Routes\ResourceRegistrar;
32
use Translation\Translator\Collection as TranslatorCollection;
33
34
class TranslationServiceProvider extends LaravelTranslationServiceProvider
35
{
36
    
37
    use ConsoleTools;
38
39
    public $packageName = 'translation';
40
    const pathVendor = 'ricardosierra/translation';
41
42
    public static $aliasProviders = [
43
        // 'Translation' => \Translation\Facades\Translation::class,
44
    ];
45
46
    public static $providers = [
47
48
        // \Translation\TranslationServiceProvider::class,
49
50
        
51
    ];
52
    /**
53
     * Indicates if loading of the provider is deferred.
54
     *
55
     * @var bool
56
     */
57
    protected $defer = false;
58
59
    public static $menuItens = [
60
        
61
    ];
62
    
63
    /**
64
     * Bootstrap the application events.
65
     *
66
     * @return void
67
     */
68
    public function boot(Router $router)
69
    {
70
        Schema::defaultStringLength(191);
71
72
        $this->loadMigrationsFrom(__DIR__ . '/../database/migrations/');
73
        $this->publishes(
74
            [
75
            __DIR__ . '/../config/translator.php' => config_path('translator.php'),
76
            __DIR__ . '/../config/translation.php' => config_path('translation.php'),
77
            ]
78
        );
79
80
        $router->pushMiddlewareToGroup('web', LocaleMiddleware::class);
81
82
        $this->routes();
83
84
85
        $this->registerFileLoader();
86
        $this->registerCacheFlusher();
87
        /**
88
         * Provider Antigo
89
         */
90
        Blade::directive(
91
            't',
92
            function ($args) {
93
                return "<?php echo App::make('translation')->translate{$args}; ?>";
94
            }
95
        );
96
        Blade::directive(
97
            'lang',
98
            function ($args) {
99
                return "<?php echo App::make('translation')->translate{$args}; ?>";
100
            }
101
        );
102
103
104
        $this->bootTranslatorCollectionMacros();
105
    }
106
107
    /**
108
     * Register the tool's routes.
109
     *
110
     * @return void
111
     */
112
    protected function routes()
113
    {
114
        if ($this->app->routesAreCached()) {
115
            return;
116
        }
117
118
        /**
119
         * Transmissor; Routes
120
         */
121
        $this->loadRoutesForRiCa(__DIR__.'/../routes');
122
    }
123
124
125
    /**
126
     * Register the service provider.
127
     *
128
     * @return void
129
     */
130
    public function register()
131
    {
132
        $this->mergeConfigFrom(__DIR__ . '/../config/translator.php', 'translator');
133
        $this->mergeConfigFrom(__DIR__ . '/../config/translation.php', 'translation');
134
135
136
        $this->registerCacheRepository();
137
138
        parent::register();
139
140
        // Bind translation to the IoC.
141
        $this->app->bind(
142
            'translation',
143
            function (Application $app) {
144
                return new \Translation\Services\Translation($app);
145
            }
146
        );
147
148
        // Bind translation contract to IoC.
149
        $this->app->bind(TranslationInterface::class, 'translation');
150
151
        $this->app->singleton('translation.uri.localizer', \Translation\Services\UriLocalizer::class);
152
        $this->app[\Illuminate\Routing\Router::class]->aliasMiddleware('localize', TranslationMiddleware::class);
153
        // Fix issue with laravel prepending the locale to localize resource routes:
154
        $this->app->bind('Illuminate\Routing\ResourceRegistrar', ResourceRegistrar::class);
155
156
        // Bind guzzle contract to IoC.
157
        $this->app->bind(ClientInterface::class, Client::class);
158
159
        // Include the helpers file for global `_t()` function
160
        include __DIR__.'/helpers.php';
161
162
163
164
        $this->app['events']->listen(
165
            'eloquent.saving:*',
166
            '\Translation\Observers\Localize'
167
        );
168
169
170
        /*
171
        |--------------------------------------------------------------------------
172
        | Register the Commands
173
        |--------------------------------------------------------------------------
174
        */
175
        // Register commands
176
        $this->registerCommandFolders(
177
            [
178
            base_path('vendor/ricardosierra/translation/src/Console/Commands') => '\Translation\Console\Commands',
179
            ]
180
        );
181
    }
182
183
    /**
184
     *  IOC alias provided by this Service Provider.
185
     *
186
     * @return array
187
     */
188
    public function provides()
189
    {
190
        $this->app->register(\Illuminate\Validation\ValidationServiceProvider::class);
191
192
        return array_merge(parent::provides(), ['translation.cache.repository', 'translation.uri.localizer', 'translation.loader', 'translation']);
193
    }
194
195
    /**
196
     * Register the translation line loader.
197
     *
198
     * @return void
199
     */
200
    protected function registerLoader()
201
    {
202
        $app = $this->app;
203
        $this->app->singleton(
204
            'translation.loader',
205
            function ($app) {
206
                $defaultLocale = $app['config']->get('app.locale');
207
                $loader        = null;
208
                $source        = $app['config']->get('translator.source');
209
210
                switch ($source) {
211
                case 'mixed':
212
                    $laravelFileLoader = new LaravelFileLoader($app['files'], $app->basePath() . '/resources/lang');
213
                    $fileLoader        = new FileLoader($defaultLocale, $laravelFileLoader);
214
                    $databaseLoader    = new DatabaseLoader($defaultLocale, $app->make(TranslationRepository::class));
215
                    $loader            = new MixedLoader($defaultLocale, $fileLoader, $databaseLoader);
216
break;
217
                case 'mixed_db':
218
                    $laravelFileLoader = new LaravelFileLoader($app['files'], $app->basePath() . '/resources/lang');
219
                    $fileLoader        = new FileLoader($defaultLocale, $laravelFileLoader);
220
                    $databaseLoader    = new DatabaseLoader($defaultLocale, $app->make(TranslationRepository::class));
221
                    $loader            = new MixedLoader($defaultLocale, $databaseLoader, $fileLoader);
222
break;
223
                case 'database':
224
                    $loader = new DatabaseLoader($defaultLocale, $app->make(TranslationRepository::class));
225
break;
226
                default:case 'files':
227
                        $laravelFileLoader = new LaravelFileLoader($app['files'], $app->basePath() . '/resources/lang');
228
                        $loader            = new FileLoader($defaultLocale, $laravelFileLoader);
229
break;
230
                }
231
                if ($app['config']->get('translator.cache.enabled')) {
232
                    $loader = new CacheLoader($defaultLocale, $app['translation.cache.repository'], $loader, $app['config']->get('translator.cache.timeout'));
233
                }
234
                return $loader;
235
            }
236
        );
237
    }
238
239
    /**
240
     *  Register the translation cache repository
241
     *
242
     * @return void
243
     */
244
    public function registerCacheRepository()
245
    {
246
        $this->app->singleton(
247
            'translation.cache.repository',
248
            function ($app) {
249
                $cacheStore = $app['cache']->getStore();
250
                return CacheRepositoryFactory::make($cacheStore, $app['config']->get('translator.cache.suffix'));
251
            }
252
        );
253
    }
254
255
    /**
256
     * Register the translator:load language file loader.
257
     *
258
     * @return void
259
     */
260
    protected function registerFileLoader()
261
    {
262
        $app                   = $this->app;
263
        $defaultLocale         = $app['config']->get('app.locale');
264
        $languageRepository    = $app->make(LanguageRepository::class);
265
        $translationRepository = $app->make(TranslationRepository::class);
266
        $translationsPath      = $app->basePath() . '/resources/lang';
267
        $command               = new FileLoaderCommand($languageRepository, $translationRepository, $app['files'], $translationsPath, $defaultLocale);
268
269
        $this->app['command.translator:load'] = $command;
270
        $this->commands('command.translator:load');
271
    }
272
273
    /**
274
     *  Flushes the translation cache
275
     *
276
     * @return void
277
     */
278
    public function registerCacheFlusher()
279
    {
280
        //$cacheStore      = $this->app['cache']->getStore();
281
        //$cacheRepository = CacheRepositoryFactory::make($cacheStore, $this->app['config']->get('translator.cache.suffix'));
282
        $command = new CacheFlushCommand($this->app['translation.cache.repository'], $this->app['config']->get('translator.cache.enabled'));
283
284
        $this->app['command.translator:flush'] = $command;
285
        $this->commands('command.translator:flush');
286
    }
287
288
    protected function bootTranslatorCollectionMacros()
289
    {
290
        Collection::macro(
291
            'translate',
292
            function () {
293
                $transtors = [];
294
295
                foreach ($this->all() as $item) {
296
                    $transtors[] = call_user_func_array([$item, 'translate'], func_get_args());
297
                }
298
299
                return new TranslatorCollection($transtors);
300
            }
301
        );
302
    }
303
}
304