11<?php
2+
3+ declare (strict_types=1 );
4+
25namespace Flowpack \ElasticSearch \ContentRepositoryAdaptor \Command ;
36
47/*
1114 * source code.
1215 */
1316
17+ use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Exception as CRAException ;
1418use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Indexer \Error \ErrorInterface ;
1519use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Driver \NodeTypeMappingBuilderInterface ;
16- use Flowpack \ElasticSearch \ContentRepositoryAdaptor \LoggerInterface ;
20+ use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Indexer \ NodeIndexer ;
1721use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Service \ErrorHandlingService ;
1822use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Service \IndexWorkspaceTrait ;
1923use Flowpack \ElasticSearch \Domain \Model \Mapping ;
2832use Neos \Flow \Cli \CommandController ;
2933use Neos \Flow \Configuration \ConfigurationManager ;
3034use Neos \Flow \Configuration \Exception \InvalidConfigurationTypeException ;
35+ use Neos \Flow \Log \Utility \LogEnvironment ;
3136use Neos \Flow \Mvc \Exception \StopActionException ;
3237use Neos \Flow \ObjectManagement \ObjectManagerInterface ;
3338use Neos \Neos \Controller \CreateContentContextTrait ;
39+ use Psr \Log \LoggerInterface ;
3440use Symfony \Component \Yaml \Yaml ;
3541
3642/**
@@ -51,8 +57,7 @@ class NodeIndexCommandController extends CommandController
5157 protected $ errorHandlingService ;
5258
5359 /**
54- * @Flow\Inject
55- * @var NodeIndexerInterface
60+ * @var NodeIndexer
5661 */
5762 protected $ nodeIndexer ;
5863
@@ -103,13 +108,22 @@ class NodeIndexCommandController extends CommandController
103108 */
104109 protected $ settings ;
105110
111+ /**
112+ * @param NodeIndexerInterface $nodeIndexer
113+ * @return void
114+ */
115+ public function injectNodeIndexer (NodeIndexerInterface $ nodeIndexer ): void
116+ {
117+ $ this ->nodeIndexer = $ nodeIndexer ;
118+ }
119+
106120 /**
107121 * Called by the Flow object framework after creating the object and resolving all dependencies.
108122 *
109123 * @param integer $cause Creation cause
110124 * @throws InvalidConfigurationTypeException
111125 */
112- public function initializeObject ($ cause )
126+ public function initializeObject (int $ cause ): void
113127 {
114128 if ($ cause === ObjectManagerInterface::INITIALIZATIONCAUSE_CREATED ) {
115129 $ this ->settings = $ this ->configurationManager ->getConfiguration (ConfigurationManager::CONFIGURATION_TYPE_SETTINGS , 'Neos.ContentRepository.Search ' );
@@ -120,8 +134,9 @@ public function initializeObject($cause)
120134 * Show the mapping which would be sent to the ElasticSearch server
121135 *
122136 * @return void
137+ * @throws CRAException
123138 */
124- public function showMappingCommand ()
139+ public function showMappingCommand (): void
125140 {
126141 $ nodeTypeMappingCollection = $ this ->nodeTypeMappingBuilder ->buildMappingInformation ($ this ->nodeIndexer ->getIndex ());
127142 foreach ($ nodeTypeMappingCollection as $ mapping ) {
@@ -145,7 +160,7 @@ public function showMappingCommand()
145160 $ this ->outputLine ('<b>Mapping Warnings</b> ' );
146161 foreach ($ mappingErrors ->getFlattenedWarnings () as $ warnings ) {
147162 foreach ($ warnings as $ warning ) {
148- $ this ->outputLine ($ warning );
163+ $ this ->outputLine (( string ) $ warning );
149164 }
150165 }
151166 }
@@ -159,7 +174,7 @@ public function showMappingCommand()
159174 * @return void
160175 * @throws StopActionException
161176 */
162- public function indexNodeCommand ($ identifier , $ workspace = null )
177+ public function indexNodeCommand (string $ identifier , string $ workspace = null ): void
163178 {
164179 if ($ workspace === null && $ this ->settings ['indexAllWorkspaces ' ] === false ) {
165180 $ workspace = 'live ' ;
@@ -201,8 +216,8 @@ public function indexNodeCommand($identifier, $workspace = null)
201216 };
202217
203218 if ($ workspace === null ) {
204- foreach ($ this ->workspaceRepository ->findAll () as $ workspace ) {
205- $ indexInWorkspace ($ identifier , $ workspace );
219+ foreach ($ this ->workspaceRepository ->findAll () as $ workspaceToIndex ) {
220+ $ indexInWorkspace ($ identifier , $ workspaceToIndex );
206221 }
207222 } else {
208223 /** @var Workspace $workspaceInstance */
@@ -220,28 +235,30 @@ public function indexNodeCommand($identifier, $workspace = null)
220235 *
221236 * This command (re-)indexes all nodes contained in the content repository and sets the schema beforehand.
222237 *
223- * @param integer $limit Amount of nodes to index at maximum
224- * @param boolean $update if TRUE, do not throw away the index at the start. Should *only be used for development*.
238+ * @param int $limit Amount of nodes to index at maximum
239+ * @param bool $update if TRUE, do not throw away the index at the start. Should *only be used for development*.
225240 * @param string $workspace name of the workspace which should be indexed
226241 * @param string $postfix Index postfix, index with the same postfix will be deleted if exist
227242 * @return void
243+ * @throws ApiException
228244 * @throws StopActionException
245+ * @throws CRAException
229246 */
230- public function buildCommand ($ limit = null , $ update = false , $ workspace = null , $ postfix = '' )
247+ public function buildCommand (int $ limit = null , bool $ update = false , string $ workspace = null , string $ postfix = '' ): void
231248 {
232249 if ($ workspace !== null && $ this ->workspaceRepository ->findByIdentifier ($ workspace ) === null ) {
233- $ this ->logger ->log ('The given workspace ( ' . $ workspace . ') does not exist. ' , LOG_ERR );
250+ $ this ->logger ->error ('The given workspace ( ' . $ workspace . ') does not exist. ' , LogEnvironment:: fromMethodName ( __METHOD__ ) );
234251 $ this ->quit (1 );
235252 }
236253
237254 if ($ update === true ) {
238- $ this ->logger ->log ('!!! Update Mode (Development) active! ' , LOG_INFO );
255+ $ this ->logger ->warning ('!!! Update Mode (Development) active! ' , LogEnvironment:: fromMethodName ( __METHOD__ ) );
239256 } else {
240257 $ this ->createNewIndex ($ postfix );
241258 }
242259 $ this ->applyMapping ();
243260
244- $ this ->logger ->log (sprintf ('Indexing %snodes ... ' , ($ limit !== null ? 'the first ' . $ limit . ' ' : '' )), LOG_INFO );
261+ $ this ->logger ->info (sprintf ('Indexing %snodes ... ' , ($ limit !== null ? 'the first ' . $ limit . ' ' : '' )), LogEnvironment:: fromMethodName ( __METHOD__ ) );
245262
246263 $ count = 0 ;
247264
@@ -257,8 +274,8 @@ public function buildCommand($limit = null, $update = false, $workspace = null,
257274 }
258275 };
259276 if ($ workspace === null ) {
260- foreach ($ this ->workspaceRepository ->findAll () as $ workspace ) {
261- $ count += $ this ->indexWorkspace ($ workspace ->getName (), $ limit , $ callback );
277+ foreach ($ this ->workspaceRepository ->findAll () as $ workspaceToIndex ) {
278+ $ count += $ this ->indexWorkspace ($ workspaceToIndex ->getName (), $ limit , $ callback );
262279 }
263280 } else {
264281 $ count += $ this ->indexWorkspace ($ workspace , $ limit , $ callback );
@@ -275,7 +292,7 @@ public function buildCommand($limit = null, $update = false, $workspace = null,
275292 $ this ->outputLine ();
276293 $ this ->outputLine ('<error>Check your logs for more information</error> ' );
277294 } else {
278- $ this ->logger ->log ('Done. (indexed ' . $ count . ' nodes) ' , LOG_INFO );
295+ $ this ->logger ->info ('Done. (indexed ' . $ count . ' nodes) ' , LogEnvironment:: fromMethodName ( __METHOD__ ) );
279296 }
280297 $ this ->nodeIndexer ->getIndex ()->refresh ();
281298
@@ -289,24 +306,25 @@ public function buildCommand($limit = null, $update = false, $workspace = null,
289306 * Clean up old indexes (i.e. all but the current one)
290307 *
291308 * @return void
309+ * @throws CRAException
292310 */
293- public function cleanupCommand ()
311+ public function cleanupCommand (): void
294312 {
295313 try {
296314 $ indicesToBeRemoved = $ this ->nodeIndexer ->removeOldIndices ();
297315 if (count ($ indicesToBeRemoved ) > 0 ) {
298316 foreach ($ indicesToBeRemoved as $ indexToBeRemoved ) {
299- $ this ->logger ->log ('Removing old index ' . $ indexToBeRemoved );
317+ $ this ->logger ->info ('Removing old index ' . $ indexToBeRemoved, LogEnvironment:: fromMethodName ( __METHOD__ ) );
300318 }
301319 } else {
302- $ this ->logger ->log ('Nothing to remove. ' );
320+ $ this ->logger ->info ('Nothing to remove. ' , LogEnvironment:: fromMethodName ( __METHOD__ ) );
303321 }
304322 } catch (ApiException $ exception ) {
305323 $ response = json_decode ($ exception ->getResponse ());
306324 if ($ response ->error instanceof \stdClass) {
307- $ this ->logger ->log (sprintf ('Nothing removed. ElasticSearch responded with status %s, saying "%s: %s" ' , $ response ->status , $ response ->error ->type , $ response ->error ->reason ));
325+ $ this ->logger ->info (sprintf ('Nothing removed. ElasticSearch responded with status %s, saying "%s: %s" ' , $ response ->status , $ response ->error ->type , $ response ->error ->reason ), LogEnvironment:: fromMethodName ( __METHOD__ ));
308326 } else {
309- $ this ->logger ->log (sprintf ('Nothing removed. ElasticSearch responded with status %s, saying "%s" ' , $ response ->status , $ response ->error ));
327+ $ this ->logger ->info (sprintf ('Nothing removed. ElasticSearch responded with status %s, saying "%s" ' , $ response ->status , $ response ->error ), LogEnvironment:: fromMethodName ( __METHOD__ ));
310328 }
311329 }
312330 }
@@ -316,30 +334,32 @@ public function cleanupCommand()
316334 *
317335 * @param string $postfix
318336 * @return void
337+ * @throws CRAException
319338 */
320- protected function createNewIndex (string $ postfix )
339+ protected function createNewIndex (string $ postfix ): void
321340 {
322- $ this ->nodeIndexer ->setIndexNamePostfix ($ postfix ?: time ());
341+ $ this ->nodeIndexer ->setIndexNamePostfix ($ postfix ?: ( string ) time ());
323342 if ($ this ->nodeIndexer ->getIndex ()->exists () === true ) {
324- $ this ->logger ->log (sprintf ('Deleted index with the same postfix (%s)! ' , $ postfix ), LOG_WARNING );
343+ $ this ->logger ->warning (sprintf ('Deleted index with the same postfix (%s)! ' , $ postfix ), LogEnvironment:: fromMethodName ( __METHOD__ ) );
325344 $ this ->nodeIndexer ->getIndex ()->delete ();
326345 }
327346 $ this ->nodeIndexer ->getIndex ()->create ();
328- $ this ->logger ->log ('Created index ' . $ this ->nodeIndexer ->getIndexName (), LOG_INFO );
347+ $ this ->logger ->info ('Created index ' . $ this ->nodeIndexer ->getIndexName (), LogEnvironment:: fromMethodName ( __METHOD__ ) );
329348 }
330349
331350 /**
332351 * Apply the mapping to the current index.
333352 *
334353 * @return void
354+ * @throws CRAException
335355 */
336- protected function applyMapping ()
356+ protected function applyMapping (): void
337357 {
338358 $ nodeTypeMappingCollection = $ this ->nodeTypeMappingBuilder ->buildMappingInformation ($ this ->nodeIndexer ->getIndex ());
339359 foreach ($ nodeTypeMappingCollection as $ mapping ) {
340360 /** @var Mapping $mapping */
341361 $ mapping ->apply ();
342362 }
343- $ this ->logger ->log ('Updated Mapping. ' , LOG_INFO );
363+ $ this ->logger ->info ('Updated Mapping. ' , LogEnvironment:: fromMethodName ( __METHOD__ ) );
344364 }
345365}
0 commit comments