Skip to content

Commit ffe7120

Browse files
committed
Handle binary properties
1 parent 2dd6203 commit ffe7120

File tree

4 files changed

+98
-15
lines changed

4 files changed

+98
-15
lines changed

features/phpcr_node_edit.feature

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,10 @@ Feature: Edit a node
135135
Then the command should not fail
136136
And there should exist a node at "/cms/products/product2"
137137
And the primary type of "/cms/products/product2" should be "nt:resource"
138+
139+
Scenario: Editor returns empty string
140+
Given I have an editor which produces the following:
141+
""""
142+
"""
143+
And I execute the "node:edit cms/products/product2 --no-interaction --type=nt:resource" command
144+
Then the command should fail

src/PHPCR/Shell/Console/Command/Phpcr/NodeEditCommand.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,41 @@ public function execute(InputInterface $input, OutputInterface $output)
5454

5555
// for now we only support YAML
5656
$encoders = array(new YamlEncoder());
57-
$normalizers = array(new NodeNormalizer());
58-
$serializer = new Serializer($normalizers, $encoders);
57+
$nodeNormalizer = new NodeNormalizer();
58+
$serializer = new Serializer(array($nodeNormalizer), $encoders);
5959
$outStr = $serializer->serialize($node, 'yaml');
6060

61-
6261
$tryAgain = false;
6362
$message = '';
63+
$error = '';
64+
$notes = implode("\n", $nodeNormalizer->getNotes());
65+
6466
do {
65-
// string pass to editor
66-
if ($message) {
67+
$message = '';
68+
if ($error) {
6769
$template = <<<EOT
6870
Error encounred:
71+
%s
72+
73+
EOT
74+
;
75+
$message .= sprintf($template, $error);
76+
}
6977

78+
if ($notes) {
79+
$template = <<<EOT
80+
NOTE:
7081
%s
82+
7183
EOT
7284
;
85+
$message .= sprintf($template, $notes);
86+
}
87+
88+
// string pass to editor
89+
if ($message) {
7390

74-
$inStr = $editor->fromStringWithMessage($outStr, sprintf($template, $message), '# ', 'yml');
91+
$inStr = $editor->fromStringWithMessage($outStr, $message, '# ', 'yml');
7592
} else {
7693
$inStr = $editor->fromString($outStr, 'yml');
7794
}
@@ -82,15 +99,19 @@ public function execute(InputInterface $input, OutputInterface $output)
8299
));
83100
$tryAgain = false;
84101
} catch (\Exception $e) {
85-
$message = $e->getMessage();
86-
$output->writeln('<error>' . $message . '</error>');
102+
$error = $e->getMessage();
103+
$output->writeln('<error>' . $error . '</error>');
87104
if (false === $input->getOption('no-interaction')) {
88105
$tryAgain = $dialog->askConfirmation($output, 'Do you want to try again? (y/n)');
89106
}
90107
$outStr = $inStr;
91108
}
92109
} while ($tryAgain == true);
93110

111+
if ($error) {
112+
return 1;
113+
}
114+
94115
return 0;
95116
}
96117
}

src/PHPCR/Shell/Resources/config.dist/alias.yml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Shell shortcuts
2-
lsa: shell:alias:list
2+
aliases: shell:alias:list
33
creload: shell:config:reload
44
cinit: shell:config:init
55

@@ -20,32 +20,38 @@ ln: node:clone {arg1} {arg2} # symlink, as in ln -s
2020
cp: node:copy {arg1} {arg2}
2121
cat: node:property:show {arg1}
2222
touch: node:property:set {arg1} {arg2} {arg3}
23+
mkdir: node:create {arg1} {arg2}
2324

2425
# Node type commands
25-
lsmix: node-type:list "^mix:"
26-
lsnt: node-type:list {arg1}
26+
mixins: node-type:list "^mix:"
27+
nodetypes: node-type:list {arg1}
2728
ntedit: node-type:edit {arg1}
2829
ntshow: node-type:show {arg1}
2930

