1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sausin\LaravelOvh; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Illuminate\Support\Facades\Storage; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
use League\Flysystem\Filesystem; |
9
|
|
|
use OpenStack\OpenStack; |
10
|
|
|
|
11
|
|
|
class OVHServiceProvider extends ServiceProvider |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Bootstrap the application services. |
15
|
|
|
* |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
|
|
public function boot() |
19
|
|
|
{ |
20
|
|
|
Storage::extend('ovh', function ($app, $config) { |
21
|
|
|
// check if the config is complete |
22
|
|
|
$this->checkConfig($config); |
23
|
|
|
|
24
|
|
|
// create the client |
25
|
|
|
$client = $this->makeClient($config); |
26
|
|
|
|
27
|
|
|
// get the container |
28
|
|
|
$container = $client->objectStoreV1()->getContainer($config['container']); |
29
|
|
|
|
30
|
|
|
return new Filesystem(new OVHSwiftAdapter($container, $this->getVars($config))); |
31
|
|
|
}); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Check that the config is properly setup. |
36
|
|
|
* |
37
|
|
|
* @param array $config |
38
|
|
|
* @return void|BadMethodCallException |
39
|
|
|
*/ |
40
|
|
|
protected function checkConfig($config) |
41
|
|
|
{ |
42
|
|
|
// needed keys |
43
|
|
|
$needKeys = ['server', 'region', 'user', 'pass', 'userDomain', 'projectId', 'container']; |
44
|
|
|
|
45
|
|
|
if (count(array_intersect($needKeys, array_keys($config))) === count($needKeys)) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// if the configuration wasn't complete, throw an exception |
50
|
|
|
throw new BadMethodCallException('Need following keys '.implode(', ', $needKeys)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Make the client needed for interaction with OVH OpenStack. |
55
|
|
|
* |
56
|
|
|
* @param array $config |
57
|
|
|
* @return \OpenStack\OpenStack |
58
|
|
|
*/ |
59
|
|
|
protected function makeClient($config) |
60
|
|
|
{ |
61
|
|
|
// setup the client for OpenStack |
62
|
|
|
return new OpenStack([ |
63
|
|
|
'authUrl' => $config['server'], |
64
|
|
|
'region' => $config['region'], |
65
|
|
|
'user' => [ |
66
|
|
|
'name' => $config['user'], |
67
|
|
|
'password' => $config['pass'], |
68
|
|
|
'domain' => [ |
69
|
|
|
'name' => $config['userDomain'], |
70
|
|
|
], |
71
|
|
|
], |
72
|
|
|
'scope' => [ |
73
|
|
|
'project' => [ |
74
|
|
|
'id' => $config['projectId'], |
75
|
|
|
], |
76
|
|
|
], |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return the config variables required by the adapter. |
82
|
|
|
* |
83
|
|
|
* @param array &$config |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
protected function getVars(&$config) |
87
|
|
|
{ |
88
|
|
|
return [ |
89
|
|
|
'region' => $config['region'], |
90
|
|
|
'projectId' => $config['projectId'], |
91
|
|
|
'container' => $config['container'], |
92
|
|
|
'urlKey' => isset($config['urlKey']) ? $config['urlKey'] : null, |
93
|
|
|
'endpoint' => isset($config['endpoint']) ? $config['endpoint'] : null, |
94
|
|
|
] + $this->getLargeObjectConfig(); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return the config variables required for large object. |
99
|
|
|
* |
100
|
|
|
* @param array &$config |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
protected function getLargeObjectConfig(&$config) |
104
|
|
|
{ |
105
|
|
|
$largeObjConfig = []; |
106
|
|
|
|
107
|
|
|
$largeObjVars = [ |
108
|
|
|
'swiftLargeObjectThreshold', |
109
|
|
|
'swiftSegmentSize', |
110
|
|
|
'swiftSegmentContainer', |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
foreach ($largeObjVars as $key) { |
114
|
|
|
if (isset($config[$key])) { |
115
|
|
|
$largeObjConfig[$key] = $config[$key]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $largeObjConfig; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks for function calls that miss required arguments.