1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Signifly\Translator; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\ServiceProvider; |
7
|
|
|
use Signifly\Translator\Models\Translation; |
8
|
|
|
use Signifly\Translator\Exceptions\InvalidConfiguration; |
9
|
|
|
use Signifly\Translator\Contracts\Translator as TranslatorContract; |
10
|
|
|
use Signifly\Translator\Contracts\Translation as TranslationContract; |
11
|
|
|
|
12
|
|
|
class TranslatorServiceProvider extends ServiceProvider |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Bootstrap the application services. |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public function boot() |
20
|
|
|
{ |
21
|
|
|
if ($this->app->runningInConsole()) { |
22
|
|
|
$this->publishConfigs(); |
23
|
|
|
$this->publishMigrations(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/translator.php', 'translator'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Register the service provider. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function register() |
35
|
|
|
{ |
36
|
|
|
$this->app->singleton(TranslatorContract::class, function ($app) { |
37
|
|
|
return new Translator($app['config']); |
38
|
|
|
}); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the services provided by the provider. |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function provides() |
47
|
|
|
{ |
48
|
|
|
return [TranslatorContract::class]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function publishConfigs(): void |
52
|
|
|
{ |
53
|
|
|
$this->publishes([ |
54
|
|
|
__DIR__.'/../config/translator.php' => config_path('translator.php'), |
55
|
|
|
], 'translator-config'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function publishMigrations(): void |
59
|
|
|
{ |
60
|
|
|
if (class_exists('CreateTranslationsTable')) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$timestamp = date('Y_m_d_His', time()); |
65
|
|
|
|
66
|
|
|
$this->publishes([ |
67
|
|
|
__DIR__.'/../migrations/create_translations_table.php.stub' => database_path("/migrations/{$timestamp}_create_translations_table.php"), |
68
|
|
|
], 'translator-migrations'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function determineTranslationModel(): string |
72
|
|
|
{ |
73
|
|
|
$model = config('translator.translation_model') ?? Translation::class; |
74
|
|
|
|
75
|
|
|
if (! is_a($model, TranslationContract::class, true) |
76
|
|
|
|| ! is_a($model, Model::class, true)) { |
77
|
|
|
throw InvalidConfiguration::modelIsNotValid($model); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $model; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function getTranslationModelInstance(): TranslationContract |
84
|
|
|
{ |
85
|
|
|
$model = self::determineTranslationModel(); |
86
|
|
|
|
87
|
|
|
return new $model(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|