1 | <?php |
||
16 | class EntityManager |
||
17 | { |
||
18 | /** |
||
19 | * @var \eZ\Publish\API\Repository\Repository $repository |
||
20 | */ |
||
21 | protected $repository; |
||
22 | protected $classMap; |
||
23 | protected $serviceMap; |
||
24 | protected $defaultClass; |
||
25 | protected $contentTypeIdentifierCache = array(); |
||
26 | |||
27 | /** |
||
28 | * @param eZRepository $repository |
||
29 | * @param array $classMap array of classes exposing a RepositoryInterface |
||
30 | * @param RepositoryManagerInterface[] $serviceMap array of services exposing a RepositoryInterface |
||
31 | */ |
||
32 | 10 | public function __construct(eZRepository $repository, array $classMap=array(), array $serviceMap=array()) |
|
42 | |||
43 | /** |
||
44 | * Registers an existing service to be used as repository for a given content type |
||
45 | * @var RepositoryInterface $service |
||
46 | * @var string $contentTypeIdentifier |
||
47 | */ |
||
48 | 10 | public function registerService(RepositoryInterface $service, $contentTypeIdentifier) |
|
49 | { |
||
50 | 10 | $this->serviceMap[$contentTypeIdentifier] = $service; |
|
51 | 10 | } |
|
52 | |||
53 | /** |
||
54 | * Registers a php class to be used as wrapper for a given content type |
||
55 | * @var string $className |
||
56 | * @var string $contentTypeIdentifier |
||
57 | * @throws \InvalidArgumentException |
||
58 | * |
||
59 | * @todo validate contentTypeIdentifier as well. Reject null identifier and integers (unless 0 isa valid content type identifier...) |
||
60 | */ |
||
61 | 2 | public function registerClass($className, $contentTypeIdentifier) |
|
68 | |||
69 | /** |
||
70 | * Registers a php class to be used as default repository |
||
71 | * @var string $className |
||
72 | * @throws \InvalidArgumentException |
||
73 | */ |
||
74 | 10 | public function registerDefaultClass($className) |
|
81 | |||
82 | /** |
||
83 | * @param string $contentTypeIdentifier as used in the mapping |
||
84 | * @return \Kaliop\eZObjectWrapperBundle\Core\RepositoryInterface |
||
85 | * @throws \UnexpectedValueException |
||
86 | */ |
||
87 | 10 | public function getRepository($contentTypeIdentifier) |
|
88 | { |
||
89 | 10 | if (isset($this->serviceMap[$contentTypeIdentifier])) { |
|
90 | 3 | $repo = $this->serviceMap[$contentTypeIdentifier]; |
|
91 | 3 | return $repo->setContentTypeIdentifier($contentTypeIdentifier); |
|
92 | } |
||
93 | |||
94 | /// @todo for a small perf gain, we might store the created repo classes in an array |
||
95 | 10 | if (isset($this->classMap[$contentTypeIdentifier])) { |
|
96 | 2 | $repoClass = $this->classMap[$contentTypeIdentifier]; |
|
97 | 2 | $repo = new $repoClass($this->repository, $this); |
|
98 | 2 | return $repo->setContentTypeIdentifier($contentTypeIdentifier); |
|
99 | } |
||
100 | |||
101 | 10 | if ($this->defaultClass != '') { |
|
102 | 10 | $repoClass = $this->defaultClass; |
|
103 | 10 | $repo = new $repoClass($this->repository, $this); |
|
104 | 10 | return $repo->setContentTypeIdentifier($contentTypeIdentifier); |
|
105 | } |
||
106 | |||
107 | throw new \UnexpectedValueException("Content type '$contentTypeIdentifier' is not registered with the Entity Manager, can not retrieve a Repository for it"); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param int $contentTypeId slightly slower than loading by Identifier, but is useful when used eg. by Entities |
||
112 | * @return \Kaliop\eZObjectWrapperBundle\Core\RepositoryInterface |
||
113 | * @throws \UnexpectedValueException |
||
114 | */ |
||
115 | 7 | public function getRepositoryByContentTypeId($contentTypeId) |
|
119 | |||
120 | /** |
||
121 | * A method added to keep the API friendly to Doctrine users |
||
122 | * @param string $contentTypeIdentifier |
||
123 | * @param mixed $id Content Id |
||
124 | * @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface |
||
125 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content with the given id does not exist |
||
126 | * @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 |
||
127 | */ |
||
128 | 10 | public function find($contentTypeIdentifier, $id) |
|
132 | |||
133 | /** |
||
134 | * NB: this is slightly slower in execution than using find(), as it does have to look up the content type identifier. |
||
135 | * |
||
136 | * @param Location|Content|ContentInfo $content If you have a Content, by all means pass it in, not just its contentInfo |
||
137 | * @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface |
||
138 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content with the given id does not exist |
||
139 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If a content type with the given id and status DEFINED can not be found |
||
140 | * @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 |
||
141 | */ |
||
142 | 4 | public function load($content) |
|
161 | |||
162 | /** |
||
163 | * @param Content[]|Location[]|Contentinfo[]|SearchResult $contents |
||
164 | * @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface[] they keys of the $contents array get preserved |
||
165 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content with the given id does not exist |
||
166 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If a content type with the given id and status DEFINED can not be found |
||
167 | * @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 |
||
168 | */ |
||
169 | 2 | public function loadMany($contents) |
|
188 | |||
189 | /** |
||
190 | * @param mixed $id |
||
191 | * @return string |
||
192 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If a content type with the given id and status DEFINED can not be found |
||
193 | */ |
||
194 | 7 | protected function getContentTypeIdentifierFromId($id) |
|
202 | } |
||
203 |