1 | <?php |
||
23 | class RepositoryFactory implements ContainerAwareInterface |
||
24 | { |
||
25 | use ContainerAwareTrait; |
||
26 | |||
27 | /** @var string */ |
||
28 | private $repositoryClass; |
||
29 | |||
30 | /** |
||
31 | * Collection of limitation types for the RoleService. |
||
32 | * |
||
33 | * @var \eZ\Publish\SPI\Limitation\Type[] |
||
34 | */ |
||
35 | protected $roleLimitations = []; |
||
36 | |||
37 | /** |
||
38 | * Policies map. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $policyMap = []; |
||
43 | |||
44 | public function __construct( |
||
45 | $repositoryClass, |
||
46 | array $policyMap |
||
47 | ) { |
||
48 | $this->repositoryClass = $repositoryClass; |
||
49 | $this->policyMap = $policyMap; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Builds the main repository, heart of eZ Publish API. |
||
54 | * |
||
55 | * This always returns the true inner Repository, please depend on ezpublish.api.repository and not this method |
||
56 | * directly to make sure you get an instance wrapped inside Event / Cache / * functionality. |
||
57 | * |
||
58 | * @param \eZ\Publish\SPI\Persistence\Handler $persistenceHandler |
||
59 | * @param \eZ\Publish\SPI\Search\Handler $searchHandler |
||
60 | * @param \eZ\Publish\Core\Search\Common\BackgroundIndexer $backgroundIndexer |
||
61 | * @param \eZ\Publish\Core\Repository\Helper\RelationProcessor $relationProcessor |
||
62 | * @param \eZ\Publish\Core\FieldType\FieldTypeRegistry $fieldTypeRegistry |
||
63 | * |
||
64 | * @return \eZ\Publish\API\Repository\Repository |
||
65 | */ |
||
66 | public function buildRepository( |
||
67 | PersistenceHandler $persistenceHandler, |
||
68 | SearchHandler $searchHandler, |
||
69 | BackgroundIndexer $backgroundIndexer, |
||
70 | RelationProcessor $relationProcessor, |
||
71 | FieldTypeRegistry $fieldTypeRegistry |
||
72 | ) { |
||
73 | $repository = new $this->repositoryClass( |
||
74 | $persistenceHandler, |
||
75 | $searchHandler, |
||
76 | $backgroundIndexer, |
||
77 | $relationProcessor, |
||
78 | $fieldTypeRegistry, |
||
79 | [ |
||
80 | 'role' => [ |
||
81 | 'limitationTypes' => $this->roleLimitations, |
||
82 | 'policyMap' => $this->policyMap, |
||
83 | ], |
||
84 | 'languages' => $this->container->getParameter('languages'), |
||
85 | ], |
||
86 | ); |
||
|
|||
87 | |||
88 | return $repository; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Registers a limitation type for the RoleService. |
||
93 | * |
||
94 | * @param string $limitationName |
||
95 | * @param \eZ\Publish\SPI\Limitation\Type $limitationType |
||
96 | */ |
||
97 | public function registerLimitationType($limitationName, SPILimitationType $limitationType) |
||
98 | { |
||
99 | $this->roleLimitations[$limitationName] = $limitationType; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Returns a service based on a name string (content => contentService, etc). |
||
104 | * |
||
105 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
106 | * @param string $serviceName |
||
107 | * |
||
108 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException |
||
109 | * |
||
110 | * @return mixed |
||
111 | */ |
||
112 | public function buildService(Repository $repository, $serviceName) |
||
113 | { |
||
114 | $methodName = 'get' . $serviceName . 'Service'; |
||
115 | if (!method_exists($repository, $methodName)) { |
||
116 | throw new InvalidArgumentException($serviceName, 'No such service'); |
||
117 | } |
||
118 | |||
119 | return $repository->$methodName(); |
||
120 | } |
||
121 | } |
||
122 |