1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WeAreNeopix\LaravelModelTranslation; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Facades\Storage; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
use Illuminate\Database\Eloquent\Collection; |
9
|
|
|
use Illuminate\Filesystem\FilesystemAdapter as StorageDisk; |
10
|
|
|
use WeAreNeopix\LaravelModelTranslation\Commands\MakeDriverCommand; |
11
|
|
|
use WeAreNeopix\LaravelModelTranslation\Drivers\JSONTranslationDriver; |
12
|
|
|
use WeAreNeopix\LaravelModelTranslation\Commands\TestTranslationExtensions; |
13
|
|
|
|
14
|
|
|
class ModelTranslationServiceProvider extends ServiceProvider |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Register services. |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function register() |
22
|
|
|
{ |
23
|
|
|
if ($this->app->runningInConsole()) { |
24
|
|
|
$this->commands(TestTranslationExtensions::class, MakeDriverCommand::class); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$this->app->singleton('translation', function ($app) { |
28
|
|
|
return new TranslationManager($app); |
29
|
|
|
}); |
30
|
|
|
|
31
|
|
|
$this->app->when(JSONTranslationDriver::class) |
32
|
|
|
->needs(StorageDisk::class) |
33
|
|
|
->give(function () { |
34
|
|
|
return Storage::disk('translation'); |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
$this->publishConfig(); |
38
|
|
|
$this->publishMigrations(); |
39
|
|
|
|
40
|
|
|
$this->createTranslationsDisk(); |
41
|
|
|
|
42
|
|
|
$this->registerCollectionMacro(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Register the config file publishing. |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
private function publishConfig() |
51
|
|
|
{ |
52
|
|
|
$this->publishes([ |
53
|
|
|
__DIR__.'/../config/translation.php' => config_path('translation.php'), |
54
|
|
|
], 'config'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Register the migration file publishing. |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
private function publishMigrations() |
63
|
|
|
{ |
64
|
|
|
$publishedMigrationName = date('Y_m_d_His').'_create_translations_table.php'; |
65
|
|
|
$publishPath = database_path('migrations'.DIRECTORY_SEPARATOR.$publishedMigrationName); |
66
|
|
|
$this->publishes([ |
67
|
|
|
__DIR__.'/../migrations/2019_02_25_163419_create_translations_table.php' => $publishPath, |
68
|
|
|
], 'migrations'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create the custom Filesystem disk for storing JSON-based translations. |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
private function createTranslationsDisk() |
77
|
|
|
{ |
78
|
|
|
$basePath = config('translation.json.base_path', 'app/translations'); |
79
|
|
|
|
80
|
|
|
config()->set('filesystems.disks.translation', [ |
81
|
|
|
'driver' => 'local', |
82
|
|
|
'root' => $basePath, |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Register the Eloquent Collection macro used for setting translations on an array of Models. |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
private function registerCollectionMacro() |
92
|
|
|
{ |
93
|
|
|
Collection::macro('setLanguage', function (string $language, bool $loadLanguage = true) { |
94
|
|
|
if ($this->count() < 1) { |
|
|
|
|
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->each(function (Model $model) use ($language) { |
|
|
|
|
99
|
|
|
// Set the language on the model but do not load it. |
100
|
|
|
// We will load the language from the collection method if the user has requested it. |
101
|
|
|
$model->setLanguage($language, false); |
102
|
|
|
}); |
103
|
|
|
|
104
|
|
|
// If requested, we will load the translations for the specified language for all the present Models. |
105
|
|
|
// We will do this all in one request to avoid N calls. |
106
|
|
|
if ($loadLanguage) { |
107
|
|
|
$translations = Translation::getTranslationsForModels($this, $language); |
108
|
|
|
$this->each(function (Model $model) use ($translations, $language) { |
|
|
|
|
109
|
|
|
$model->setTranslations( |
110
|
|
|
$translations->get($model->getInstanceIdentifier(), []) |
111
|
|
|
); |
112
|
|
|
}); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
}); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.