Skip to content

Commit 2c01188

Browse files
committed
Shell is working
1 parent 4dd3f7d commit 2c01188

File tree

9 files changed

+201
-79
lines changed

9 files changed

+201
-79
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,23 @@
66
use PHPCR\SessionInterface;
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Output\OutputInterface;
9-
use PHPCR\Shell\Console\Command\SelectCommand;
109
use PHPCR\Shell\Console\Command\AbstractSessionCommand;
10+
use PHPCR\Shell\Console\Command\Workspace\SelectCommand;
11+
use PHPCR\Shell\Console\Command\Workspace\NodeTypeListCommand;
12+
use PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper;
13+
use PHPCR\Shell\Console\Command\Workspace\NodeDumpCommand;
1114

1215
class ShellApplication extends Application
1316
{
1417
public function __construct(SessionInterface $session)
1518
{
1619
parent::__construct('PHPCR', '1.0');
1720

18-
$this->add(
19-
new SelectCommand()
20-
);
21+
$this->add(new SelectCommand());
22+
$this->add(new NodeTypeListCommand());
23+
$this->add(new NodeDumpCommand());
24+
25+
$this->getHelperSet()->set(new PhpcrConsoleDumperHelper());
2126

2227
foreach ($this->all() as $command) {
2328
if ($command instanceof AbstractSessionCommand) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function configure()
3232
new InputOption('--db_password', '-dp', InputOption::VALUE_OPTIONAL, 'Database Password.'),
3333
new InputOption('--db_host', '-dh', InputOption::VALUE_REQUIRED, 'Database Host.', 'localhost'),
3434
new InputOption('--db_driver', '-dd', InputOption::VALUE_REQUIRED, 'Database Transport.', 'pdo_mysql'),
35+
new InputOption('--db_path', '-dP', InputOption::VALUE_REQUIRED, 'Database Path.'),
3536
));
3637
}
3738

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Console\Command\Workspace;
4+
5+
use PHPCR\ItemNotFoundException;
6+
use PHPCR\RepositoryException;
7+
use PHPCR\PathNotFoundException;
8+
9+
use PHPCR\Util\UUIDHelper;
10+
11+
use Symfony\Component\Console\Input\InputArgument;
12+
use Symfony\Component\Console\Input\InputOption;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
use PHPCR\Shell\Console\Command\AbstractSessionCommand;
16+
17+
/**
18+
* Command subtrees under a path to the console
19+
*
20+
* @license http://www.apache.org/licenses Apache License Version 2.0, January 2004
21+
* @license http://opensource.org/licenses/MIT MIT License
22+
*
23+
* @author Daniel Barsotti <daniel.barsotti@liip.ch>
24+
* @author Daniel Leech <daniel@dantleech.com>
25+
*/
26+
class NodeDumpCommand extends AbstractSessionCommand
27+
{
28+
/**
29+
* {@inheritDoc}
30+
*/
31+
protected function configure()
32+
{
33+
$this
34+
->setName('dump')
35+
->addOption('sys-nodes', null, InputOption::VALUE_NONE, 'Also dump system nodes (recommended to use with a depth limit)')
36+
->addOption('props', null, InputOption::VALUE_NONE, 'Also dump properties of the nodes')
37+
->addOption('identifiers', null, InputOption::VALUE_NONE, 'Also output node UUID')
38+
->addOption('depth', null, InputOption::VALUE_OPTIONAL, 'Limit how many level of children to show', "-1")
39+
->addOption('max_line_length', null, InputOption::VALUE_OPTIONAL, 'Limit the maximum characters per property', "120")
40+
->addOption('ref-format', 'uuid', InputOption::VALUE_REQUIRED, 'Set the way references should be displayed when dumping reference properties - either "uuid" (default) or "path"')
41+
->addArgument('identifier', InputArgument::OPTIONAL, 'Root path to dump', '/')
42+
->setDescription('Dump subtrees of the content repository')
43+
->setHelp(<<<HERE
44+
The <info>dump</info> command recursively outputs the name of the node specified
45+
by the <info>identifier</info> argument and its subnodes in a yaml-like style.
46+
47+
If the <info>props</info> option is used the nodes properties are
48+
displayed as yaml arrays.
49+
50+
By default the command filters out system nodes and properties (i.e. nodes and
51+
properties with names starting with 'jcr:'), the <info>--sys-nodes</info> option
52+
allows to turn this filter off.
53+
HERE
54+
)
55+
;
56+
}
57+
58+
/**
59+
* {@inheritDoc}
60+
*/
61+
protected function execute(InputInterface $input, OutputInterface $output)
62+
{
63+
$session = $this->getSession();
64+
65+
// node to dump
66+
$identifier = $input->getArgument('identifier');
67+
68+
// whether to dump node uuid
69+
$options = array();
70+
$options['dump_uuids'] = $input->hasParameterOption('--identifiers');
71+
$options['ref_format'] = $input->getOption('ref-format');
72+
$options['show_props'] = $input->hasParameterOption('--props');
73+
$options['show_sys_nodes'] = $input->hasParameterOption('--sys-nodes');
74+
$options['max_line_length'] = $input->getOption('max_line_length');
75+
76+
if (null !== $options['ref_format'] && !in_array($options['ref_format'], array('uuid', 'path'))) {
77+
throw new \Exception('The ref-format option must be set to either "path" or "uuid"');
78+
}
79+
80+
$walker = $this->getHelper('phpcr_console_dumper')->getTreeWalker($output, $options);
81+
82+
try {
83+
if (UUIDHelper::isUUID($identifier)) {
84+
$node = $session->getNodeByIdentifier($identifier);
85+
} else {
86+
$node = $session->getNode($identifier);
87+
}
88+
89+
$walker->traverse($node, $input->getOption('depth'));
90+
} catch (RepositoryException $e) {
91+
if ($e instanceof PathNotFoundException || $e instanceof ItemNotFoundException) {
92+
$output->writeln("<error>Path '$identifier' does not exist</error>");
93+
} else {
94+
throw $e;
95+
}
96+
97+
return 1;
98+
}
99+
100+
return 0;
101+
}
102+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Console\Command\Workspace;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use PHPCR\Shell\Console\Command\AbstractSessionCommand;
9+
10+
/**
11+
* A command to list all node types
12+
*
13+
* @license http://www.apache.org/licenses Apache License Version 2.0, January 2004
14+
* @license http://opensource.org/licenses/MIT MIT License
15+
*
16+
* @author Daniel Leech <daniel@dantleech.com>
17+
*/
18+
class NodeTypeListCommand extends AbstractSessionCommand
19+
{
20+
/**
21+
* {@inheritDoc}
22+
*/
23+
protected function configure()
24+
{
25+
$this
26+
->setName('nt-list')
27+
->setDescription('List all available node types in the repository')
28+
->setHelp(<<<EOT
29+
This command lists all of the available node types and their subtypes
30+
in the PHPCR repository.
31+
EOT
32+
)
33+
;
34+
}
35+
36+
/**
37+
* {@inheritDoc}
38+
*/
39+
protected function execute(InputInterface $input, OutputInterface $output)
40+
{
41+
$session = $this->getSession();
42+
$ntm = $session->getWorkspace()->getNodeTypeManager();
43+
44+
$nodeTypes = $ntm->getAllNodeTypes();
45+
46+
foreach ($nodeTypes as $name => $nodeType) {
47+
$output->writeln('<info>'.$name.'</info>');
48+
49+
$superTypes = $nodeType->getSupertypeNames();
50+
if (count($superTypes)) {
51+
$output->writeln(' <comment>Supertypes:</comment>');
52+
foreach ($superTypes as $stName) {
53+
$output->writeln(' <comment>></comment> '.$stName);
54+
}
55+
}
56+
}
57+
58+
return 0;
59+
}
60+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
<?php
22

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

55
use PHPCR\Shell\Console\ShellQueryCommand;
66
use Symfony\Component\Console\Input\InputInterface;
77
use Symfony\Component\Console\Output\OutputInterface;
88
use Symfony\Component\Console\Input\InputOption;
9+
use PHPCR\Shell\Console\Command\AbstractSessionCommand;
910

1011
class SelectCommand extends AbstractSessionCommand
1112
{
1213
protected function configure()
1314
{
1415
$this->setName('select');
16+
$this->setDescription('Execute an JCR_SQL2 query.');
1517
$this->addArgument('query');
1618
$this->addOption('language', 'l', InputOption::VALUE_OPTIONAL, 'The query language (e.g. jcr-sql2', 'jcr-sql2');
1719
$this->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'The query limit', 0);
@@ -20,7 +22,7 @@ protected function configure()
2022

2123
public function execute(InputInterface $input, OutputInterface $output)
2224
{
23-
$sql = $input->getArgument('query');
25+
$sql = $input->getRawCommand();
2426
$language = strtoupper($input->getOption('language'));
2527
$limit = $input->getOption('limit');
2628
$offset = $input->getOption('offset');
@@ -41,6 +43,5 @@ public function execute(InputInterface $input, OutputInterface $output)
4143
$start = microtime(true);
4244
$result = $query->execute();
4345
$elapsed = microtime(true) - $start;
44-
var_dump($result);die();
4546
}
4647
}

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

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

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

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

src/PHPCR/Shell/Console/Input/StringInput.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class StringInput extends BaseInput
1010

1111
public function __construct($command)
1212
{
13-
$this->rawCommand = $command;
13+
$this->rawCommand = trim($command);
1414

1515
parent::__construct($command);
1616
}
@@ -19,4 +19,27 @@ public function getRawCommand()
1919
{
2020
return $this->rawCommand;
2121
}
22+
23+
public function validate()
24+
{
25+
if (false === $this->isQuery()) {
26+
return parent::validate();
27+
}
28+
}
29+
30+
protected function parse()
31+
{
32+
if (false === $this->isQuery()) {
33+
return parent::parse();
34+
}
35+
}
36+
37+
protected function isQuery()
38+
{
39+
if (strpos($this->rawCommand, 'select') === 0) {
40+
return true;
41+
}
42+
43+
return false;
44+
}
2245
}

src/PHPCR/Shell/Transport/DoctrineDbal.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function getRepository()
3333
'host' => $this->input->getOption('db_host'),
3434
'driver' => $this->input->getOption('db_driver'),
3535
'dbname' => $this->input->getOption('db_name'),
36+
'path' => $this->input->getOption('db_path'),
3637
));
3738

3839
$factory = new RepositoryFactoryDoctrineDBAL();

0 commit comments

Comments
 (0)