Skip to content

Commit 06aa589

Browse files
committed
Improved result set formatting
1 parent d4f140f commit 06aa589

File tree

6 files changed

+54
-22
lines changed

6 files changed

+54
-22
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ Shell for PHPCR
77

88
To connect to a doctrine-dbal PHPCR repository:
99

10-
$ php bin/phpcr-shell --db_name=foobar --db_username=user --db_password=foobar
10+
$ php bin/phpcr --db-name=foobar --db-username=user --db-password=foobar
1111

1212
Full definition:
1313

1414
````bash
15-
./bin/phpcrsh --help
15+
./bin/phpcr --help
1616
Usage:
1717
phpcr_shell [-h|--help] [-v|--verbose] [-V|--version] [--ansi] [--no-ansi] [-t|--transport="..."] [-pu|--phpcr-username="..."] [-pp|--phpcr-password[="..."]] [-pw|--phpcr-workspace[="..."]] [-du|--db-username="..."] [-dn|--db-name="..."] [-dp|--db-password[="..."]] [-dh|--db-host="..."] [-dd|--db-driver="..."] [-dP|--db-path="..."] [-url|--repo-url="..."]
1818

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
use PHPCR\Shell\Console\Command\ShellCommand;
1717
use PHPCR\Shell\Console\Input\ArgvInput;
1818

19+
/**
20+
* This application wraps a single command which accepts
21+
* the connection parameters and starts an interactive shell.
22+
*
23+
* @author Daniel Leech <daniel@dantleech.com>
24+
*/
1925
class SessionApplication extends BaseApplication
2026
{
2127
public function __construct()
2228
{
23-
parent::__construct('PHPCRSH', '0.1');
29+
parent::__construct('PHPCR', '1.0');
2430

2531
$command = new ShellCommand();
2632
$command->setApplication($this);
@@ -32,11 +38,6 @@ public function getDefaultInputDefinition()
3238
return new InputDefinition(array());
3339
}
3440

35-
public function run()
36-
{
37-
parent::run();
38-
}
39-
4041
protected function getCommandName($input)
4142
{
4243
return 'phpcr_shell';

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
use Symfony\Component\Console\Shell as BaseShell;
1010
use PHPCR\Shell\Console\Input\StringInput;
1111

12+
/**
13+
* This is more or less a copy of the Symfony\Component\Shell
14+
*
15+
* @author Daniel Leech
16+
*/
1217
class Shell
1318
{
1419
private $application;
@@ -66,10 +71,6 @@ public function run()
6671
}
6772

6873
$ret = $this->application->run(new StringInput($command), $this->output);
69-
70-
if (0 !== $ret) {
71-
$this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));
72-
}
7374
}
7475
}
7576

@@ -87,7 +88,7 @@ protected function getHeader()
8788
At the prompt, type <comment>help</comment> for some help,
8889
or <comment>list</comment> to get a list of available commands.
8990
90-
To exit the shell, type <comment>^D</comment>.
91+
To exit the shell, type <comment>exit</comment>.
9192
9293
EOF;
9394
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class ShellApplication extends Application
4141
protected $transports;
4242
protected $credentials;
4343

44-
public function __construct(InputInterface $input, $transports = array())
44+
public function __construct($appName, $version, InputInterface $input, $transports = array())
4545
{
46-
parent::__construct('PHPCRSH', '0.1');
46+
parent::__construct($appName, $version);
4747

4848
// initialize transports
4949
foreach (array_merge(array(
@@ -162,4 +162,11 @@ public function doRun(InputInterface $input, OutputInterface $output)
162162

163163
return parent::doRun($input, $output);
164164
}
165+
166+
public function renderException($e, $output)
167+
{
168+
do {
169+
$output->writeln(sprintf('<fg=red>%s</fg=red>', $e->getMessage()));
170+
} while ($e = $e->getPrevious());
171+
}
165172
}

src/PHPCR/Shell/Console/Command/ShellCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ public function configure()
4040

4141
public function execute(InputInterface $input, OutputInterface $output)
4242
{
43-
$application = new Shell(new ShellApplication($input));
43+
$application = new Shell(new ShellApplication(
44+
$this->getApplication()->getName(),
45+
$this->getApplication()->getVersion(),
46+
$input
47+
));
4448
$application->run($input, $output);
4549
}
4650
}

src/PHPCR/Shell/Console/Helper/ResultFormatterHelper.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPCR\Query\QueryResultInterface;
77
use Symfony\Component\Console\Output\OutputInterface;
88
use PHPCR\PropertyType;
9+
use PHPCR\NodeInterface;
910

1011
class ResultFormatterHelper extends Helper
1112
{
@@ -14,19 +15,22 @@ public function getName()
1415
return 'result_formatter';
1516
}
1617

17-
public function format(QueryResultInterface $result, OutputInterface $output)
18+
public function format(QueryResultInterface $result, OutputInterface $output, $elapsed)
1819
{
1920
$selectorNames = $result->getSelectorNames();
21+
2022
foreach ($result->getRows() as $i => $row) {
21-
$output->writeln($i);
23+
$output->writeln(sprintf('%s ----', $i + 1));
2224
foreach ($selectorNames as $selectorName) {
2325
$node = $row->getNode($selectorName);
2426
$properties = $node->getProperties();
25-
$output->writeln(' ' . $selectorName);
26-
$output->writeln(' <comment>' . $node->getPath().'</comment>');
27+
$output->writeln(sprintf(
28+
' [%s] [path:<comment>%s</comment>] [uid:<info>%s</info>]',
29+
$selectorName, $node->getPath(), $node->getIdentifier()
30+
));
2731

2832
foreach ($properties as $key => $value) {
29-
$output->writeln(sprintf(' <info>%s</info> %s%s: %s',
33+
$output->writeln(sprintf(' <info>%s</info> <fg=magenta>%s%s</fg=magenta>: %s',
3034
$key,
3135
PropertyType::nameFromValue($value->getType()),
3236
$value->isMultiple() ? '[]' : '',
@@ -35,6 +39,9 @@ public function format(QueryResultInterface $result, OutputInterface $output)
3539
}
3640
}
3741
}
42+
43+
$output->writeln('');
44+
$output->writeln(sprintf('%s rows in set (%s sec)', count($result->getRows()), number_format($elapsed, 2)));
3845
}
3946

4047
protected function formatValue($value)
@@ -43,8 +50,20 @@ protected function formatValue($value)
4350
if (empty($value->getValue())) {
4451
return '';
4552
}
53+
$array = $value;
54+
$values = array();
55+
56+
foreach ($array->getValue() as $value) {
57+
if ($value instanceof NodeInterface) {
58+
$values[] = $value->getPath();
59+
} else if (is_object($value)) {
60+
$values[] = '<UNKNOWN OBJECT>';
61+
} else {
62+
$values[] = $value;
63+
}
64+
}
4665

47-
return "\n - " . implode("\n - ", $value->getvalue());
66+
return "\n - " . implode("\n - ", $values);
4867
}
4968

5069
switch (intval($value->getType())) {

0 commit comments

Comments
 (0)