Skip to content

Commit 60bb7ad

Browse files
committed
Wrapped phpcr-util commands
todo: Wrap the PHPCR session to use the CWD
1 parent 2ecf2b5 commit 60bb7ad

File tree

8 files changed

+176
-189
lines changed

8 files changed

+176
-189
lines changed

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

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Process\PhpExecutableFinder;
99
use Symfony\Component\Console\Shell as BaseShell;
1010
use PHPCR\Shell\Console\Input\StringInput;
11+
use PHPCR\Shell\Context;
1112

1213
class Shell
1314
{
@@ -43,6 +44,7 @@ public function run()
4344
{
4445
$this->application->setAutoExit(false);
4546
$this->application->setCatchExceptions(true);
47+
$context = new Context('/');
4648

4749
if ($this->hasReadline) {
4850
readline_read_history($this->history);
@@ -65,26 +67,7 @@ public function run()
6567
readline_write_history($this->history);
6668
}
6769

68-
if ($this->processIsolation) {
69-
$pb = new ProcessBuilder();
70-
71-
$process = $pb
72-
->add($php)
73-
->add($_SERVER['argv'][0])
74-
->add($command)
75-
->inheritEnvironmentVariables(true)
76-
->getProcess()
77-
;
78-
79-
$output = $this->output;
80-
$process->run(function($type, $data) use ($output) {
81-
$output->writeln($data);
82-
});
83-
84-
$ret = $process->getExitCode();
85-
} else {
86-
$ret = $this->application->run(new StringInput($command), $this->output);
87-
}
70+
$ret = $this->application->run(new StringInput($command), $this->output);
8871

8972
if (0 !== $ret) {
9073
$this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));

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

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,109 @@
44

55
use Symfony\Component\Console\Application;
66
use PHPCR\SessionInterface;
7+
78
use Symfony\Component\Console\Input\InputInterface;
89
use Symfony\Component\Console\Output\OutputInterface;
9-
use PHPCR\Shell\Console\Command\AbstractSessionCommand;
10-
use PHPCR\Shell\Console\Command\Workspace\SelectCommand;
11-
use PHPCR\Shell\Console\Command\Workspace\NodeTypeListCommand;
10+
1211
use PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper;
13-
use PHPCR\Shell\Console\Command\Workspace\NodeDumpCommand;
1412
use PHPCR\Shell\Console\Helper\ResultFormatterHelper;
13+
use PHPCR\Util\Console\Helper\PhpcrHelper;
14+
use PHPCR\Util\Console\Helper\PhpcrCliHelper;
15+
use PHPCR\Util\Console\Command\NodeDumpCommand;
16+
use PHPCR\Shell\Console\Command\Query\SelectCommand;
17+
use PHPCR\Util\Console\Command\NodeMoveCommand;
18+
use PHPCR\Util\Console\Command\NodeRemoveCommand;
19+
use PHPCR\Util\Console\Command\NodesUpdateCommand;
20+
use PHPCR\Util\Console\Command\NodeTouchCommand;
21+
use PHPCR\Util\Console\Command\NodeTypeListCommand;
22+
use PHPCR\Util\Console\Command\NodeTypeRegisterCommand;
23+
use PHPCR\Util\Console\Command\WorkspaceCreateCommand;
24+
use PHPCR\Util\Console\Command\WorkspaceDeleteCommand;
25+
use PHPCR\Util\Console\Command\WorkspaceExportCommand;
26+
use PHPCR\Util\Console\Command\WorkspaceImportCommand;
27+
use PHPCR\Util\Console\Command\WorkspaceListCommand;
28+
use PHPCR\Util\Console\Command\WorkspacePurgeCommand;
29+
use Symfony\Component\Console\Command\Command;
30+
use PHPCR\Shell\Console\Command\Shell\ChangePathCommand;
31+
use PHPCR\Shell\Console\Command\Shell\PwdCommand;
1532

1633
class ShellApplication extends Application
1734
{
35+
protected $cwd;
36+
37+
public function getCwd()
38+
{
39+
return $this->cwd;
40+
}
41+
42+
public function setCwd($cwd)
43+
{
44+
$this->cwd = $cwd;
45+
}
46+
1847
public function __construct(SessionInterface $session)
1948
{
2049
parent::__construct('PHPCR', '1.0');
2150

2251
$this->add(new SelectCommand());
23-
$this->add(new NodeTypeListCommand());
24-
$this->add(new NodeDumpCommand());
52+
$this->add(new ChangePathCommand());
53+
$this->add(new PwdCommand());
54+
55+
$this->add($this->wrap(new NodeDumpCommand())
56+
->setName('ls')
57+
->setDescription('Alias for dump')
58+
);
59+
$this->add($this->wrap(new NodeMoveCommand())
60+
->setName('mv')
61+
);
62+
$this->add($this->wrap(new NodeRemoveCommand())
63+
->setName('rm')
64+
);
65+
$this->add($this->wrap(new NodesUpdateCommand())
66+
->setName('update')
67+
);
68+
$this->add($this->wrap(new NodeTouchCommand())
69+
->setName('touch')
70+
);
71+
$this->add($this->wrap(new NodeTypeListCommand())
72+
->setName('nt-list')
73+
);
74+
$this->add($this->wrap(new NodeTypeRegisterCommand())
75+
->setName('nt-register')
76+
);
77+
$this->add($this->wrap(new WorkspaceCreateCommand())
78+
->setName('workspace-create')
79+
);
80+
$this->add($this->wrap(new WorkspaceDeleteCommand())
81+
->setName('workspace-delete')
82+
);
83+
$this->add($this->wrap(new WorkspaceExportCommand())
84+
->setName('workspace-export')
85+
);
86+
$this->add($this->wrap(new WorkspaceImportCommand())
87+
->setName('workspace-import')
88+
);
89+
$this->add($this->wrap(new WorkspaceListCommand())
90+
->setName('workspace-list')
91+
);
92+
$this->add($this->wrap(new WorkspacePurgeCommand())
93+
->setName('workspace-purge')
94+
);
2595

2696
$this->getHelperSet()->set(new PhpcrConsoleDumperHelper());
2797
$this->getHelperSet()->set(new ResultFormatterHelper());
98+
$this->getHelperSet()->set(new PhpcrHelper($session));
99+
$this->getHelperSet()->set(new PhpcrCliHelper($session));
28100

29101
foreach ($this->all() as $command) {
30102
if ($command instanceof AbstractSessionCommand) {
31103
$command->setSession($session);
32104
}
33105
}
34106
}
107+
108+
public function wrap(Command $command)
109+
{
110+
return $command;
111+
}
35112
}

src/PHPCR/Shell/Console/Command/Workspace/SelectCommand.php renamed to src/PHPCR/Shell/Console/Command/Query/SelectCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace PHPCR\Shell\Console\Command\Workspace;
3+
namespace PHPCR\Shell\Console\Command\Query;
44

55
use PHPCR\Shell\Console\ShellQueryCommand;
66
use Symfony\Component\Console\Input\InputInterface;
@@ -23,6 +23,12 @@ protected function configure()
2323
public function execute(InputInterface $input, OutputInterface $output)
2424
{
2525
$sql = $input->getRawCommand();
26+
27+
// trim ";" for people used to MysQL
28+
if (substr($sql, -1) == ';') {
29+
$sql = substr($sql, 0, -1);
30+
}
31+
2632
$language = strtoupper($input->getOption('language'));
2733
$limit = $input->getOption('limit');
2834
$offset = $input->getOption('offset');
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Console\Command\Shell;
4+
5+
use PHPCR\Shell\Console\ShellQueryCommand;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
9+
use PHPCR\Shell\Console\Command\AbstractSessionCommand;
10+
11+
class ChangePathCommand extends AbstractSessionCommand
12+
{
13+
protected function configure()
14+
{
15+
$this->setName('cd');
16+
$this->setDescription('Change the current path');
17+
$this->addArgument('path');
18+
}
19+
20+
public function execute(InputInterface $input, OutputInterface $output)
21+
{
22+
$session = $this->getHelper('phpcr')->getSession();
23+
$path = $input->getArgument('path');
24+
$cwd = $this->getApplication()->getCwd();
25+
26+
// absolute path
27+
if (substr($path, 0, 1) == '/') {
28+
$newPath = $path;
29+
} elseif ($path == '..') {
30+
$newPath = dirname($cwd);
31+
} else {
32+
$newPath = sprintf('%s/%s', $cwd, $path);
33+
}
34+
35+
$session->getNode($newPath);
36+
$this->getApplication()->setCwd($newPath);
37+
$output->writeln($newPath);
38+
}
39+
}
40+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Console\Command\Shell;
4+
5+
use PHPCR\Shell\Console\ShellQueryCommand;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
9+
use PHPCR\Shell\Console\Command\AbstractSessionCommand;
10+
11+
class PwdCommand extends AbstractSessionCommand
12+
{
13+
protected function configure()
14+
{
15+
$this->setName('pwd');
16+
$this->setDescription('Print Working Directory (or path)');
17+
}
18+
19+
public function execute(InputInterface $input, OutputInterface $output)
20+
{
21+
$output->writeln($this->getApplication()->getCwd());
22+
}
23+
}
24+
25+

src/PHPCR/Shell/Console/Command/Workspace/NodeDumpCommand.php

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)