1
|
|
|
<?php |
2
|
|
|
namespace Dkd\PhpCmis\Bindings; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of php-cmis-client. |
6
|
|
|
* |
7
|
|
|
* (c) Sascha Egerer <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
use Dkd\Enumeration\Exception\InvalidEnumerationValueException; |
14
|
|
|
use Dkd\PhpCmis\Converter\JsonConverter; |
15
|
|
|
use Dkd\PhpCmis\Enum\BindingType; |
16
|
|
|
use Dkd\PhpCmis\Exception\CmisInvalidArgumentException; |
17
|
|
|
use Dkd\PhpCmis\Exception\CmisRuntimeException; |
18
|
|
|
use Dkd\PhpCmis\SessionParameter; |
19
|
|
|
use Doctrine\Common\Cache\Cache; |
20
|
|
|
use GuzzleHttp\ClientInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A collection of methods that are used in multiple places within the |
24
|
|
|
* bindings implementation. |
25
|
|
|
*/ |
26
|
|
|
class CmisBindingsHelper |
27
|
|
|
{ |
28
|
|
|
const SPI_OBJECT = 'dkd.phpcmis.binding.spi.object'; |
29
|
|
|
const TYPE_DEFINITION_CACHE = 'dkd.phpcmis.binding.typeDefinitionCache'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $parameters |
33
|
|
|
* @param Cache|null $typeDefinitionCache |
34
|
|
|
* @return CmisBindingInterface |
35
|
|
|
*/ |
36
|
5 |
|
public function createBinding( |
37
|
|
|
array $parameters, |
38
|
|
|
Cache $typeDefinitionCache = null |
39
|
|
|
) { |
40
|
5 |
|
if (count($parameters) === 0) { |
41
|
1 |
|
throw new CmisRuntimeException('Session parameters must be set!'); |
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
if (!isset($parameters[SessionParameter::BINDING_TYPE])) { |
45
|
1 |
|
throw new CmisRuntimeException('Required binding type is not configured!'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
try { |
49
|
3 |
|
$bindingType = BindingType::cast($parameters[SessionParameter::BINDING_TYPE]); |
50
|
|
|
|
51
|
2 |
|
$bindingFactory = $this->getCmisBindingFactory(); |
52
|
|
|
|
53
|
2 |
|
switch (true) { |
54
|
2 |
|
case $bindingType->equals(BindingType::BROWSER): |
55
|
1 |
|
$binding = $bindingFactory->createCmisBrowserBinding( |
56
|
1 |
|
$parameters, |
57
|
|
|
$typeDefinitionCache |
58
|
1 |
|
); |
59
|
1 |
|
break; |
60
|
1 |
|
case $bindingType->equals(BindingType::ATOMPUB): |
61
|
1 |
|
case $bindingType->equals(BindingType::WEBSERVICES): |
62
|
1 |
|
case $bindingType->equals(BindingType::CUSTOM): |
63
|
1 |
|
default: |
64
|
1 |
|
$binding = null; |
65
|
1 |
|
} |
66
|
|
|
|
67
|
2 |
|
if (!is_object($binding) || !($binding instanceof CmisBinding)) { |
68
|
1 |
|
throw new CmisRuntimeException( |
69
|
1 |
|
sprintf( |
70
|
1 |
|
'The given binding "%s" is not yet implemented.', |
71
|
1 |
|
$parameters[SessionParameter::BINDING_TYPE] |
72
|
1 |
|
) |
73
|
1 |
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
} catch (InvalidEnumerationValueException $exception) { |
77
|
1 |
|
throw new CmisRuntimeException( |
78
|
1 |
|
'Invalid binding type given: ' . $parameters[SessionParameter::BINDING_TYPE] |
79
|
1 |
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
return $binding; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Gets the SPI object for the given session. If there is already a SPI |
87
|
|
|
* object in the session it will be returned. If there is no SPI object it |
88
|
|
|
* will be created and put into the session. |
89
|
|
|
* |
90
|
|
|
* @param BindingSessionInterface $session |
91
|
|
|
* @return CmisInterface |
92
|
|
|
*/ |
93
|
6 |
|
public function getSpi(BindingSessionInterface $session) |
94
|
|
|
{ |
95
|
6 |
|
$spi = $session->get(self::SPI_OBJECT); |
96
|
|
|
|
97
|
6 |
|
if ($spi !== null) { |
98
|
1 |
|
return $spi; |
99
|
|
|
} |
100
|
|
|
|
101
|
5 |
|
$spiClass = $session->get(SessionParameter::BINDING_CLASS); |
102
|
5 |
|
if (empty($spiClass) || !class_exists($spiClass)) { |
103
|
2 |
|
throw new CmisRuntimeException( |
104
|
2 |
|
sprintf('The given binding class "%s" is not valid!', $spiClass) |
105
|
2 |
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
if (!is_a($spiClass, CmisInterface::class, true)) { |
109
|
1 |
|
throw new CmisRuntimeException( |
110
|
1 |
|
sprintf('The given binding class "%s" does not implement required CmisInterface!', $spiClass) |
111
|
1 |
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
try { |
115
|
2 |
|
$spi = new $spiClass($session); |
116
|
2 |
|
} catch (\Exception $exception) { |
117
|
1 |
|
throw new CmisRuntimeException( |
118
|
1 |
|
sprintf('Could not create object of type "%s"!', $spiClass), |
119
|
1 |
|
null, |
120
|
|
|
$exception |
121
|
1 |
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
$session->put(self::SPI_OBJECT, $spi); |
125
|
|
|
|
126
|
1 |
|
return $spi; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param BindingSessionInterface $session |
131
|
|
|
* @return mixed |
132
|
|
|
* @throws CmisRuntimeException |
133
|
|
|
*/ |
134
|
6 |
|
public function getHttpInvoker(BindingSessionInterface $session) |
135
|
|
|
{ |
136
|
6 |
|
$invoker = $session->get(SessionParameter::HTTP_INVOKER_OBJECT); |
137
|
|
|
|
138
|
6 |
|
if (is_object($invoker) && is_a($invoker, ClientInterface::class)) { |
139
|
1 |
|
return $invoker; |
140
|
5 |
|
} elseif (is_object($invoker) && !is_a($invoker, ClientInterface::class)) { |
141
|
1 |
|
throw new CmisInvalidArgumentException( |
142
|
1 |
|
sprintf( |
143
|
1 |
|
'Invalid HTTP invoker given. The given instance "%s" does not implement %s!', |
144
|
1 |
|
get_class($invoker), |
145
|
|
|
ClientInterface::class |
146
|
1 |
|
), |
147
|
|
|
1415281262 |
148
|
1 |
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
4 |
|
$invokerClass = $session->get(SessionParameter::HTTP_INVOKER_CLASS); |
152
|
4 |
|
if (!is_a($invokerClass, ClientInterface::class, true)) { |
153
|
2 |
|
throw new CmisRuntimeException( |
154
|
2 |
|
sprintf('The given HTTP Invoker class "%s" is not valid!', $invokerClass) |
155
|
2 |
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
try { |
159
|
2 |
|
$invoker = new $invokerClass; |
160
|
2 |
|
} catch (\Exception $exception) { |
161
|
1 |
|
throw new CmisRuntimeException( |
162
|
1 |
|
sprintf('Could not create object of type "%s"!', $invokerClass), |
163
|
1 |
|
null, |
164
|
|
|
$exception |
165
|
1 |
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
|
$session->put(SessionParameter::HTTP_INVOKER_OBJECT, $invoker); |
169
|
|
|
|
170
|
1 |
|
return $invoker; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param BindingSessionInterface $session |
175
|
|
|
* @return JsonConverter |
176
|
|
|
*/ |
177
|
5 |
|
public function getJsonConverter(BindingSessionInterface $session) |
178
|
|
|
{ |
179
|
5 |
|
$jsonConverter = $session->get(SessionParameter::JSON_CONVERTER); |
180
|
|
|
|
181
|
5 |
|
if ($jsonConverter !== null) { |
182
|
1 |
|
return $jsonConverter; |
183
|
|
|
} |
184
|
|
|
|
185
|
4 |
|
$jsonConverterClass = $session->get(SessionParameter::JSON_CONVERTER_CLASS); |
186
|
4 |
|
if (empty($jsonConverterClass) || !class_exists($jsonConverterClass)) { |
187
|
2 |
|
throw new CmisRuntimeException( |
188
|
2 |
|
sprintf('The given JSON Converter class "%s" is not valid!', $jsonConverterClass) |
189
|
2 |
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
try { |
193
|
2 |
|
$jsonConverter = new $jsonConverterClass(); |
194
|
2 |
|
} catch (\Exception $exception) { |
195
|
1 |
|
throw new CmisRuntimeException( |
196
|
1 |
|
sprintf('Could not create object of type "%s"!', $jsonConverterClass), |
197
|
1 |
|
null, |
198
|
|
|
$exception |
199
|
1 |
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
// we have a json converter object -> put it into the session |
203
|
1 |
|
$session->put(SessionParameter::JSON_CONVERTER, $jsonConverter); |
204
|
|
|
|
205
|
1 |
|
return $jsonConverter; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return CmisBindingFactory |
210
|
|
|
*/ |
211
|
1 |
|
protected function getCmisBindingFactory() |
212
|
|
|
{ |
213
|
1 |
|
return new CmisBindingFactory(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Returns the type definition cache from the session. |
218
|
|
|
* |
219
|
|
|
* @param BindingSessionInterface $session |
220
|
|
|
* @return Cache |
221
|
|
|
* @throws CmisRuntimeException Exception is thrown if cache instance could not be initialized. |
222
|
|
|
*/ |
223
|
1 |
|
public function getTypeDefinitionCache(BindingSessionInterface $session) |
224
|
|
|
{ |
225
|
1 |
|
$cache = $session->get(self::TYPE_DEFINITION_CACHE); |
226
|
1 |
|
if ($cache !== null) { |
227
|
|
|
return $cache; |
228
|
|
|
} |
229
|
|
|
|
230
|
1 |
|
$className = $session->get(SessionParameter::TYPE_DEFINITION_CACHE_CLASS); |
231
|
|
|
try { |
232
|
1 |
|
$cache = new $className(); |
233
|
1 |
|
} catch (\Exception $exception) { |
234
|
|
|
throw new CmisRuntimeException( |
235
|
|
|
sprintf('Could not create object of type "%s"!', $className), |
236
|
|
|
null, |
237
|
|
|
$exception |
238
|
|
|
); |
239
|
|
|
} |
240
|
1 |
|
$session->put(self::TYPE_DEFINITION_CACHE, $cache); |
241
|
|
|
|
242
|
1 |
|
return $cache; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|