Skip to content

Commit d989623

Browse files
committed
Added shell exit command
1 parent d126ce8 commit d989623

File tree

2 files changed

+75
-8
lines changed

2 files changed

+75
-8
lines changed

src/PHPCR/Shell/Console/Application/ShellApplication.php

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,42 @@
88
use Symfony\Component\Console\Input\InputInterface;
99
use Symfony\Component\Console\Output\OutputInterface;
1010

11-
use PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper;
11+
use PHPCR\Shell\Console\Command\Query\SelectCommand;
12+
use PHPCR\Shell\Console\Command\Shell\ChangePathCommand;
13+
use PHPCR\Shell\Console\Command\Shell\PwdCommand;
1214
use PHPCR\Shell\Console\Helper\ResultFormatterHelper;
13-
use PHPCR\Util\Console\Helper\PhpcrHelper;
14-
use PHPCR\Util\Console\Helper\PhpcrCliHelper;
15+
use PHPCR\Shell\PhpcrSession;
16+
use PHPCR\SimpleCredentials;
1517
use PHPCR\Util\Console\Command\NodeDumpCommand;
16-
use PHPCR\Shell\Console\Command\Query\SelectCommand;
1718
use PHPCR\Util\Console\Command\NodeMoveCommand;
1819
use PHPCR\Util\Console\Command\NodeRemoveCommand;
19-
use PHPCR\Util\Console\Command\NodesUpdateCommand;
2020
use PHPCR\Util\Console\Command\NodeTouchCommand;
2121
use PHPCR\Util\Console\Command\NodeTypeListCommand;
2222
use PHPCR\Util\Console\Command\NodeTypeRegisterCommand;
23+
use PHPCR\Util\Console\Command\NodesUpdateCommand;
2324
use PHPCR\Util\Console\Command\WorkspaceCreateCommand;
2425
use PHPCR\Util\Console\Command\WorkspaceDeleteCommand;
2526
use PHPCR\Util\Console\Command\WorkspaceExportCommand;
2627
use PHPCR\Util\Console\Command\WorkspaceImportCommand;
2728
use PHPCR\Util\Console\Command\WorkspaceListCommand;
2829
use PHPCR\Util\Console\Command\WorkspacePurgeCommand;
30+
use PHPCR\Util\Console\Helper\PhpcrCliHelper;
31+
use PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper;
32+
use PHPCR\Util\Console\Helper\PhpcrHelper;
2933
use Symfony\Component\Console\Command\Command;
30-
use PHPCR\Shell\Console\Command\Shell\ChangePathCommand;
31-
use PHPCR\Shell\Console\Command\Shell\PwdCommand;
3234
use Symfony\Component\Console\Input\ArrayInput;
35+
use PHPCR\Shell\Console\Command\Shell\ExitCommand;
3336

3437
class ShellApplication extends Application
3538
{
36-
public function __construct(SessionInterface $session)
39+
public function __construct(InputInterface $input)
3740
{
3841
parent::__construct('PHPCR', '1.0');
3942

4043
$this->add(new SelectCommand());
4144
$this->add(new ChangePathCommand());
4245
$this->add(new PwdCommand());
46+
$this->add(new ExitCommand());
4347

4448
$this->add($this->wrap(new NodeDumpCommand())
4549
->setName('ls')
@@ -85,12 +89,52 @@ public function __construct(SessionInterface $session)
8589
->setName('workspace-purge')
8690
);
8791

92+
$session = $this->getSession($input, $workspace);
93+
8894
$this->getHelperSet()->set(new PhpcrConsoleDumperHelper());
8995
$this->getHelperSet()->set(new ResultFormatterHelper());
9096
$this->getHelperSet()->set(new PhpcrHelper($session));
9197
$this->getHelperSet()->set(new PhpcrCliHelper($session));
9298
}
9399

100+
private function getSession($input)
101+
{
102+
$transport = $this->getTransport($input);
103+
$repository = $transport->getRepository();
104+
$credentials = new SimpleCredentials(
105+
$input->getOption('phpcr-username'),
106+
$input->getOption('phpcr-password')
107+
);
108+
109+
$session = $repository->login($credentials, $input->getOption('phpcr-workspace'));
110+
$session = new PhpcrSession($session);
111+
112+
return $session;
113+
}
114+
115+
private function getTransport(InputInterface $input)
116+
{
117+
foreach (array(
118+
new \PHPCR\Shell\Transport\DoctrineDbal($input),
119+
new \PHPCR\Shell\Transport\Jackrabbit($input),
120+
) as $transport) {
121+
$transports[$transport->getName()] = $transport;
122+
}
123+
124+
$transportName = $input->getOption('transport');
125+
126+
if (!isset($transports[$transportName])) {
127+
throw new \InvalidArgumentException(sprintf(
128+
'Unknown transport "%s", I have "%s"',
129+
$transportName, implode(', ', array_keys($transports))
130+
));
131+
}
132+
133+
$transport = $transports[$transportName];
134+
135+
return $transport;
136+
}
137+
94138
public function wrap(Command $command)
95139
{
96140
return $command;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Console\Command\Shell;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
class ExitCommand extends Command
10+
{
11+
public function configure()
12+
{
13+
$this->setName('exit');
14+
$this->setDescription('Logout and quit the shell');
15+
}
16+
17+
public function execute(InputInterface $input, OutputInterface $output)
18+
{
19+
$this->getHelper('phpcr')->getSession()->logout();
20+
$output->writeln('<info>Bye!</info>');
21+
exit(0);
22+
}
23+
}

0 commit comments

Comments
 (0)