Skip to content

properly use the cli helper #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions cli-config.php.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php

// adjust as needed
/**
* This is a sample bootstrap file for the console. All jackalope variants
* provide their own bootstrap file. This file is only relevant for people
* implementing PHPCR and using the phpcr-utils.
*/

$path_to_jackalope = __DIR__.'/../jackalope';

// $autoload was created in the autoload that is included before this.
Expand All @@ -12,12 +17,19 @@ $workspace = 'default';
$user = 'admin';
$pass = 'admin';

/* bootstrapping the repository implementation. for jackalope with jackrabbit, do this: */
$factory = new \Jackalope\RepositoryFactoryJackrabbit;
$repository = $factory->getRepository(array("jackalope.jackrabbit_uri" => $jackrabbit_url));
$credentials = new \PHPCR\SimpleCredentials($user, $pass);
$session = $repository->login($credentials, $workspace);
if (isset($argv[1])
&& $argv[1] != 'list'
&& $argv[1] != 'help'
) {
/* bootstrapping the repository implementation. for jackalope with jackrabbit, do this: */
$factory = new \Jackalope\RepositoryFactoryJackrabbit;
$repository = $factory->getRepository(array("jackalope.jackrabbit_uri" => $jackrabbit_url));
$credentials = new \PHPCR\SimpleCredentials($user, $pass);
$session = $repository->login($credentials, $workspace);

$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'session' => new \PHPCR\Util\Console\Helper\PhpcrHelper($session)
));
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'dialog' => new \Symfony\Component\Console\Helper\DialogHelper(),
'phpcr' => new \PHPCR\Util\Console\Helper\PhpcrHelper($session),
'phpcr_console_dumper' => new \PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper(),
));
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
"dev-master": "1.1-dev"
}
}
}
2 changes: 1 addition & 1 deletion src/PHPCR/Util/CND/Parser/CndParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @license http://opensource.org/licenses/MIT MIT License
*
* @author Daniel Barsotti <[email protected]>
* @author David Buchmann <david@liip.ch>
* @author David Buchmann <mail@davidbu.ch>
*/
class CndParser extends AbstractParser
{
Expand Down
2 changes: 1 addition & 1 deletion src/PHPCR/Util/CND/Writer/CndWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @license http://www.apache.org/licenses Apache License Version 2.0, January 2004
* @license http://opensource.org/licenses/MIT MIT License
*
* @author David Buchmann <david@liip.ch>
* @author David Buchmann <mail@davidbu.ch>
*/
class CndWriter
{
Expand Down
62 changes: 10 additions & 52 deletions src/PHPCR/Util/Console/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace PHPCR\Util\Console\Command;

use PHPCR\SessionInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use PHPCR\Util\Console\Helper\PhpcrCliHelper;
use Symfony\Component\Console\Output\OutputInterface;

use PHPCR\SessionInterface;
use PHPCR\Util\Console\Helper\PhpcrHelper;
use PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper;

/**
Expand All @@ -24,67 +27,22 @@ abstract class BaseCommand extends Command
*/
protected function getPhpcrSession()
{
return $this->getHelper('phpcr')->getSession();
return $this->getPhpcrHelper()->getSession();
}

/**
* @return PhpcrCliHelper
* @return PhpcrHelper
*/
protected function getPhpcrCliHelper()
protected function getPhpcrHelper()
{
if (!$this->phpcrCliHelper) {
$this->phpcrCliHelper = new PhpcrCliHelper($this->getPhpcrSession());
}

return $this->phpcrCliHelper;
return $this->getHelperSet()->get('phpcr');
}

/**
* @return PhpcrConsoleDumperHelper
*/
protected function getPhpcrConsoleDumperHelper()
{
if (!$this->phpcrConsoleDumperHelper) {
$this->phpcrConsoleDumperHelper = new PhpcrConsoleDumperHelper();
}

return $this->phpcrConsoleDumperHelper;
}

public function setPhpcrConsoleDumperHelper($consoleDumperHelper)
{
$this->phpcrConsoleDumperHelper = $consoleDumperHelper;
}

/**
* Hack to enable overriding for unit tests.
*/
public function setPhpcrCliHelper(PhpcrCliHelper $helper)
{
$this->phpcrCliHelper = $helper;
}

public function configureNodeManipulationInput()
{
$this->addOption('set-prop', 'p',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Set node property on nodes use foo=bar'
);
$this->addOption('remove-prop', 'r',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Remove property from nodes'
);
$this->addOption('add-mixin', null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Add a mixin to the nodes'
);
$this->addOption('remove-mixin', null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Remove mixin from the nodes'
);
$this->addOption('apply-closure', null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Apply a closure to each node, closures are passed PHPCR\Session and PHPCR\NodeInterface'
);
return $this->getHelperSet()->get('phpcr_console_dumper');
}
}
41 changes: 41 additions & 0 deletions src/PHPCR/Util/Console/Command/BaseNodeManipulationCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace PHPCR\Util\Console\Command;

use Symfony\Component\Console\Input\InputOption;

/**
* Base command for node manipulations, providing common setup.
*
* @license http://www.apache.org/licenses Apache License Version 2.0, January 2004
* @license http://opensource.org/licenses/MIT MIT License
*/
abstract class BaseNodeManipulationCommand extends BaseCommand
{
/**
* Set up the options to manipulate nodes.
*/
protected function configureNodeManipulationInput()
{
$this->addOption('set-prop', 'p',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Set node property on nodes use foo=bar'
);
$this->addOption('remove-prop', 'r',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Remove property from nodes'
);
$this->addOption('add-mixin', null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Add a mixin to the nodes'
);
$this->addOption('remove-mixin', null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Remove mixin from the nodes'
);
$this->addOption('apply-closure', null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Apply a closure to each node, closures are passed PHPCR\Session and PHPCR\NodeInterface'
);
}
}
6 changes: 4 additions & 2 deletions src/PHPCR/Util/Console/Command/NodeMoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @author Daniel Leech <[email protected]>
*/
class NodeMoveCommand extends Command
class NodeMoveCommand extends BaseCommand
{
/**
* {@inheritDoc}
Expand Down Expand Up @@ -45,7 +45,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$session = $this->getHelper('phpcr')->getSession();
$session = $this->getPhpcrSession();

$sourcePath = $input->getArgument('source');
$destPath = $input->getArgument('destination');
Expand All @@ -57,6 +57,8 @@ protected function execute(InputInterface $input, OutputInterface $output)

$session->move($sourcePath, $destPath);
$session->save();

return 0;
}

}
8 changes: 4 additions & 4 deletions src/PHPCR/Util/Console/Command/NodeRemoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @author Daniel Barsotti <[email protected]>
* @author Daniel Leech <[email protected]>
*/
class NodeRemoveCommand extends Command
class NodeRemoveCommand extends BaseCommand
{
/**
* {@inheritDoc}
Expand Down Expand Up @@ -57,8 +57,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var $session SessionInterface*/
$session = $this->getHelper('phpcr')->getSession();
$session = $this->getPhpcrSession();

$path = $input->getArgument('path');
$force = $input->getOption('force');
Expand All @@ -74,7 +73,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

if (!$force) {
$dialog = new DialogHelper();
/** @var $dialog DialogHelper */
$dialog = $this->getHelperSet()->get('dialog');
$workspaceName = $session->getWorkspace()->getName();

if ($onlyChildren) {
Expand Down
15 changes: 9 additions & 6 deletions src/PHPCR/Util/Console/Command/NodeTouchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author Daniel Leech <[email protected]>
*/
class NodeTouchCommand extends BaseCommand
class NodeTouchCommand extends BaseNodeManipulationCommand
{
/**
* {@inheritDoc}
Expand Down Expand Up @@ -71,7 +71,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$helper = $this->getPhpcrCliHelper();
$helper = $this->getPhpcrHelper();
$session = $this->getPhpcrSession();

$path = $input->getArgument('path');
Expand All @@ -98,13 +98,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
));

if ($nodeType != $type) {
throw new \Exception(sprintf(
'You have specified node type "%s" but the existing node is of type "%s"',
$output->writeln(sprintf(
'<error>You have specified node type "%s" but the existing node is of type "%s"</error>',
$type, $nodeType
));

return 1;
}
} else {

$nodeName = PathHelper::getNodeName($path);
$parentPath = PathHelper::getParentPath($path);

Expand All @@ -116,7 +117,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$parentPath
));

return;
return 2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity - why 2?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a non 0 return code of a linux binary means an error state. i just counted up the return statements so a calling programm could distinguish the states if it cares.

}

$output->writeln(sprintf(
Expand All @@ -135,5 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
));

$session->save();

return 0;
}
}
11 changes: 5 additions & 6 deletions src/PHPCR/Util/Console/Command/NodesUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\DialogHelper;
use PHPCR\Util\Console\Command\BaseCommand;

/**
* Command which can update the properties of nodes found
Expand All @@ -17,7 +16,7 @@
*
* @author Daniel Leech <[email protected]>
*/
class NodesUpdateCommand extends BaseCommand
class NodesUpdateCommand extends BaseNodeManipulationCommand
{
/**
* {@inheritDoc}
Expand Down Expand Up @@ -82,8 +81,6 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->dialog = new DialogHelper();

$query = $input->getOption('query');
$queryLanguage = strtoupper($input->getOption('query-language'));
$persistCounter = intval($input->getOption('persist-counter'));
Expand All @@ -93,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$removeMixins = $input->getOption('remove-mixin');
$applyClosures = $input->getOption('apply-closure');
$noInteraction = $input->getOption('no-interaction');
$helper = $this->getPhpcrCliHelper();
$helper = $this->getPhpcrHelper();
$session = $this->getPhpcrSession();

if (!$query) {
Expand Down Expand Up @@ -154,7 +151,9 @@ protected function execute(InputInterface $input, OutputInterface $output)

protected function getAction($output, $result)
{
$response = strtoupper($this->dialog->ask($output, sprintf(
/** @var $dialog DialogHelper */
$dialog = $this->getHelperSet()->get('dialog');
$response = strtoupper($dialog->ask($output, sprintf(
'<question>About to update %d nodes. Enter "Y" to continue, "N" to cancel or "L" to list.</question>',
count($result->getRows())
), false));
Expand Down
17 changes: 12 additions & 5 deletions src/PHPCR/Util/Console/Command/WorkspaceCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
* @license http://opensource.org/licenses/MIT MIT License
*
* @author Lukas Kahwe Smith <[email protected]>
* @author David Buchmann <david@liip.ch>
* @author David Buchmann <mail@davidbu.ch>
*/
class WorkspaceCreateCommand extends Command
class WorkspaceCreateCommand extends BaseCommand
{
/**
* {@inheritDoc}
Expand All @@ -43,8 +43,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var $session SessionInterface */
$session = $this->getHelper('phpcr')->getSession();
$session = $this->getPhpcrSession();

$workspaceName = $input->getArgument('name');

Expand All @@ -61,9 +60,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

if (array_search($workspaceName, $workspace->getAccessibleWorkspaceNames())) {
$output->writeln(
sprintf('<comment>This repository already has a workspace called "%s"</comment>', $workspaceName)
);

return 2;
}

$workspace->createWorkspace($workspaceName);

$output->writeln("Created workspace '$workspaceName'.");
$output->writeln(sprintf('<info>Created workspace "%s".</info>', $workspaceName));

return 0;
}
Expand Down
Loading