-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathShellApplication.php
330 lines (286 loc) · 11.5 KB
/
ShellApplication.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
namespace PHPCR\Shell\Console\Application;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use PHPCR\Shell\Console\Command\Phpcr as CommandPhpcr;
use PHPCR\Shell\Console\Command\Shell as CommandShell;
use PHPCR\Shell\Event;
use PHPCR\Shell\Event\ApplicationInitEvent;
use PHPCR\Shell\Event\PhpcrShellEvents;
use PHPCR\Shell\Console\Command\Phpcr\PhpcrShellCommand;
use PHPCR\Shell\Config\Profile;
/**
* Main application for PHPCRSH
*
* @author Daniel Leech <[email protected]>
*/
class ShellApplication extends Application
{
/**
* True when application has been initialized once
*
* @var boolean
*/
protected $initialized;
/**
* @var boolean
*/
protected $showUnsupported = false;
/**
* @var Symfony\Component\DependencyInjection\ContainerBuilder
*/
protected $container;
/**
* @var boolean
*/
protected $debug = false;
/**
* Constructor - name and version inherited from SessionApplication
*
* {@inheritDoc}
*/
public function __construct($container)
{
parent::__construct(SessionApplication::APP_NAME, SessionApplication::APP_VERSION);
$this->dispatcher = $container->get('event.dispatcher') ? : new EventDispatcher();
$this->setDispatcher($this->dispatcher);
$this->container = $container;
}
/**
* If true, show all commands, even if they are unsupported by the
* transport.
*
* @param boolean $boolean
*/
public function setShowUnsupported($boolean)
{
$this->showUnsupported = $boolean;
}
/**
* Initialize the application.
*
* Note that we do this "lazily" because we instantiate the ShellApplication early,
* before the SessionInput has been set. The SessionInput must be set before we
* can initialize the application.
*
* @todo: The above scenario is no longer true, we use a Profile. So maybe it can
* be refactored.
*/
public function init()
{
if (true === $this->initialized) {
return;
}
$this->registerPhpcrCommands();
$this->registerPhpcrStandaloneCommands();
$this->registerShellCommands();
$event = new ApplicationInitEvent($this);
$this->dispatcher->dispatch(PhpcrShellEvents::APPLICATION_INIT, $event);
$this->initialized = true;
}
/**
* Register the commands used in the shell
*/
protected function registerPhpcrCommands()
{
// phpcr commands
$this->add(new CommandPhpcr\AccessControlPrivilegeListCommand());
$this->add(new CommandPhpcr\RepositoryDescriptorListCommand());
$this->add(new CommandPhpcr\SessionExportCommand());
$this->add(new CommandPhpcr\SessionImpersonateCommand());
$this->add(new CommandPhpcr\SessionImportCommand());
$this->add(new CommandPhpcr\SessionInfoCommand());
$this->add(new CommandPhpcr\SessionNamespaceListCommand());
$this->add(new CommandPhpcr\SessionNamespaceSetCommand());
$this->add(new CommandPhpcr\NodePropertyRemoveCommand());
$this->add(new CommandPhpcr\NodePropertyShowCommand());
$this->add(new CommandPhpcr\SessionRefreshCommand());
$this->add(new CommandPhpcr\SessionSaveCommand());
$this->add(new CommandPhpcr\QueryCommand());
$this->add(new CommandPhpcr\QuerySelectCommand());
$this->add(new CommandPhpcr\QueryUpdateCommand());
$this->add(new CommandPhpcr\QueryDeleteCommand());
$this->add(new CommandPhpcr\RetentionHoldAddCommand());
$this->add(new CommandPhpcr\RetentionHoldListCommand());
$this->add(new CommandPhpcr\RetentionHoldRemoveCommand());
$this->add(new CommandPhpcr\RetentionPolicyGetCommand());
$this->add(new CommandPhpcr\RetentionPolicyRemoveCommand());
$this->add(new CommandPhpcr\NodeCloneCommand());
$this->add(new CommandPhpcr\NodeCopyCommand());
$this->add(new CommandPhpcr\NodeEditCommand());
$this->add(new CommandPhpcr\WorkspaceNamespaceListCommand());
$this->add(new CommandPhpcr\WorkspaceNamespaceRegisterCommand());
$this->add(new CommandPhpcr\WorkspaceNamespaceUnregisterCommand());
$this->add(new CommandPhpcr\NodeTypeShowCommand());
$this->add(new CommandPhpcr\NodeTypeEditCommand());
$this->add(new CommandPhpcr\NodeTypeUnregisterCommand());
$this->add(new CommandPhpcr\NodeTypeListCommand());
$this->add(new CommandPhpcr\NodeTypeLoadCommand());
$this->add(new CommandPhpcr\VersionCheckoutCommand());
$this->add(new CommandPhpcr\VersionHistoryCommand());
$this->add(new CommandPhpcr\VersionRestoreCommand());
$this->add(new CommandPhpcr\VersionRemoveCommand());
$this->add(new CommandPhpcr\VersionCheckpointCommand());
$this->add(new CommandPhpcr\VersionCheckinCommand());
$this->add(new CommandPhpcr\NodeCreateCommand());
$this->add(new CommandPhpcr\NodeCorrespondingCommand());
$this->add(new CommandPhpcr\NodeDefinitionCommand());
$this->add(new CommandPhpcr\NodeFileImportCommand());
$this->add(new CommandPhpcr\NodePropertySetCommand());
$this->add(new CommandPhpcr\NodeSetPrimaryTypeCommand());
$this->add(new CommandPhpcr\NodeRenameCommand());
$this->add(new CommandPhpcr\NodeMoveCommand());
$this->add(new CommandPhpcr\NodeMixinAddCommand());
$this->add(new CommandPhpcr\NodeMixinRemoveCommand());
$this->add(new CommandPhpcr\NodeOrderBeforeCommand());
$this->add(new CommandPhpcr\NodeInfoCommand());
$this->add(new CommandPhpcr\NodeLifecycleFollowCommand());
$this->add(new CommandPhpcr\NodeLifecycleListCommand());
$this->add(new CommandPhpcr\NodeListCommand());
$this->add(new CommandPhpcr\NodeUpdateCommand());
$this->add(new CommandPhpcr\NodeReferencesCommand());
$this->add(new CommandPhpcr\NodeSharedShowCommand());
$this->add(new CommandPhpcr\NodeSharedRemoveCommand());
$this->add(new CommandPhpcr\NodeRemoveCommand());
$this->add(new CommandPhpcr\LockLockCommand());
$this->add(new CommandPhpcr\LockInfoCommand());
$this->add(new CommandPhpcr\LockRefreshCommand());
$this->add(new CommandPhpcr\LockTokenAddCommand());
$this->add(new CommandPhpcr\LockTokenListCommand());
$this->add(new CommandPhpcr\LockTokenRemoveCommand());
$this->add(new CommandPhpcr\LockUnlockCommand());
}
protected function registerPhpcrStandaloneCommands()
{
$this->add(new CommandPhpcr\SessionLoginCommand());
$this->add(new CommandPhpcr\SessionLogoutCommand());
$this->add(new CommandPhpcr\WorkspaceUseCommand());
$this->add(new CommandPhpcr\WorkspaceCreateCommand());
$this->add(new CommandPhpcr\WorkspaceDeleteCommand());
$this->add(new CommandPhpcr\WorkspaceListCommand());
}
protected function registerShellCommands()
{
// add shell-specific commands
$this->add(new CommandShell\AliasListCommand());
$this->add(new CommandShell\ConfigInitCommand());
$this->add(new CommandShell\ConfigReloadCommand());
$this->add(new CommandShell\PathChangeCommand());
$this->add(new CommandShell\PathShowCommand());
$this->add(new CommandShell\ExitCommand());
}
/**
* Configure the output formatter
*/
private function configureFormatter(OutputFormatter $formatter)
{
$style = new OutputFormatterStyle('yellow', null, array('bold'));
$formatter->setStyle('path', $style);
$style = new OutputFormatterStyle('green');
$formatter->setStyle('localname', $style);
$style = new OutputFormatterStyle(null, null, array('bold'));
$formatter->setStyle('node', $style);
$style = new OutputFormatterStyle('blue', null, array('bold'));
$formatter->setStyle('templatenode', $style);
$style = new OutputFormatterStyle('blue', null, array());
$formatter->setStyle('templateproperty', $style);
$style = new OutputFormatterStyle(null, null, array());
$formatter->setStyle('property', $style);
$style = new OutputFormatterStyle('magenta', null, array('bold'));
$formatter->setStyle('node-type', $style);
$style = new OutputFormatterStyle('magenta', null, array());
$formatter->setStyle('property-type', $style);
$style = new OutputFormatterStyle(null, null, array());
$formatter->setStyle('property-value', $style);
$style = new OutputFormatterStyle(null, 'red', array());
$formatter->setStyle('exception', $style);
}
/**
* {@inheritDoc}
*/
public function doRun(InputInterface $input, OutputInterface $output)
{
$this->init();
// configure the formatter for the output
$this->configureFormatter($output->getFormatter());
$name = $this->getCommandName($input);
$event = new Event\CommandPreRunEvent($name, $input);
$this->dispatcher->dispatch(PhpcrShellEvents::COMMAND_PRE_RUN, $event);
$input = $event->getInput();
if (!$name) {
$input = new ArrayInput(array('command' => $this->getDefaultCommand()));
}
try {
$exitCode = parent::doRun($input, $output);
} catch (\Exception $e) {
$this->dispatcher->dispatch(PhpcrShellEvents::COMMAND_EXCEPTION, new Event\CommandExceptionEvent($e, $this, $output));
return 1;
}
return $exitCode;
}
/**
* Return the default command
*/
protected function getDefaultCommand()
{
return 'shell:path:show';
}
/**
* Render an exception to the console
*
* {@inheritDoc}
*/
public function renderException($exception, $output)
{
$output->writeln(sprintf('<exception>%s</exception>', $exception->getMessage()));
}
/**
* Wrap the add method and do not register commands which are unsupported by
* the current transport.
*
* {@inheritDoc}
*/
public function add(Command $command)
{
if ($command instanceof ContainerAwareInterface) {
$command->setContainer($this->container);
}
if ($command instanceof PhpcrShellCommand) {
if ($this->showUnsupported || $command->isSupported()) {
parent::add($command);
}
} else {
parent::add($command);
}
}
public function dispatchProfileInitEvent(InputInterface $sessionInput, OutputInterface $output)
{
$event = new Event\ProfileInitEvent($this->container->get('config.profile'), $sessionInput, $output);
$this->dispatcher->dispatch(PhpcrShellEvents::PROFILE_INIT, $event);
}
public function getContainer()
{
return $this->container;
}
/**
* Return if the shell is in debug mode
*/
public function isDebug()
{
return $this->debug;
}
/**
* Debug mode -- more verbose exceptions
*/
public function setDebug($debug)
{
$this->debug = $debug;
}
}