1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Filesystem; |
4
|
|
|
|
5
|
|
|
use Nip\Container\ServiceProviders\Providers\AbstractServiceProvider; |
6
|
|
|
use Nip\Container\ServiceProviders\Providers\BootableServiceProviderInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class FilesystemServiceProvider |
10
|
|
|
* @package Nip\Filesystem |
11
|
|
|
* |
12
|
|
|
* @inspiration https://github.com/laravel/framework/blob/5.4/src/Illuminate/Filesystem/FilesystemServiceProvider.php |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
class FilesystemServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @inheritdoc |
20
|
|
|
*/ |
21
|
|
|
public function provides() |
22
|
|
|
{ |
23
|
|
|
return ['files', 'filesystem', 'filesystem.disk']; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
1 |
|
public function register() |
30
|
|
|
{ |
31
|
1 |
|
$this->registerNativeFilesystem(); |
32
|
1 |
|
$this->registerFlysystem(); |
33
|
1 |
|
} |
34
|
|
|
|
35
|
|
|
public function boot() |
36
|
|
|
{ |
37
|
|
|
$this->mergeDefaultFilesystem(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Register the native filesystem implementation. |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
1 |
|
protected function registerNativeFilesystem() |
46
|
|
|
{ |
47
|
|
|
$this->getContainer()->share('files', function () { |
48
|
1 |
|
return new Filesystem; |
49
|
1 |
|
}); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Register the driver based filesystem. |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
1 |
|
protected function registerFlysystem() |
58
|
|
|
{ |
59
|
1 |
|
$this->registerManager(); |
60
|
|
|
|
61
|
1 |
|
$defaultDriver = $this->getDefaultDriver(); |
62
|
1 |
|
if ($defaultDriver) { |
63
|
|
|
$this->getContainer()->share('filesystem.disk', function () { |
64
|
|
|
return app('filesystem')->disk($this->getDefaultDriver()); |
65
|
1 |
|
}); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
$cloudDriver = $this->getDefaultDriver(); |
69
|
1 |
|
if ($cloudDriver) { |
70
|
|
|
$this->getContainer()->share('filesystem.cloud', function () { |
71
|
|
|
return app('filesystem')->disk($this->getCloudDriver()); |
72
|
1 |
|
}); |
73
|
|
|
} |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Register the filesystem manager. |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
1 |
|
protected function registerManager() |
82
|
|
|
{ |
83
|
|
|
$this->getContainer()->share('filesystem', function () { |
84
|
1 |
|
$app = $this->getContainer()->has('app') ? $this->getContainer()->get('app') : null; |
85
|
|
|
|
86
|
1 |
|
return new FilesystemManager($app); |
87
|
1 |
|
}); |
88
|
1 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the default file driver. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
1 |
|
protected function getDefaultDriver() |
96
|
|
|
{ |
97
|
1 |
|
return function_exists('config') && function_exists('app') ? config('filesystems.default') : null; |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the default cloud based file driver. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function getCloudDriver() |
106
|
|
|
{ |
107
|
|
|
return function_exists('config') ? config('filesystems.cloud') : null; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function mergeDefaultFilesystem() |
111
|
|
|
{ |
112
|
|
|
$config = app('config'); |
113
|
|
|
|
114
|
|
|
if ($config->has('filesystems')) { |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$urlUploads = defined('UPLOADS_URL') ? UPLOADS_URL : ''; |
|
|
|
|
119
|
|
|
|
120
|
|
|
$config = new Config([ |
|
|
|
|
121
|
|
|
'filesystems' => [ |
122
|
|
|
'disks' => [ |
123
|
|
|
'local' => [ |
124
|
|
|
'driver' => 'local', |
125
|
|
|
'root' => UPLOADS_PATH, |
|
|
|
|
126
|
|
|
], |
127
|
|
|
'public' => [ |
128
|
|
|
'driver' => 'local', |
129
|
|
|
'root' => UPLOADS_PATH, |
130
|
|
|
'url' => $urlUploads, |
131
|
|
|
'visibility' => 'public', |
132
|
|
|
] |
133
|
|
|
] |
134
|
|
|
] |
135
|
|
|
]); |
136
|
|
|
app('config')->merge($config); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|