1 | <?php |
||
41 | class SiteRepository |
||
42 | { |
||
43 | /** |
||
44 | * Rootpage resolver |
||
45 | * |
||
46 | * @var RootPageResolver |
||
47 | */ |
||
48 | protected $rootPageResolver; |
||
49 | |||
50 | /** |
||
51 | * @var TwoLevelCache |
||
52 | */ |
||
53 | protected $runtimeCache; |
||
54 | |||
55 | /** |
||
56 | * @var Registry |
||
57 | */ |
||
58 | protected $registry; |
||
59 | |||
60 | /** |
||
61 | * SiteRepository constructor. |
||
62 | * |
||
63 | * @param RootPageResolver|null $rootPageResolver |
||
64 | * @param TwoLevelCache|null $twoLevelCache |
||
65 | * @param Registry|null $registry |
||
66 | */ |
||
67 | 128 | public function __construct(RootPageResolver $rootPageResolver = null, TwoLevelCache $twoLevelCache = null, Registry $registry = null) |
|
68 | { |
||
69 | 128 | $this->rootPageResolver = isset($rootPageResolver) ? $rootPageResolver : GeneralUtility::makeInstance(RootPageResolver::class); |
|
70 | 128 | $this->runtimeCache = isset($twoLevelCache) ? $twoLevelCache : GeneralUtility::makeInstance(TwoLevelCache::class, 'cache_runtime'); |
|
71 | 128 | $this->registry = isset($registry) ? $registry : GeneralUtility::makeInstance(Registry::class); |
|
72 | 128 | } |
|
73 | |||
74 | /** |
||
75 | * Gets the Site for a specific page Id. |
||
76 | * |
||
77 | * @param int $pageId The page Id to get a Site object for. |
||
78 | * @return Site Site for the given page Id. |
||
79 | */ |
||
80 | 66 | public function getSiteByPageId($pageId) |
|
81 | { |
||
82 | 66 | $rootPageId = $this->rootPageResolver->getRootPageId($pageId); |
|
83 | 66 | return $this->getSiteByRootPageId($rootPageId); |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Gets the Site for a specific root page Id. |
||
88 | * |
||
89 | * @param int $rootPageId Root page Id to get a Site object for. |
||
90 | * @return Site Site for the given page Id. |
||
91 | */ |
||
92 | 74 | public function getSiteByRootPageId($rootPageId) |
|
106 | |||
107 | /** |
||
108 | * Returns the first available Site. |
||
109 | * |
||
110 | * @param bool $stopOnInvalidSite |
||
111 | * @return Site |
||
112 | */ |
||
113 | 22 | public function getFirstAvailableSite($stopOnInvalidSite = false) |
|
114 | { |
||
115 | 22 | $sites = $this->getAvailableSites($stopOnInvalidSite); |
|
116 | 22 | return array_shift($sites); |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Gets all available TYPO3 sites with Solr configured. |
||
121 | * |
||
122 | * @param bool $stopOnInvalidSite |
||
123 | * @return Site[] An array of available sites |
||
124 | */ |
||
125 | 66 | public function getAvailableSites($stopOnInvalidSite = false) |
|
126 | { |
||
127 | 66 | $sites = []; |
|
128 | 66 | $cacheId = 'SiteRepository' . '_' . 'getAvailableSites'; |
|
129 | |||
130 | 66 | $methodResult = $this->runtimeCache->get($cacheId); |
|
131 | 66 | if (!empty($methodResult)) { |
|
132 | 15 | return $methodResult; |
|
133 | } |
||
134 | |||
135 | 66 | $servers = $this->getSolrServersFromRegistry(); |
|
136 | 66 | foreach ($servers as $server) { |
|
137 | 66 | if (isset($sites[$server['rootPageUid']])) { |
|
138 | //get each site only once |
||
139 | 4 | continue; |
|
140 | } |
||
141 | |||
142 | try { |
||
143 | 66 | $sites[$server['rootPageUid']] = $this->buildSite($server['rootPageUid']); |
|
144 | 4 | } catch (\InvalidArgumentException $e) { |
|
145 | 4 | if ($stopOnInvalidSite) { |
|
146 | throw $e; |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | |||
151 | 66 | $methodResult = $sites; |
|
152 | 66 | $this->runtimeCache->set($cacheId, $methodResult); |
|
153 | |||
154 | 66 | return $methodResult; |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * Gets the system languages (IDs) for which Solr connections have been |
||
159 | * configured. |
||
160 | * |
||
161 | * @return array Array of system language IDs for which connections have been configured on this site. |
||
162 | */ |
||
163 | 1 | public function getAllLanguages(Site $site) |
|
164 | { |
||
165 | 1 | $siteLanguages = []; |
|
166 | 1 | $servers = $this->getSolrServersFromRegistry(); |
|
167 | |||
168 | 1 | foreach ($servers as $connectionKey => $solrConnection) { |
|
169 | 1 | list($siteRootPageId, $systemLanguageId) = explode('|', $connectionKey); |
|
170 | |||
171 | 1 | if ($siteRootPageId == $site->getRootPageId()) { |
|
172 | 1 | $siteLanguages[] = $systemLanguageId; |
|
173 | } |
||
174 | } |
||
175 | |||
176 | 1 | return $siteLanguages; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * Creates an instance of the Site object. |
||
181 | * |
||
182 | * @param integer $rootPageId |
||
183 | * @return Site |
||
184 | */ |
||
185 | 109 | protected function buildSite($rootPageId) |
|
189 | |||
190 | /** |
||
191 | * Retrieves the configured solr servers from the registry. |
||
192 | * |
||
193 | * @return array |
||
194 | */ |
||
195 | 66 | protected function getSolrServersFromRegistry() |
|
200 | } |
||
201 |