3031
# Workspace commands
31-
wslist: workspace:list
32+
workspaces: workspace:list
3233

3334
# Namespsce commands
34-
lsns: workspace:namespace:list {arg1}
35+
namespaces: workspace:namespace:list {arg1}
3536
nsset: workspace:namespace:register
3637

3738
# Editor commands
38-
vim: node:property:edit {arg1} {arg2}
39-
nano: node:property:edit {arg1} {arg2}
39+
vi: node:edit {arg1} {arg2}
40+
vim: node:edit {arg1} {arg2}
41+
nano: node:edit {arg1} {arg2}
4042

4143
# GNU commands
4244
man: help {arg1}
4345

4446
# Version commands
47+
checkin: version:checkin {arg1}
4548
ci: version:checkin {arg1}
4649
co: version:checkout {arg1}
50+
checkout: version:checkout {arg1}
4751
cp: version:checkpoint {arg1}
52+
checkpoint: version:checkpoint {arg1}
4853
vhist: version:history {arg1}
54+
versions: version:history {arg1}
4955

5056
# Session commands
5157
save: session:save

src/PHPCR/Shell/Serializer/NodeNormalizer.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPCR\PropertyType;
88
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
99
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
10+
use PHPCR\PropertyInterface;
1011

1112
/**
1213
* Normalizer for PHPCR Nodes
@@ -15,6 +16,19 @@
1516
*/
1617
class NodeNormalizer implements NormalizerInterface, DenormalizerInterface
1718
{
19+
protected $allowBinary;
20+
protected $notes = array();
21+
22+
public function __construct($allowBinary = false)
23+
{
24+
$this->allowBinary = $allowBinary;
25+
}
26+
27+
public function getNotes()
28+
{
29+
return $this->notes;
30+
}
31+
1832
/**
1933
* {@inheritDoc}
2034
*/
@@ -23,6 +37,10 @@ public function normalize($node, $format = null, array $context = array())
2337
$res = array();
2438

2539
foreach ($node->getProperties() as $property) {
40+
if (false === $this->isPropertyEditable($property)) {
41+
continue;
42+
}
43+
2644
$propertyType = $property->getType();
2745
$propertyValue = $property->getValue();
2846
$propertyName = $property->getName();
@@ -49,6 +67,12 @@ public function supportsNormalization($data, $format = null)
4967
*/
5068
public function denormalize($data, $class, $format = null, array $context = array())
5169
{
70+
if (!$data) {
71+
throw new \InvalidArgumentException(
72+
'Editor returned nothing .. nodes must have at least one property (i.e. the jcr:primaryType property)'
73+
);
74+
}
75+
5276
if (!isset($context['node'])) {
5377
throw new \InvalidArgumentException(sprintf(
5478
'You must provide the PHPCR node instance to update in the context using the "node" key.'
@@ -61,7 +85,12 @@ public function denormalize($data, $class, $format = null, array $context = arra
6185

6286
// Update / remove existing properties
6387
foreach ($node->getProperties() as $property) {
88+
if (false === $this->isPropertyEditable($property)) {
89+
continue;
90+
}
91+
6492
try {
93+
6594
if (!isset($data[$property->getName()])) {
6695
$property->remove();
6796
continue;
@@ -139,4 +168,24 @@ private function normalizeDatum($value)
139168

140169
return $value;
141170
}
171+
172+
/**
173+
* Return false if property type is not editable
174+
*
175+
* (e.g. property type is binary)
176+
*
177+
* @return boolean
178+
*/
179+
private function isPropertyEditable(PropertyInterface $property)
180+
{
181+
// do not serialize binary objects
182+
if (false === $this->allowBinary && PropertyType::BINARY == $property->getType()) {
183+
$this->notes[] = sprintf(
184+
'Binary property "%s" has been omitted', $property->getName()
185+
);
186+
return false;
187+
}
188+
189+
return true;
190+
}
142191
}

0 commit comments

Comments
 (0)