-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathImporter.php
145 lines (118 loc) · 4.86 KB
/
Importer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Copyright © element119. All rights reserved.
* See LICENCE.txt for licence details.
*/
declare(strict_types=1);
namespace Element119\IndexerDeployConfig\Model\Config;
use InvalidArgumentException;
use Magento\Framework\App\DeploymentConfig\ImporterInterface;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Indexer\Console\Command\IndexerSetModeCommand as IndexerMode;
class Importer implements ImporterInterface
{
/** @var IndexerRegistry */
private IndexerRegistry $indexerRegistry;
/**
* @param IndexerRegistry $indexerRegistry
*/
public function __construct(
IndexerRegistry $indexerRegistry
) {
$this->indexerRegistry = $indexerRegistry;
}
/**
* Import indexer config.
*
* @param array $indexerConfig
* @return string[]
*/
public function import(array $indexerConfig): array
{
$messages = [];
foreach ($indexerConfig as $mode => $indexers) {
foreach ($indexers as $indexerId) {
try {
$indexer = $this->indexerRegistry->get($indexerId);
} catch (InvalidArgumentException $e) {
$messages[] = "<info>{$indexerId} does not exist, skipping</info>";
continue;
}
$isRealTimeMode = $mode === IndexerMode::INPUT_KEY_REALTIME;
$isScheduleMode = $mode === IndexerMode::INPUT_KEY_SCHEDULE;
// only change index mode when different, slightly more performant
if ($indexer->isScheduled() && $isRealTimeMode) {
$indexer->setScheduled(false);
$messages[] = "<info>{$indexerId} updated to \"Update on Save\".</info>";
} elseif (!$indexer->isScheduled() && $isScheduleMode) {
$indexer->setScheduled(true);
$messages[] = "<info>{$indexerId} updated to \"Update by Schedule\".</info>";
}
}
}
return $messages;
}
/**
* Provide user with information regarding what will change after importing indexer config.
*
* @param array $indexerConfig
* @return array|string[]
*/
public function getWarningMessages(array $indexerConfig): array
{
$messages = [];
$indexersToChangeToRealtime = [];
$indexersToChangeToSchedule = [];
$realtimeLockedIndexers = [];
$scheduleLockedIndexers = [];
foreach ($indexerConfig as $mode => $indexers) {
foreach ($indexers as $indexerId) {
try {
$indexer = $this->indexerRegistry->get($indexerId);
} catch (InvalidArgumentException $e) {
continue;
}
$isRealTimeMode = $mode === IndexerMode::INPUT_KEY_REALTIME;
$isScheduleMode = $mode === IndexerMode::INPUT_KEY_SCHEDULE;
if ($indexer->isScheduled() && $isRealTimeMode) {
$indexersToChangeToRealtime[] = $indexerId;
} elseif (!$indexer->isScheduled() && $isScheduleMode) {
$indexersToChangeToSchedule[] = $indexerId;
}
if ($isRealTimeMode) {
$realtimeLockedIndexers[] = $indexerId;
} elseif ($isScheduleMode) {
$scheduleLockedIndexers[] = $indexerId;
}
}
}
if ($indexersToChangeToRealtime) {
$messages[] = "<info>The following indexers will be changed to \"Update on Save\":</info>\n"
. implode("\n", $indexersToChangeToRealtime);
// new line separator, "\n" didn't work for some reason ¯\_(ツ)_/¯
$messages[] = '';
}
if ($indexersToChangeToSchedule) {
$messages[] = "<info>The following indexers will be changed to \"Update by Schedule\":</info>\n"
. implode("\n", $indexersToChangeToSchedule);
// new line separator, "\n" didn't work for some reason ¯\_(ツ)_/¯
$messages[] = '';
}
if ($realtimeLockedIndexers) {
$messages[] = "<info>The following indexers will be locked to \"Update on Save\":</info>\n"
. implode("\n", $realtimeLockedIndexers);
// new line separator, "\n" didn't work for some reason ¯\_(ツ)_/¯
$messages[] = '';
}
if ($scheduleLockedIndexers) {
$messages[] = "<info>The following indexers will be locked to \"Update by Schedule\":</info>\n"
. implode("\n", $scheduleLockedIndexers);
// new line separator, "\n" didn't work for some reason ¯\_(ツ)_/¯
$messages[] = '';
}
if ($messages) {
$messages[] = '<info>All other indexers will be unlocked.</info>';
}
return $messages;
}
}