Skip to content

Commit a0158cb

Browse files
committed
adding the register node types command even though it currently works only for jackalope - it should be generalized and will tell you so for now
1 parent 4983f7d commit a0158cb

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ There are a couple of useful commands to interact with a phpcr repository.
2121
To use the console, make sure you have initialized the git submodules of
2222
phpcr-utils, then copy cli-config.php.dist to cli-config.php and adjust it
2323
to your implementation of PHPCR. Then you can run the commands from the
24-
phpcr-utils directory with ``./bin/phpcr``
24+
phpcr-utils directory with ``./bin/phpcr``
2525
NOTE: If you are using PHPCR inside of Symfony, the DoctrinePHPCRBundle
2626
provides the commands inside the normal Symfony console and you don't need to
2727
prepare anything special.
2828

2929
* ``phpcr:workspace:create <name>``: Create the workspace name in the configured repository
30+
* ``phpcr:register-node-types --allow-update [cnd-file]``: Register namespaces and node types from a "Compact Node Type Definition" .cnd file
3031
* ``phpcr:dump [--sys_nodes[="..."]] [--props[="..."]] [path]``: Show the node names
3132
under the specified path. If you set sys_nodes=yes you will also see system nodes.
3233
If you set props=yes you will additionally see all properties of the dumped nodes.
@@ -35,8 +36,12 @@ prepare anything special.
3536
* ``phpcr:sql2``: Run a query in the JCR SQL2 language against the repository and dump
3637
the resulting rows to the console.
3738

38-
TODO: Implement commands for phpcr:import and phpcr:export to import
39-
and export the PHPCR document view and system view XML dumps.
39+
**TODO:**
40+
41+
* Implement commands for phpcr:import and phpcr:export to import and export the
42+
PHPCR document view and system view XML dumps.
43+
* Implement a simple .cnd parser in PHP and use it to make register-node-types
44+
work with all repositories
4045

4146

4247
## Helper Classes

bin/phpcr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ $cli->addCommands(array(
3636
new \PHPCR\Util\Console\Command\CreateWorkspaceCommand(),
3737
new \PHPCR\Util\Console\Command\DumpCommand(),
3838
new \PHPCR\Util\Console\Command\PurgeCommand(),
39+
new \PHPCR\Util\Console\Command\RegisterNodeTypesCommand(),
3940
new \PHPCR\Util\Console\Command\Sql2Command(),
4041
));
4142
$cli->run();
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace PHPCR\Util\Console\Command;
4+
5+
use Symfony\Component\Console\Input\InputArgument;
6+
use Symfony\Component\Console\Input\InputOption;
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
/**
12+
* Command to load and register a node type defined in a CND file.
13+
*
14+
* See the link below for the cnd definition.
15+
* @link http://jackrabbit.apache.org/node-type-notation.html
16+
*
17+
* @author Uwe Jäger <uwej711@googlemail.com>
18+
*/
19+
class RegisterNodeTypesCommand extends Command
20+
{
21+
/**
22+
* @see Command
23+
*/
24+
protected function configure()
25+
{
26+
$this
27+
->setName('phpcr:register-node-types')
28+
->setDescription('Register node types in the PHPCR repository')
29+
->setDefinition(array(
30+
new InputArgument(
31+
'cnd-file', InputArgument::REQUIRED, 'Register namespaces and node types from a "Compact Node Type Definition" .cnd file'
32+
),
33+
new InputOption('allow-update', '', InputOption::VALUE_NONE, 'Overwrite existig node type'),
34+
))
35+
->setHelp(<<<EOT
36+
Register node types in the PHPCR repository.
37+
38+
This command allows to register node types in the repository that are defined
39+
in a CND (Compact Namespace and Node Type Definition) file as used by jackrabbit.
40+
41+
Custom node types can be used to define the structure of content repository
42+
nodes, like allowed properties and child nodes together with the namespaces
43+
and their prefix used for the names of node types and properties.
44+
45+
If you use --allow-update existing node type definitions will be overwritten
46+
in the repository.
47+
EOT
48+
);
49+
}
50+
51+
/**
52+
* @see Command
53+
*/
54+
protected function execute(InputInterface $input, OutputInterface $output)
55+
{
56+
$cnd_file = realpath($input->getArgument('cnd-file'));
57+
58+
if (!file_exists($cnd_file)) {
59+
throw new \InvalidArgumentException(
60+
sprintf("Node type definition file '<info>%s</info>' does not exist.", $cnd_file)
61+
);
62+
} elseif (!is_readable($cnd_file)) {
63+
throw new \InvalidArgumentException(
64+
sprintf("Node type definition file '<info>%s</info>' does not have read permissions.", $cnd_file)
65+
);
66+
}
67+
68+
$cnd = file_get_contents($cnd_file);
69+
$allowUpdate = $input->getOption('allow-update');
70+
71+
$session = $this->getHelper('phpcr')->getSession();
72+
if (! $session instanceof \Jackalope\Session) {
73+
throw new \Exception('PHPCR only provides an API to register node types. Your implementation is not Jackalope (which provides a method for .cnd). TODO: parse the file and do the necessary API calls');
74+
}
75+
$ntm = $session->getWorkspace()->getNodeTypeManager();
76+
77+
try {
78+
$ntm->registerNodeTypesCnd($cnd, $allowUpdate);
79+
} catch (\PHPCR\NodeType\NodeTypeExistsException $e) {
80+
if (!$allowUpdate) {
81+
$output->write(PHP_EOL.'<error>The node type(s) you tried to register already exist.</error>'.PHP_EOL);
82+
$output->write(PHP_EOL.'If you want to override the existing definition call this command with the ');
83+
$output->write('<info>--allow-update</info> option.'.PHP_EOL);
84+
}
85+
throw $e;
86+
}
87+
$output->write(PHP_EOL.sprintf('Successfully registered node types from "<info>%s</info>"', $cnd_file) . PHP_EOL);
88+
}
89+
}

0 commit comments

Comments
 (0)