Skip to content

Commit 7847990

Browse files
committed
Merge pull request #108 from dantleech/ntregfromfolder
Refactored node type register command to be more flexible re. loading
2 parents b738908 + 1b51944 commit 7847990

File tree

2 files changed

+84
-20
lines changed

2 files changed

+84
-20
lines changed

src/PHPCR/Util/Console/Command/NodeTypeRegisterCommand.php

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @license http://opensource.org/licenses/MIT MIT License
2323
*
2424
* @author Uwe Jäger <[email protected]>
25+
* @author Daniel Leech <[email protected]>
2526
*/
2627
class NodeTypeRegisterCommand extends BaseCommand
2728
{
@@ -33,23 +34,27 @@ protected function configure()
3334
$this
3435
->setName('phpcr:node-type:register')
3536
->setDescription('Register node types in the PHPCR repository')
36-
->setDefinition(array(
37-
new InputArgument(
38-
'cnd-file', InputArgument::REQUIRED, 'Register namespaces and node types from a "Compact Node Type Definition" .cnd file'
39-
),
40-
new InputOption('allow-update', '', InputOption::VALUE_NONE, 'Overwrite existig node type'),
41-
))
37+
->addArgument('cnd-file', InputArgument::IS_ARRAY, 'Register namespaces and node types from a "Compact Node Type Definition" .cnd file(s)')
38+
->addOption('allow-update', null, InputOption::VALUE_NONE, 'Overwrite existig node type')
4239
->setHelp(<<<EOT
4340
Register node types in the PHPCR repository.
4441
4542
This command allows to register node types in the repository that are defined
46-
in a CND (Compact Namespace and Node Type Definition) file as used by jackrabbit.
43+
in a CND (Compact Namespace and Node Type Definition) file as defined in the JCR-283
44+
specification.
4745
4846
Custom node types can be used to define the structure of content repository
4947
nodes, like allowed properties and child nodes together with the namespaces
5048
and their prefix used for the names of node types and properties.
5149
52-
If you use --allow-update existing node type definitions will be overwritten
50+
This command allows you to specify multiple files and/or folders:
51+
52+
$ php app/console phpcr:node-type:register /path/to/nodetype1.cnd /path/to/a/folder
53+
54+
When a folder is specified all files within the folder that have the <comment>.cnd</comment>
55+
extension will be treated as node definition files.
56+
57+
If you use <info>--allow-update</info> existing node type definitions will be overwritten
5358
in the repository.
5459
EOT
5560
)
@@ -61,25 +66,28 @@ protected function configure()
6166
*/
6267
protected function execute(InputInterface $input, OutputInterface $output)
6368
{
64-
$cnd_file = realpath($input->getArgument('cnd-file'));
69+
$definitions = $input->getArgument('cnd-file');
6570

66-
if (!file_exists($cnd_file)) {
67-
throw new \InvalidArgumentException(
68-
sprintf("Node type definition file '<info>%s</info>' does not exist.", $cnd_file)
69-
);
70-
} elseif (!is_readable($cnd_file)) {
71+
if (count($definitions) == 0) {
7172
throw new \InvalidArgumentException(
72-
sprintf("Node type definition file '<info>%s</info>' does not have read permissions.", $cnd_file)
73+
'At least one definition (i.e. file or folder) must be specified'
7374
);
7475
}
7576

76-
$cnd = file_get_contents($cnd_file);
7777
$allowUpdate = $input->getOption('allow-update');
7878
$session = $this->getPhpcrSession();
7979

80-
$this->updateFromCnd($output, $session, $cnd, $allowUpdate);
80+
$filePaths = $this->getFilePaths($definitions);
81+
82+
$count = 0;
83+
foreach ($filePaths as $filePath) {
84+
$cnd = file_get_contents($filePath);
85+
$this->updateFromCnd($output, $session, $cnd, $allowUpdate);
86+
$output->writeln(sprintf('Registered: <info>%s</info>', $filePath));
87+
$count++;
88+
}
8189

82-
$output->write(PHP_EOL.sprintf('Successfully registered node types from "<info>%s</info>"', $cnd_file) . PHP_EOL);
90+
$output->writeln(sprintf('%d node definition file(s)', $count));
8391

8492
return 0;
8593
}
@@ -109,4 +117,60 @@ protected function updateFromCnd(OutputInterface $output, SessionInterface $sess
109117
throw $e;
110118
}
111119
}
120+
121+
/**
122+
* Return a list of node type definition file paths from
123+
* the given definition files or folders.
124+
*
125+
* @param array $definitions List of files of folders
126+
*
127+
* @return array Array of full paths to all the type node definition files.
128+
*/
129+
protected function getFilePaths($definitions)
130+
{
131+
$filePaths = array();
132+
133+
foreach ($definitions as $definition) {
134+
$definition = realpath($definition);
135+
136+
if (is_dir($definition)) {
137+
$dirHandle = opendir($definition);
138+
139+
while ($file = readdir($dirHandle)) {
140+
if (false === $this->fileIsNodeType($file)) {
141+
continue;
142+
}
143+
144+
$filePath = sprintf('%s/%s', $definition, $file);
145+
146+
if (!is_readable($filePath)) {
147+
throw new \InvalidArgumentException(
148+
sprintf("Node type definition file '<info>%s</info>' does not have read permissions.", $file)
149+
);
150+
}
151+
152+
$filePaths[] = $filePath;
153+
}
154+
} else {
155+
if (!file_exists($definition)) {
156+
throw new \InvalidArgumentException(
157+
sprintf("Node type definition file '<info>%s</info>' does not exist.", $definition)
158+
);
159+
}
160+
161+
$filePaths[] = $definition;
162+
}
163+
}
164+
165+
return $filePaths;
166+
}
167+
168+
protected function fileIsNodeType($filename)
169+
{
170+
if (substr($filename, -4) == '.cnd') {
171+
return true;
172+
}
173+
174+
return false;
175+
}
112176
}

tests/PHPCR/Tests/Util/Console/Command/NodeTypeRegisterCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function setUp()
1616
)->disableOriginalConstructor()->getMock();
1717
}
1818

19-
public function testNodeTypeList()
19+
public function testNodeTypeRegister()
2020
{
2121
$this->session->expects($this->once())
2222
->method('getWorkspace')
@@ -28,7 +28,7 @@ public function testNodeTypeList()
2828
->method('registerNodeTypesCnd');
2929

3030
$ct = $this->executeCommand('phpcr:node-type:register', array(
31-
'cnd-file' => __DIR__.'/fixtures/cnd_dummy.cnd',
31+
'cnd-file' => array(__DIR__.'/fixtures/cnd_dummy.cnd'),
3232
));
3333
}
3434
}

0 commit comments

Comments
 (0)