Skip to content

[WIP] Support for YAML NT edition #31

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

Open
wants to merge 1 commit into
base: 1.x
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
"jackalope/jackalope": "dev-master",
"phpcr/phpcr": "dev-master",
"phpcr/phpcr-utils": "dev-master",
"dantleech/phpcr-nodetype-serializer": "dev-master",
"symfony/finder": "~2.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
"require-dev": {
"mockery/mockery": "0.9",
"symfony/process": "2.3.*",
Expand Down
11 changes: 2 additions & 9 deletions features/phpcr_node_type_show.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@ Feature: Show a node type
Then the command should not fail
And I should see the following:
"""
[nt:unstructured] > nt:base
orderable query
- *
multiple jcr.operator.equal.to', 'jcr.operator.not.equal.to', 'jcr.operator.greater.than', 'jcr.operator.greater.than.or.equal.to', 'jcr.operator.less.than', 'jcr.operator.less.than.or.equal.to', 'jcr.operator.like
- *
jcr.operator.equal.to', 'jcr.operator.not.equal.to', 'jcr.operator.greater.than', 'jcr.operator.greater.than.or.equal.to', 'jcr.operator.less.than', 'jcr.operator.less.than.or.equal.to', 'jcr.operator.like
+ * (nt:base)
= nt:unstructured
VERSION sns
name: 'nt:unstructured'
declared_supertypes:
"""

Scenario: Execute the note-type show command
Expand Down
9 changes: 9 additions & 0 deletions features/shell_connect.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: Launch a new shell session
In order to administer a PHPCR repository
As a user
I need to be able to launch the PHPCRSH

Scenario: Connect
Given I execute the "shell:config:reload" command
Then the command should not fail

38 changes: 21 additions & 17 deletions src/PHPCR/Shell/Console/Command/Phpcr/NodeTypeEditCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use PHPCR\Util\CND\Writer\CndWriter;
use PHPCR\NodeType\NoSuchNodeTypeException;
use PHPCR\Util\CND\Parser\CndParser;
use PHPCR\Util\NodeType\Serializer\YAMLSerializer;
use PHPCR\Util\NodeType\Serializer\YAMLDeserializer;

class NodeTypeEditCommand extends Command
{
Expand Down Expand Up @@ -40,10 +40,10 @@ public function execute(InputInterface $input, OutputInterface $output)
$namespaceRegistry = $workspace->getNamespaceRegistry();
$nodeTypeManager = $workspace->getNodeTypeManager();

$serializer = new YAMLSerializer();

try {
$nodeType = $nodeTypeManager->getNodeType($nodeTypeName);
$cndWriter = new CndWriter($namespaceRegistry);
$out = $cndWriter->writeString(array($nodeType));
$message = null;
} catch (NoSuchNodeTypeException $e) {
$parts = explode(':', $nodeTypeName);
Expand All @@ -55,24 +55,31 @@ public function execute(InputInterface $input, OutputInterface $output)
}
list($namespace, $name) = $parts;
$uri = $session->getNamespaceURI($namespace);
$nodeType = $nodeTypeManager->createNodeTypeTemplate();
$propertyTemplate = $nodeTypeManager->createPropertyDefinitionTemplate();
$propertyTemplate->setName($namespace . ':change-me');
$childTemplate = $nodeTypeManager->createNodeDefinitionTemplate();
$childTemplate->setName($namespace . ':change-me');

// so we will create one ..
$out = <<<EOT
<$namespace ='$uri'>
[$namespace:$name] > nt:unstructured
$nodeType->setName($nodeTypeName);
$nodeType->getNodeDefinitionTemplates()->append($childTemplate);
$nodeType->getPropertyDefinitionTemplates()->append($propertyTemplate);

EOT
;
// so we will create one ..
$message = <<<EOT
Creating a new node type: $nodeTypeName

An example property and an example node have been added. Feel free to delete or change them.
EOT
;
}

$out = $serializer->serialize($nodeType);

$valid = false;
$prefix = '# ';
do {
$res = $editor->fromStringWithMessage($out, $message);
$res = $editor->fromStringWithMessage($out, $message, $prefix, '.yml');

if (empty($res)) {
$output->writeln('<info>Editor emptied the CND file, doing nothing. Use node-type:delete to remove node types.</info>');
Expand All @@ -81,12 +88,9 @@ public function execute(InputInterface $input, OutputInterface $output)
}

try {
$cndParser = new CndParser($nodeTypeManager);
$namespacesAndNodeTypes = $cndParser->parseString($res);

foreach ($namespacesAndNodeTypes['nodeTypes'] as $nodeType) {
$nodeTypeManager->registerNodeType($nodeType, true);
}
$serializer = new YAMLDeserializer($session);
$nodeType = $serializer->deserialize($res);
$nodeTypeManager->registerNodeType($nodeType, true);
$valid = true;
} catch (\Exception $e) {
$output->writeln('<error>'.$e->getMessage().'</error>');
Expand Down
10 changes: 5 additions & 5 deletions src/PHPCR/Shell/Console/Command/Phpcr/NodeTypeShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use PHPCR\Util\CND\Writer\CndWriter;
use PHPCR\NodeType\NoSuchNodeTypeException;
use PHPCR\Util\NodeType\Serializer\YAMLSerializer;

class NodeTypeShowCommand extends Command
{
protected function configure()
{
$this->setName('node-type:show');
$this->setDescription('Show the CND of a node type');
$this->setDescription('Show the node definition of a given node type');
$this->addArgument('nodeTypeName', null, InputArgument::REQUIRED, 'The name of the node type to show');
$this->setHelp(<<<HERE
Show the CND (Compact Node Definition) of a given node type.
Show the node type definition in YAML
HERE
);
}
Expand All @@ -37,8 +37,8 @@ public function execute(InputInterface $input, OutputInterface $output)
'The node type "%s" does not exist'
, $nodeTypeName));
}
$cndWriter = new CndWriter($namespaceRegistry);
$out = $cndWriter->writeString(array($nodeType));
$cndWriter = new YAMLSerializer();
$out = $cndWriter->serialize($nodeType);
$output->writeln(sprintf('<comment>%s</comment>', $out));
}
}
12 changes: 9 additions & 3 deletions src/PHPCR/Shell/Console/Helper/EditorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class EditorHelper extends Helper
* file containing the given string value.
*
* @param string $string
* @param string $extension Optional extension for filetype hinting (e.g. .yml)
*
* @return string
*/
public function fromString($string)
public function fromString($string, $extension = null)
{
$fs = new Filesystem();
$dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'phpcr-shell';
Expand All @@ -30,6 +31,11 @@ public function fromString($string)
}

$tmpName = tempnam($dir, '');

if (null !== $extension) {
$tmpName .= $extension;
}

file_put_contents($tmpName, $string);
$editor = getenv('EDITOR');

Expand All @@ -45,7 +51,7 @@ public function fromString($string)
return $contents;
}

public function fromStringWithMessage($string, $message, $messagePrefix = '# ')
public function fromStringWithMessage($string, $message, $messagePrefix = '# ', $extension = null)
{
if (null !== $message) {
$message = explode("\n", $message);
Expand All @@ -60,7 +66,7 @@ public function fromStringWithMessage($string, $message, $messagePrefix = '# ')

$source .= $string;

$res = $this->fromString($source);
$res = $this->fromString($source, $extension);
$res = explode("\n", $res);

$line = current($res);
Expand Down