1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nikaia\TranslationSheet; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
use Nikaia\TranslationSheet\Client\Client; |
7
|
|
|
use Nikaia\TranslationSheet\Commands\Lock; |
8
|
|
|
use Nikaia\TranslationSheet\Commands\Prepare; |
9
|
|
|
use Nikaia\TranslationSheet\Commands\Pull; |
10
|
|
|
use Nikaia\TranslationSheet\Commands\Push; |
11
|
|
|
use Nikaia\TranslationSheet\Commands\Setup; |
|
|
|
|
12
|
|
|
use Nikaia\TranslationSheet\Commands\Status; |
13
|
|
|
use Nikaia\TranslationSheet\Commands\Unlock; |
14
|
|
|
|
15
|
|
|
class TranslationSheetServiceProvider extends ServiceProvider |
16
|
|
|
{ |
17
|
34 |
|
public function boot() |
18
|
|
|
{ |
19
|
34 |
|
$this->publishes([ |
20
|
34 |
|
__DIR__.'/../config/config.php' => config_path('translation_sheet.php'), |
21
|
34 |
|
], 'config'); |
22
|
34 |
|
} |
23
|
|
|
|
24
|
34 |
|
public function register() |
25
|
|
|
{ |
26
|
34 |
|
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'translation_sheet'); |
27
|
|
|
|
28
|
34 |
|
$this->registerGoogleApiClient(); |
29
|
|
|
|
30
|
34 |
|
$this->registerSpreadsheet(); |
31
|
|
|
|
32
|
34 |
|
$this->registerCommands(); |
33
|
34 |
|
} |
34
|
|
|
|
35
|
34 |
|
private function registerGoogleApiClient() |
36
|
|
|
{ |
37
|
|
|
$this->app->singleton(Client::class, function () { |
38
|
17 |
|
$client = new Client; |
39
|
17 |
|
$client->setAuthConfigFile($this->app['config']['translation_sheet.serviceAccountCredentialsFile']); |
40
|
17 |
|
$client->setApplicationName($this->app['config']['translation_sheet.googleApplicationName']); |
41
|
17 |
|
$client->setScopes(\Google_Service_Sheets::SPREADSHEETS); |
42
|
|
|
|
43
|
17 |
|
return $client; |
44
|
34 |
|
}); |
45
|
34 |
|
} |
46
|
|
|
|
47
|
34 |
|
private function registerSpreadsheet() |
48
|
|
|
{ |
49
|
|
|
$this->app->singleton(Spreadsheet::class, function () { |
50
|
1 |
|
return new Spreadsheet( |
51
|
1 |
|
$this->app['config']['translation_sheet.spreadsheetId'], |
52
|
1 |
|
$this->asArray($this->app['config']['translation_sheet.locales']) |
53
|
|
|
); |
54
|
34 |
|
}); |
55
|
34 |
|
} |
56
|
|
|
|
57
|
34 |
|
private function registerCommands() |
58
|
|
|
{ |
59
|
34 |
|
$this->commands([ |
60
|
34 |
|
Setup::class, |
61
|
|
|
Push::class, |
62
|
|
|
Pull::class, |
63
|
|
|
Prepare::class, |
64
|
|
|
Lock::class, |
65
|
|
|
Unlock::class, |
66
|
|
|
Status::class, |
67
|
|
|
]); |
68
|
34 |
|
} |
69
|
|
|
|
70
|
1 |
|
private function asArray($value) |
71
|
|
|
{ |
72
|
1 |
|
if (is_array($value)) { |
73
|
|
|
return $value; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return array_filter(array_map(function ($item) { |
77
|
1 |
|
return trim($item); |
78
|
1 |
|
}, explode(',', $value))); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: