Skip to content

Commit

Permalink
Merge pull request #67 from dantleech/workspace-node-update
Browse files Browse the repository at this point in the history
[WIP] Workspace Node Update Command and fixes
  • Loading branch information
dbu committed Jun 12, 2013
2 parents 9360d81 + dabbb3a commit c4041d0
Show file tree
Hide file tree
Showing 18 changed files with 584 additions and 95 deletions.
49 changes: 49 additions & 0 deletions src/PHPCR/Util/Console/Command/BaseCommand.php
@@ -0,0 +1,49 @@
<?php

namespace PHPCR\Util\Console\Command;

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

abstract class BaseCommand extends Command
{
/**
* @return PHPCR\SessionInterface
*/
protected function getPhpcrSession()
{
return $this->getHelper('phpcr')->getSession();
}

/**
* @return PHPCR\Util\Console\Helper\PhpcrCliHelper
*/
protected function getPhpcrCliHelper()
{
$phpcrCliHelper = new PhpcrCliHelper($this->getPhpcrSession());
return $phpcrCliHelper;
}

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'
);
}
}
9 changes: 4 additions & 5 deletions src/PHPCR/Util/Console/Command/NodeDumpCommand.php
Expand Up @@ -22,7 +22,6 @@
namespace PHPCR\Util\Console\Command;

use PHPCR\Util\UUIDHelper;
use Symfony\Component\Console\Command\Command;
use PHPCR\ItemNotFoundException;
use PHPCR\RepositoryException;
use PHPCR\PathNotFoundException;
Expand All @@ -42,7 +41,7 @@
* @author Daniel Barsotti <daniel.barsotti@liip.ch>
* @author Daniel Leech <daniel@dantleech.com>
*/
class NodeDumpCommand extends Command
class NodeDumpCommand extends BaseCommand
{
/**
* Limit after which to cut lines when dumping properties
Expand All @@ -65,7 +64,7 @@ protected function configure()
->addOption('ref-format', 'uuid', InputOption::VALUE_REQUIRED, 'Set the way references should be displayed when dumping reference properties - either "uuid" (default) or "path"')
->addArgument('identifier', InputArgument::OPTIONAL, 'Root path to dump', '/')
->setDescription('Dump subtrees of the content repository')
->setHelp(<<<EOF
->setHelp(<<<HERE
The <info>dump</info> command recursively outputs the name of the node specified
by the <info>identifier</info> argument and its subnodes in a yaml-like style.
Expand All @@ -75,7 +74,7 @@ protected function configure()
By default the command filters out system nodes and properties (i.e. nodes and
properties with names starting with 'jcr:'), the <info>sys_nodes</info> option
allows to turn this filter off.
EOF
HERE
)
;
}
Expand All @@ -95,7 +94,7 @@ public function setDumpMaxLineLength($length)
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$session = $this->getHelper('phpcr')->getSession();
$session = $this->getPhpcrSession();
$dumperHelper = $this->getHelper('phpcr_console_dumper');

// node to dump
Expand Down
73 changes: 14 additions & 59 deletions src/PHPCR/Util/Console/Command/NodeTouchCommand.php
Expand Up @@ -36,7 +36,7 @@
*
* @author Daniel Leech <daniel@dantleech.com>
*/
class NodeTouchCommand extends Command
class NodeTouchCommand extends BaseCommand
{
/**
* {@inheritDoc}
Expand All @@ -45,6 +45,8 @@ protected function configure()
{
parent::configure();

$this->configureNodeManipulationInput();

$this->setName('phpcr:node:touch')
->addArgument(
'path',
Expand All @@ -57,26 +59,10 @@ protected function configure()
'Node type, default nt:unstructured',
'nt:unstructured'
)
->addOption('set-prop', 'p',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Set node property, use foo=bar'
)
->addOption('remove-prop', 'r',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Remove node property'
)
->addOption('dump', 'd',
InputOption::VALUE_NONE,
'Dump a string reperesentation of the created / modified node.'
)
->addOption('add-mixin', null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Add a mixin to the node'
)
->addOption('remove-mixin', null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Add a mixin to the node'
)
->setDescription('Create or modify a node')
->setHelp(<<<HERE
This command allows you to create or modify a node at the specified path.
Expand All @@ -102,14 +88,15 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var $session SessionInterface */
$session = $this->getHelper('phpcr')->getSession();
$helper = $this->getPhpcrCliHelper();
$session = $this->getPhpcrSession();

$path = $input->getArgument('path');
$type = $input->getOption('type');
$dump = $input->getOption('dump');

$setProp = $input->getOption('set-prop');
$removeProp = $input->getOption('remove-prop');
$dump = $input->getOption('dump');
$addMixins = $input->getOption('add-mixin');
$removeMixins = $input->getOption('remove-mixin');

Expand Down Expand Up @@ -155,45 +142,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$node = $parentNode->addNode($nodeName, $type);
}

foreach ($setProp as $set) {
$parts = explode('=', $set);
$output->writeln(sprintf(
'<comment> > Setting property </comment>%s<comment> to </comment>%s',
$parts[0], $parts[1]
));
$node->setProperty($parts[0], $parts[1]);
}

foreach ($removeProp as $unset) {
$output->writeln(sprintf(
'<comment> > Unsetting property </comment>%s',
$unset
));
$node->setProperty($unset, null);
}

foreach ($addMixins as $addMixin) {
$node->addMixin($addMixin);
}

foreach ($removeMixins as $removeMixin) {
$node->removeMixin($removeMixin);
}

if ($dump) {
$output->writeln('<info>Node dump: </info>');
/** @var $property PropertyInterface */
foreach ($node->getProperties() as $property) {
$value = $property->getValue();
if (!is_string($value)) {
$value = print_r($value, true);
}
$output->writeln(sprintf('<comment> - %s = </comment>%s',
$property->getName(),
$value
));
}
}
$helper->processNode($output, $node, array(
'setProps' => $setProp,
'removeProps' => $removeProp,
'addMixins' => $addMixins,
'removeMixins' => $removeMixins,
'dump' => $dump,
));

$session->save();
}
Expand Down
4 changes: 2 additions & 2 deletions src/PHPCR/Util/Console/Command/NodeTypeListCommand.php
Expand Up @@ -30,7 +30,7 @@
*
* @author Daniel Leech <daniel@dantleech.com>
*/
class NodeTypeListCommand extends Command
class NodeTypeListCommand extends BaseCommand
{
/**
* {@inheritDoc}
Expand All @@ -53,7 +53,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$session = $this->getHelper('phpcr')->getSession();
$session = $this->getPhpcrSession();
$ntm = $session->getWorkspace()->getNodeTypeManager();

$nodeTypes = $ntm->getAllNodeTypes();
Expand Down
4 changes: 2 additions & 2 deletions src/PHPCR/Util/Console/Command/NodeTypeRegisterCommand.php
Expand Up @@ -39,7 +39,7 @@
*
* @author Uwe Jäger <uwej711@googlemail.com>
*/
class NodeTypeRegisterCommand extends Command
class NodeTypeRegisterCommand extends BaseCommand
{
/**
* {@inheritDoc}
Expand Down Expand Up @@ -91,7 +91,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$cnd = file_get_contents($cnd_file);
$allowUpdate = $input->getOption('allow-update');
$session = $this->getHelper('phpcr')->getSession();
$session = $this->getPhpcrSession();

try {
$this->updateFromCnd($output, $session, $cnd, $allowUpdate);
Expand Down

0 comments on commit c4041d0

Please sign in to comment.