1 | <?php |
||
23 | class MultiLangServiceProvider extends ServiceProvider |
||
24 | { |
||
25 | /** |
||
26 | * Indicates if loading of the provider is deferred. |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | protected $defer = false; |
||
31 | |||
32 | /** |
||
33 | * Bootstrap any application services. |
||
34 | * |
||
35 | * @return void |
||
36 | */ |
||
37 | 43 | public function boot() |
|
38 | { |
||
39 | // Publish config files |
||
40 | 43 | $this->publishes( |
|
41 | [ |
||
42 | 43 | __DIR__ . '/../config/config.php' => config_path('multilang.php'), |
|
43 | 43 | __DIR__ . '/../views' => base_path('resources/views/vendor/multilang'), |
|
44 | ] |
||
45 | ); |
||
46 | |||
47 | // Append the country settings |
||
48 | 43 | $this->mergeConfigFrom( |
|
49 | 43 | __DIR__ . '/../config/config.php', |
|
50 | 43 | 'multilang' |
|
51 | ); |
||
52 | |||
53 | // Register blade directives |
||
54 | 43 | Blade::directive('t', function ($expression) { |
|
55 | return "<?php echo e(t({$expression})); ?>"; |
||
56 | 43 | }); |
|
57 | |||
58 | 43 | $this->app['events']->listen(RouteMatched::class, function () { |
|
59 | $scope = $this->app['config']->get('app.scope'); |
||
60 | if ($scope && $scope != 'global') { |
||
61 | $this->app['multilang']->setScope($scope); |
||
62 | } |
||
63 | 43 | }); |
|
64 | |||
65 | 43 | $this->app['events']->listen(LocaleUpdated::class, function ($event) { |
|
66 | 1 | $this->app['multilang']->setLocale($event->locale); |
|
67 | 43 | }); |
|
68 | |||
69 | 43 | $this->loadViewsFrom(__DIR__ . '/../views', 'multilang'); |
|
70 | 43 | } |
|
71 | |||
72 | /** |
||
73 | * Register any application services. |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | 43 | public function register() |
|
78 | { |
||
79 | 43 | $configPath = __DIR__ . '/../config/config.php'; |
|
80 | 43 | $this->mergeConfigFrom($configPath, 'debugbar'); |
|
81 | |||
82 | 43 | $this->app->singleton('multilang', function ($app) { |
|
83 | 4 | $environment = $app->environment(); |
|
84 | 4 | $config = $app['config']->get('multilang'); |
|
85 | |||
86 | 4 | $multilang = new \Longman\LaravelMultiLang\MultiLang( |
|
87 | 4 | $environment, |
|
88 | 4 | $config, |
|
89 | 4 | $app['cache'], |
|
90 | 4 | $app['db'] |
|
91 | ); |
||
92 | |||
93 | 4 | if ($multilang->autoSaveIsAllowed()) { |
|
94 | $app->terminating(function () use ($multilang) { |
||
95 | $scope = $this->app['config']->get('app.scope'); |
||
96 | if ($scope && $scope != 'global') { |
||
97 | $multilang->setScope($scope); |
||
98 | } |
||
99 | |||
100 | return $multilang->saveTexts(); |
||
101 | }); |
||
102 | } |
||
103 | |||
104 | 4 | return $multilang; |
|
105 | 43 | }); |
|
106 | |||
107 | 43 | $this->app->alias('multilang', 'Longman\LaravelMultiLang\MultiLang'); |
|
108 | |||
109 | 43 | $this->app->singleton( |
|
110 | 43 | 'command.multilang.migration', |
|
111 | 43 | function () { |
|
112 | 5 | return new MigrationCommand(); |
|
113 | 43 | } |
|
114 | ); |
||
115 | |||
116 | 43 | $this->app->singleton( |
|
117 | 43 | 'command.multilang.texts', |
|
118 | 43 | function () { |
|
119 | 5 | return new TextsCommand(); |
|
120 | 43 | } |
|
121 | ); |
||
122 | |||
123 | 43 | $this->app->singleton( |
|
124 | 43 | 'command.multilang.import', |
|
125 | 43 | function () { |
|
126 | 5 | return new ImportCommand(); |
|
127 | 43 | } |
|
128 | ); |
||
129 | |||
130 | 43 | $this->app->singleton( |
|
131 | 43 | 'command.multilang.export', |
|
132 | 43 | function () { |
|
133 | 5 | return new ExportCommand(); |
|
134 | 43 | } |
|
135 | ); |
||
136 | |||
137 | 43 | $this->commands( |
|
138 | [ |
||
139 | 43 | 'command.multilang.migration', |
|
140 | 'command.multilang.texts', |
||
141 | 'command.multilang.import', |
||
142 | 'command.multilang.export', |
||
143 | ] |
||
144 | ); |
||
145 | |||
146 | 43 | Request::macro('locale', function () { |
|
147 | return app('multilang')->getLocale(); |
||
148 | 43 | }); |
|
149 | 43 | } |
|
150 | |||
151 | /** |
||
152 | * Get the services provided by the provider. |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | 3 | public function provides() |
|
167 | } |
||
168 |