1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZObjectWrapperBundle\Core; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Repository as eZRepository; |
6
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location; |
7
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
8
|
|
|
use eZ\Publish\API\Repository\Values\Content\ContentInfo; |
9
|
|
|
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Typehint methods we expose from Logger and Repository using magic calls. NB: to be adjusted manually if those change... |
14
|
|
|
* |
15
|
|
|
* @method null emergency($message, array $context = array()) |
16
|
|
|
* @method null alert($message, array $context = array()) |
17
|
|
|
* @method null critical($message, array $context = array()) |
18
|
|
|
* @method null error($message, array $context = array()) |
19
|
|
|
* @method null warning($message, array $context = array()) |
20
|
|
|
* @method null notice($message, array $context = array()) |
21
|
|
|
* @method null info($message, array $context = array()) |
22
|
|
|
* @method null debug($message, array $context = array()) |
23
|
|
|
* @method null log($message, array $context = array()) |
24
|
|
|
* |
25
|
|
|
* @method \eZ\Publish\API\Repository\ContentService getContentService() |
26
|
|
|
* @method \eZ\Publish\API\Repository\LanguageService getContentLanguageService() |
27
|
|
|
* @method \eZ\Publish\API\Repository\ContentTypeService getContentTypeService() |
28
|
|
|
* @method \eZ\Publish\API\Repository\LocationService getLocationService() |
29
|
|
|
* @method \eZ\Publish\API\Repository\TrashService getTrashService() |
30
|
|
|
* @method \eZ\Publish\API\Repository\SectionService getSectionService() |
31
|
|
|
* @method \eZ\Publish\API\Repository\SearchService getSearchService() |
32
|
|
|
* @method \eZ\Publish\API\Repository\UserService getUserService() |
33
|
|
|
* @method \eZ\Publish\API\Repository\URLAliasService getURLAliasService() |
34
|
|
|
* @method \eZ\Publish\API\Repository\URLWildcardService getURLWildcardService() |
35
|
|
|
* @method \eZ\Publish\API\Repository\ObjectStateService getObjectStateService() |
36
|
|
|
* @method \eZ\Publish\API\Repository\RoleService getRoleService() |
37
|
|
|
* @method \eZ\Publish\API\Repository\FieldTypeService getFieldTypeService() |
38
|
|
|
* |
39
|
|
|
* @method \eZ\Publish\API\Repository\Values\User\User getCurrentUser() |
40
|
|
|
* @method null setCurrentUser(\eZ\Publish\API\Repository\Values\User\User $user) |
41
|
|
|
* @method array|null hasAccess($module, $function, \eZ\Publish\API\Repository\Values\User\User $user = null) |
42
|
|
|
* @method bool canUser($module, $function, \eZ\Publish\API\Repository\Values\ValueObject $object, $targets = null) |
43
|
|
|
* |
44
|
|
|
* @method null beginTransaction() |
45
|
|
|
* @method null commit() |
46
|
|
|
* @method null rollback() |
47
|
|
|
* |
48
|
|
|
* @todo we could add simple methods like findAll() and fetch($offset, $limit) which simply filter based on contentType |
49
|
|
|
*/ |
50
|
|
|
class Repository implements RepositoryInterface |
51
|
|
|
{ |
52
|
|
|
// Name of the php class used to create entities. Subclasses have to set a value to this, to be able to create entities |
53
|
|
|
protected $entityClass; |
54
|
|
|
|
55
|
|
|
protected $repository; |
56
|
|
|
protected $entityManager; |
57
|
|
|
protected $contentTypeIdentifier; |
58
|
|
|
protected $settings; |
59
|
|
|
protected $logger; |
60
|
|
|
|
61
|
10 |
|
/** |
62
|
|
|
* Prioritized languages |
63
|
10 |
|
* |
64
|
10 |
|
* @var array |
65
|
10 |
|
*/ |
66
|
10 |
|
protected $languages; |
67
|
10 |
|
|
68
|
|
|
public function __construct(eZRepository $repository, $entityManager, array $settings=array(), $contentTypeIdentifier='') |
69
|
10 |
|
{ |
70
|
|
|
$this->repository = $repository; |
71
|
10 |
|
$this->entityManager = $entityManager; |
72
|
10 |
|
$this->settings = $this->validateSettings($settings); |
73
|
|
|
$this->contentTypeIdentifier = $contentTypeIdentifier; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function setContentTypeIdentifier($contentTypeIdentifier) |
77
|
|
|
{ |
78
|
|
|
$this->contentTypeIdentifier = $contentTypeIdentifier; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
10 |
|
|
82
|
|
|
public function setSettings(array $settings) |
83
|
10 |
|
{ |
84
|
10 |
|
$this->settings = $this->validateSettings($settings); |
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function setLogger(LoggerInterface $logger=null) |
89
|
|
|
{ |
90
|
|
|
$this->logger = $logger; |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function setLanguages(array $languages = null) |
95
|
10 |
|
{ |
96
|
|
|
$this->languages = $languages; |
|
|
|
|
97
|
10 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Called from the constructor, with the settings received from the caller. |
101
|
|
|
* Subclasses can implement checking here, or merge the received settings with other data, using f.e. the Symfony |
102
|
|
|
* OptionsResolver component (see http://symfony.com/doc/current/components/options_resolver.html). |
103
|
|
|
* |
104
|
|
|
* @param array $settings |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
protected function validateSettings(array $settings) |
108
|
|
|
{ |
109
|
|
|
return $settings; |
110
|
10 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
10 |
|
* Nice syntactic sugar ( manually typehinted :-) ) |
114
|
10 |
|
* Allow all logger methods and eZ repo methods to be called on this extended repo |
115
|
10 |
|
* |
116
|
10 |
|
* @param string $method |
117
|
10 |
|
* @param array $args |
118
|
10 |
|
* @return mixed |
119
|
10 |
|
* |
120
|
10 |
|
* @todo !important move this method to protected access? |
121
|
10 |
|
*/ |
122
|
|
|
public function __call($method, $args) |
123
|
|
|
{ |
124
|
|
|
switch($method) { |
125
|
|
|
case 'emergency': |
126
|
|
|
case 'alert': |
127
|
10 |
|
case 'critical': |
128
|
10 |
|
case 'error': |
129
|
10 |
|
case 'warning': |
130
|
|
|
case 'notice': |
131
|
|
|
case 'info': |
132
|
|
|
case 'debug': |
133
|
|
|
case 'log': |
134
|
|
|
if ($this->logger) { |
135
|
|
|
return call_user_func_array(array($this->logger, $method), $args); |
136
|
|
|
} |
137
|
|
|
// if no logger is defined, swallow the method call |
138
|
|
|
return; |
139
|
10 |
|
default: |
140
|
|
|
return call_user_func_array(array($this->repository, $method), $args); |
141
|
10 |
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
10 |
|
/** |
145
|
10 |
|
* To be overridden in subclasses, this method allows injecting extra services/settings in the entities created. |
146
|
10 |
|
* This version 'knows' about EntityManagerAware and Logging entity traits. |
147
|
10 |
|
* |
148
|
|
|
* @param \Kaliop\eZObjectWrapperBundle\Core\EntityInterface $entity |
149
|
|
|
* @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface |
150
|
|
|
*/ |
151
|
|
|
protected function enrichEntityAtLoad($entity) |
152
|
|
|
{ |
153
|
|
|
if (is_callable(array($entity, 'setLogger'))) { |
154
|
|
|
$entity->setLogger($this->logger); |
155
|
|
|
} |
156
|
10 |
|
if (is_callable(array($entity, 'setEntityManager'))) { |
157
|
|
|
$entity->setEntityManager($this->entityManager); |
158
|
10 |
|
} |
159
|
10 |
|
if (is_callable(array($entity, 'setLanguages'))) { |
160
|
10 |
|
$entity->setLanguages($this->languages); |
161
|
|
|
} |
162
|
|
|
return $entity; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param Content $content |
167
|
4 |
|
* @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface |
168
|
|
|
* |
169
|
4 |
|
* @todo optionally (?) throw an error if bad content type is detected |
170
|
4 |
|
*/ |
171
|
4 |
|
public function loadEntityFromContent(Content $content) |
172
|
|
|
{ |
173
|
|
|
$class = $this->entityClass; |
174
|
|
|
$entity = new $class($this->repository, $content, null); |
175
|
|
|
return $this->enrichEntityAtLoad($entity); |
176
|
|
|
} |
177
|
|
|
|
178
|
2 |
|
/** |
179
|
|
|
* @param Location $location |
180
|
2 |
|
* @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface |
181
|
|
|
*/ |
182
|
|
|
public function loadEntityFromLocation(Location $location) |
183
|
|
|
{ |
184
|
|
|
$class = $this->entityClass; |
185
|
|
|
$entity = new $class($this->repository, null, $location); |
186
|
|
|
return $this->enrichEntityAtLoad($entity); |
187
|
|
|
} |
188
|
|
|
|
189
|
10 |
|
/** |
190
|
|
|
* @param ContentInfo $contentInfo |
191
|
10 |
|
* @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface |
192
|
|
|
*/ |
193
|
|
|
public function loadEntityFromContentInfo(ContentInfo $contentInfo) |
194
|
|
|
{ |
195
|
|
|
return $this->loadEntityFromContent($this->getContentService()->loadContentByContentInfo($contentInfo)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param int $id |
200
|
|
|
* @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface |
201
|
10 |
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content with the given id does not exist |
202
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the user has no access to read content and in case of un-published content: read versions |
203
|
10 |
|
*/ |
204
|
|
|
public function loadEntityFromContentId($id) |
205
|
|
|
{ |
206
|
|
|
return $this->loadEntityFromContent($this->getContentService()->loadContent($id)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* An alias for loadEntityFromContentId, to keep the API close to Doctrine |
211
|
|
|
* @param int $id |
212
|
1 |
|
* @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface |
213
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content with the given id does not exist |
214
|
1 |
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the user has no access to read content and in case of un-published content: read versions |
215
|
|
|
*/ |
216
|
|
|
public function find($id) |
217
|
|
|
{ |
218
|
|
|
return $this->loadEntityFromContentId($id); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param int $id |
223
|
1 |
|
* @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface |
224
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location |
225
|
1 |
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found |
226
|
|
|
*/ |
227
|
|
|
public function loadEntityFromLocationId($id) |
228
|
|
|
{ |
229
|
|
|
return $this->loadEntityFromLocation($this->getLocationService()->loadLocation($id)); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $remoteId |
234
|
1 |
|
* @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface |
235
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content with the given id does not exist |
236
|
1 |
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the user has no access to read content and in case of un-published content: read versions |
237
|
|
|
*/ |
238
|
|
|
public function loadEntityFromContentRemoteId($remoteId) |
239
|
|
|
{ |
240
|
|
|
return $this->loadEntityFromContent($this->getContentService()->loadContentByRemoteId($remoteId)); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param string $remoteId |
245
|
|
|
* @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface |
246
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location |
247
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found |
248
|
|
|
*/ |
249
|
|
|
public function loadEntityFromLocationRemoteId($remoteId) |
250
|
|
|
{ |
251
|
|
|
return $this->loadEntityFromLocation($this->getLocationService()->loadLocationByRemoteId($remoteId)); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* NB: assumes that all search results are homogeneous (same content type) |
256
|
|
|
* @param SearchResult $searchResult |
257
|
|
|
* @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface[] |
258
|
|
|
*/ |
259
|
|
|
protected function loadEntitiesFromSearchResults(SearchResult $searchResult) |
260
|
|
|
{ |
261
|
|
|
$entities = array(); |
262
|
|
|
foreach ($searchResult->searchHits as $searchHit) { |
263
|
|
|
// let's hope that in the future eZPublish does not add new types of results to SearchResult... :-P |
264
|
|
|
if ($searchHit->valueObject instanceof \eZ\Publish\API\Repository\Values\Content\Location) { |
265
|
|
|
$entities[] = $this->loadEntityFromLocation($searchHit->valueObject); |
266
|
|
|
} else { |
267
|
|
|
$entities[] = $this->loadEntityFromContent($searchHit->valueObject); |
|
|
|
|
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
return $entities; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.