-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInspectorCommand.php
More file actions
225 lines (196 loc) · 6.93 KB
/
InspectorCommand.php
File metadata and controls
225 lines (196 loc) · 6.93 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
declare(strict_types=1);
namespace OpenForgeProject\MageForge\Console\Command\Dev;
use Magento\Framework\App\Cache\Manager as CacheManager;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\App\State;
use Magento\Framework\Console\Cli;
use OpenForgeProject\MageForge\Console\Command\AbstractCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Command to enable, disable or check status of MageForge Inspector
*/
class InspectorCommand extends AbstractCommand
{
private const XML_PATH_INSPECTOR_ENABLED = 'dev/mageforge_inspector/enabled';
private const ARGUMENT_ACTION = 'action';
/**
* @param WriterInterface $configWriter
* @param State $state
* @param CacheManager $cacheManager
* @param ScopeConfigInterface $scopeConfig
* @param string|null $name
*/
public function __construct(
private readonly WriterInterface $configWriter,
private readonly State $state,
private readonly CacheManager $cacheManager,
private readonly ScopeConfigInterface $scopeConfig,
?string $name = null,
) {
parent::__construct($name);
}
/**
* Configure command
*
* @return void
*/
protected function configure(): void
{
$this
->setName($this->getCommandName('theme', 'inspector'))
->setDescription('Manage MageForge Frontend Inspector (Actions: enable|disable|status)')
->addArgument(
self::ARGUMENT_ACTION,
InputArgument::REQUIRED,
'Action to perform: enable, disable, or status',
)
->setHelp(<<<HELP
The <info>%command.name%</info> command manages the MageForge Frontend Inspector:
<info>php %command.full_name%</info> <comment>enable</comment>
Enable the inspector (requires developer mode)
<info>php %command.full_name%</info> <comment>disable</comment>
Disable the inspector
<info>php %command.full_name%</info> <comment>status</comment>
Show current inspector status
The inspector allows you to hover over frontend elements to see template paths,
block classes, modules, and other metadata. Activate with Ctrl+Shift+I.
HELP);
parent::configure();
}
/**
* Execute command
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function executeCommand(InputInterface $input, OutputInterface $output): int
{
$arg = $input->getArgument(self::ARGUMENT_ACTION);
$action = strtolower(is_string($arg) ? $arg : '');
// Validate action
if (!in_array($action, ['enable', 'disable', 'status'], true)) {
$this->io->error(sprintf('Invalid action "%s". Use: enable, disable, or status', $action));
return Cli::RETURN_FAILURE;
}
// Check developer mode for enable/disable actions
if (in_array($action, ['enable', 'disable'], true) && !$this->isDeveloperMode()) {
$this->io->error([
'Inspector can only be enabled/disabled in developer mode.',
'Current mode: ' . $this->state->getMode(),
'',
'To switch to developer mode, run:',
' bin/magento deploy:mode:set developer',
]);
return Cli::RETURN_FAILURE;
}
return match ($action) {
'enable' => $this->enableInspector(),
'disable' => $this->disableInspector(),
'status' => $this->showStatus(),
};
}
/**
* Enable inspector
*
* @return int
*/
private function enableInspector(): int
{
$this->configWriter->save(self::XML_PATH_INSPECTOR_ENABLED, '1');
$this->cleanCache();
$this->io->success('MageForge Inspector has been enabled!');
$this->io->writeln([
'The inspector will now be active on the frontend for allowed IPs.',
'',
'Usage:',
' • Press Ctrl+Shift+I (or Cmd+Option+I on macOS) to toggle the inspector',
' • Hover over elements to see their template information',
' • Click to pin the inspector panel',
'',
'<comment>Note: Inspector only works in developer mode and for allowed IPs</comment>',
]);
return Cli::RETURN_SUCCESS;
}
/**
* Disable inspector
*
* @return int
*/
private function disableInspector(): int
{
$this->configWriter->save(self::XML_PATH_INSPECTOR_ENABLED, '0');
$this->cleanCache();
$this->io->success('MageForge Inspector has been disabled.');
return Cli::RETURN_SUCCESS;
}
/**
* Show inspector status
*
* @return int
*/
private function showStatus(): int
{
$isDeveloperMode = $this->isDeveloperMode();
$isEnabled = $this->isInspectorEnabled();
$this->io->section('MageForge Inspector Status');
$this->io->writeln([
sprintf('Mode: <comment>%s</comment>', $this->state->getMode()),
sprintf('Inspector: %s', $isEnabled ? '<info>Enabled</info>' : '<comment>Disabled</comment>'),
]);
if (!$isDeveloperMode) {
$this->io->newLine();
$this->io->warning([
'Inspector requires developer mode to function.',
'Switch to developer mode with: bin/magento deploy:mode:set developer',
]);
} elseif (!$isEnabled) {
$this->io->newLine();
$this->io->note('Run "bin/magento mageforge:theme:inspector enable" to activate the inspector.');
} else {
$this->io->newLine();
$this->io->writeln('<info>✓</info> Inspector is active and ready to use!');
}
return Cli::RETURN_SUCCESS;
}
/**
* Check if Magento is in developer mode
*
* @return bool
*/
private function isDeveloperMode(): bool
{
try {
return $this->state->getMode() === State::MODE_DEVELOPER;
} catch (\Exception $e) {
return false;
}
}
/**
* Check if inspector is enabled in configuration
*
* @return bool
*/
private function isInspectorEnabled(): bool
{
try {
return $this->scopeConfig->isSetFlag(self::XML_PATH_INSPECTOR_ENABLED);
} catch (\Exception $e) {
return false;
}
}
/**
* Clean config cache
*
* @return void
*/
private function cleanCache(): void
{
$this->cacheManager->clean(['config']);
$this->io->writeln('<comment>Config cache cleaned</comment>');
}
}