1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sausin\LaravelOvh; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Facades\Storage; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
use League\Flysystem\Cached\CachedAdapter; |
9
|
|
|
use League\Flysystem\Cached\Storage\Memory as MemoryStore; |
10
|
|
|
use League\Flysystem\Filesystem; |
11
|
|
|
use OpenStack\ObjectStore\v1\Models\Container; |
12
|
|
|
use OpenStack\OpenStack; |
13
|
|
|
|
14
|
|
|
class OVHServiceProvider extends ServiceProvider |
15
|
|
|
{ |
16
|
|
|
/** @var OVHConfiguration */ |
17
|
|
|
private OVHConfiguration $config; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Bootstrap the application services. |
21
|
|
|
*/ |
22
|
|
|
public function boot(): void |
23
|
|
|
{ |
24
|
|
|
$this->configureCommands(); |
25
|
|
|
|
26
|
|
|
$this->configureStorage(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Configures available commands. |
31
|
|
|
*/ |
32
|
|
|
protected function configureCommands(): void |
33
|
|
|
{ |
34
|
|
|
if (!$this->app->runningInConsole()) { |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->commands([ |
39
|
|
|
Commands\SetTempUrlKey::class, |
40
|
|
|
Commands\SetCORSHeaders::class, |
41
|
|
|
]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Configures extended filesystem storage for interaction with OVH Object Storage. |
46
|
|
|
*/ |
47
|
|
|
protected function configureStorage(): void |
48
|
|
|
{ |
49
|
|
|
Storage::extend('ovh', function ($app, array $config) { |
50
|
|
|
$cache = Arr::pull($config, 'cache', false); |
51
|
|
|
|
52
|
|
|
// Creates a Configuration instance. |
53
|
|
|
$this->config = OVHConfiguration::make($config); |
54
|
|
|
|
55
|
|
|
$client = $this->makeOpenStackClient(); |
56
|
|
|
|
57
|
|
|
// Get the Object Storage container. |
58
|
|
|
$container = $client->objectStoreV1()->getContainer($this->config->getContainerName()); |
59
|
|
|
|
60
|
|
|
return $this->makeFileSystem($container, $cache); |
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Creates an OpenStack client instance, needed for interaction with OVH OpenStack. |
66
|
|
|
* |
67
|
|
|
* @return OpenStack |
68
|
|
|
*/ |
69
|
|
|
protected function makeOpenStackClient(): OpenStack |
70
|
|
|
{ |
71
|
|
|
return new OpenStack([ |
72
|
|
|
'authUrl' => $this->config->getAuthUrl(), |
73
|
|
|
'region' => $this->config->getRegion(), |
74
|
|
|
'user' => [ |
75
|
|
|
'name' => $this->config->getUsername(), |
76
|
|
|
'password' => $this->config->getPassword(), |
77
|
|
|
'domain' => [ |
78
|
|
|
'name' => $this->config->getUserDomain(), |
79
|
|
|
], |
80
|
|
|
], |
81
|
|
|
'scope' => [ |
82
|
|
|
'project' => [ |
83
|
|
|
'id' => $this->config->getProjectId(), |
84
|
|
|
], |
85
|
|
|
], |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Creates a Filesystem instance for interaction with the Object Storage. |
91
|
|
|
* |
92
|
|
|
* @param Container $container |
93
|
|
|
* @param bool |
94
|
|
|
* @return Filesystem |
95
|
|
|
*/ |
96
|
|
|
protected function makeFileSystem(Container $container, bool $cache): Filesystem |
97
|
|
|
{ |
98
|
|
|
$adapter = new OVHSwiftAdapter($container, $this->config, $this->config->getPrefix()); |
99
|
|
|
|
100
|
|
|
if ($cache) { |
101
|
|
|
$adapter = new CachedAdapter($adapter, new MemoryStore); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return new Filesystem( |
105
|
|
|
$adapter, |
106
|
|
|
array_filter([ |
107
|
|
|
'swiftLargeObjectThreshold' => $this->config->getSwiftLargeObjectThreshold(), |
108
|
|
|
'swiftSegmentSize' => $this->config->getSwiftSegmentSize(), |
109
|
|
|
'swiftSegmentContainer' => $this->config->getSwiftSegmentContainer(), |
110
|
|
|
]) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|