Skip to content
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
100 changes: 82 additions & 18 deletions src/PHPCR/Util/Console/Command/NodeTypeRegisterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @license http://opensource.org/licenses/MIT MIT License
*
* @author Uwe Jäger <uwej711@googlemail.com>
* @author Daniel Leech <daniel@dantleech.com>
*/
class NodeTypeRegisterCommand extends BaseCommand
{
Expand All @@ -33,23 +34,27 @@ protected function configure()
$this
->setName('phpcr:node-type:register')
->setDescription('Register node types in the PHPCR repository')
->setDefinition(array(
new InputArgument(
'cnd-file', InputArgument::REQUIRED, 'Register namespaces and node types from a "Compact Node Type Definition" .cnd file'
),
new InputOption('allow-update', '', InputOption::VALUE_NONE, 'Overwrite existig node type'),
))
->addArgument('cnd-file', InputArgument::IS_ARRAY, 'Register namespaces and node types from a "Compact Node Type Definition" .cnd file(s)')
->addOption('allow-update', null, InputOption::VALUE_NONE, 'Overwrite existig node type')
Copy link
Member

Choose a reason for hiding this comment

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

imo its not an option to BC break this command, its used in too many places. i think we still should alternatively accept a filename as argument. i guess nothing can then be required anymore but the check if we have something would be in the command itself.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok, do you think it better to deprecate the first argument in favor of multiple arguments? And eventually do a BC break on the next major version?

Copy link
Member

Choose a reason for hiding this comment

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

we could also keep using the argument instead of an option, then you could just specify one or several things after the command name that are file or folder paths. then only the naming of the argument for use inside the command is a bit off.

Copy link
Member Author

Choose a reason for hiding this comment

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

hmm, but how can you specify multiple paths with an argument?

Copy link
Member

Choose a reason for hiding this comment

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

can't you simply repeat the argument when its an array argument?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh yeah, didn't know you could do that. Maybe thats the way to do it.

Copy link
Member

Choose a reason for hiding this comment

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

+1

->setHelp(<<<EOT
Register node types in the PHPCR repository.

This command allows to register node types in the repository that are defined
in a CND (Compact Namespace and Node Type Definition) file as used by jackrabbit.
in a CND (Compact Namespace and Node Type Definition) file as defined in the JCR-283
Copy link
Member

Choose a reason for hiding this comment

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

i was going to say this is jackrabbit specific but then i found http://www.day.com/maven/jcr/2.0/25_Appendix.html#25.2%20Compact%20Node%20Type%20Definition%20Notation which indicates its now somehow part of jcr. did not find it in the chapter on node type management...

specification.
Copy link
Member

Choose a reason for hiding this comment

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

i think we should say here that it can also be folders and that you can specify several files / folders. (btw, is it a deep recursion or just the folder itself?) folder itself is fine, but we should say it.


Custom node types can be used to define the structure of content repository
nodes, like allowed properties and child nodes together with the namespaces
and their prefix used for the names of node types and properties.

If you use --allow-update existing node type definitions will be overwritten
This command allows you to specify multiple files and/or folders:

$ php app/console phpcr:node-type:register /path/to/nodetype1.cnd /path/to/a/folder

When a folder is specified all files within the folder that have the <comment>.cnd</comment>
extension will be treated as node definition files.

If you use <info>--allow-update</info> existing node type definitions will be overwritten
in the repository.
EOT
)
Expand All @@ -61,25 +66,28 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$cnd_file = realpath($input->getArgument('cnd-file'));
$definitions = $input->getArgument('cnd-file');

if (!file_exists($cnd_file)) {
throw new \InvalidArgumentException(
sprintf("Node type definition file '<info>%s</info>' does not exist.", $cnd_file)
);
} elseif (!is_readable($cnd_file)) {
if (count($definitions) == 0) {
throw new \InvalidArgumentException(
sprintf("Node type definition file '<info>%s</info>' does not have read permissions.", $cnd_file)
'At least one definition (i.e. file or folder) must be specified'
);
}

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

$this->updateFromCnd($output, $session, $cnd, $allowUpdate);
$filePaths = $this->getFilePaths($definitions);

$count = 0;
foreach ($filePaths as $filePath) {
$cnd = file_get_contents($filePath);
$this->updateFromCnd($output, $session, $cnd, $allowUpdate);
$output->writeln(sprintf('Registered: <info>%s</info>', $filePath));
$count++;
}

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

return 0;
}
Expand Down Expand Up @@ -109,4 +117,60 @@ protected function updateFromCnd(OutputInterface $output, SessionInterface $sess
throw $e;
}
}

/**
* Return a list of node type definition file paths from
* the given definition files or folders.
*
* @param array $definitions List of files of folders
*
* @return array Array of full paths to all the type node definition files.
*/
protected function getFilePaths($definitions)
Copy link
Member

Choose a reason for hiding this comment

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

we could depend on the symfony file component, would be much nicer. but i would say its not worth the additional dependency.

Copy link
Member

Choose a reason for hiding this comment

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

yeah

{
$filePaths = array();

foreach ($definitions as $definition) {
$definition = realpath($definition);

if (is_dir($definition)) {
$dirHandle = opendir($definition);

while ($file = readdir($dirHandle)) {
if (false === $this->fileIsNodeType($file)) {
continue;
}

$filePath = sprintf('%s/%s', $definition, $file);

if (!is_readable($filePath)) {
throw new \InvalidArgumentException(
sprintf("Node type definition file '<info>%s</info>' does not have read permissions.", $file)
);
}

$filePaths[] = $filePath;
}
} else {
if (!file_exists($definition)) {
throw new \InvalidArgumentException(
sprintf("Node type definition file '<info>%s</info>' does not exist.", $definition)
);
}

$filePaths[] = $definition;
}
}

return $filePaths;
}

protected function fileIsNodeType($filename)
{
if (substr($filename, -4) == '.cnd') {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function setUp()
)->disableOriginalConstructor()->getMock();
}

public function testNodeTypeList()
public function testNodeTypeRegister()
{
$this->session->expects($this->once())
->method('getWorkspace')
Expand All @@ -28,7 +28,7 @@ public function testNodeTypeList()
->method('registerNodeTypesCnd');

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