From 1b51944b867b463c8a8173fb75be458858b4472d Mon Sep 17 00:00:00 2001 From: dantleech Date: Fri, 2 May 2014 17:25:42 +0200 Subject: [PATCH] Refactored node type register command to be more flexible re. loading - Allow multiple files to be specified - Allow folders to be specified --- .../Command/NodeTypeRegisterCommand.php | 100 ++++++++++++++---- .../Command/NodeTypeRegisterCommandTest.php | 4 +- 2 files changed, 84 insertions(+), 20 deletions(-) diff --git a/src/PHPCR/Util/Console/Command/NodeTypeRegisterCommand.php b/src/PHPCR/Util/Console/Command/NodeTypeRegisterCommand.php index 41d11d5..201d43c 100644 --- a/src/PHPCR/Util/Console/Command/NodeTypeRegisterCommand.php +++ b/src/PHPCR/Util/Console/Command/NodeTypeRegisterCommand.php @@ -22,6 +22,7 @@ * @license http://opensource.org/licenses/MIT MIT License * * @author Uwe Jäger + * @author Daniel Leech */ class NodeTypeRegisterCommand extends BaseCommand { @@ -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') ->setHelp(<<.cnd +extension will be treated as node definition files. + +If you use --allow-update existing node type definitions will be overwritten in the repository. EOT ) @@ -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 '%s' does not exist.", $cnd_file) - ); - } elseif (!is_readable($cnd_file)) { + if (count($definitions) == 0) { throw new \InvalidArgumentException( - sprintf("Node type definition file '%s' 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: %s', $filePath)); + $count++; + } - $output->write(PHP_EOL.sprintf('Successfully registered node types from "%s"', $cnd_file) . PHP_EOL); + $output->writeln(sprintf('%d node definition file(s)', $count)); return 0; } @@ -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) + { + $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 '%s' does not have read permissions.", $file) + ); + } + + $filePaths[] = $filePath; + } + } else { + if (!file_exists($definition)) { + throw new \InvalidArgumentException( + sprintf("Node type definition file '%s' does not exist.", $definition) + ); + } + + $filePaths[] = $definition; + } + } + + return $filePaths; + } + + protected function fileIsNodeType($filename) + { + if (substr($filename, -4) == '.cnd') { + return true; + } + + return false; + } } diff --git a/tests/PHPCR/Tests/Util/Console/Command/NodeTypeRegisterCommandTest.php b/tests/PHPCR/Tests/Util/Console/Command/NodeTypeRegisterCommandTest.php index b023b72..6c7adfb 100644 --- a/tests/PHPCR/Tests/Util/Console/Command/NodeTypeRegisterCommandTest.php +++ b/tests/PHPCR/Tests/Util/Console/Command/NodeTypeRegisterCommandTest.php @@ -16,7 +16,7 @@ public function setUp() )->disableOriginalConstructor()->getMock(); } - public function testNodeTypeList() + public function testNodeTypeRegister() { $this->session->expects($this->once()) ->method('getWorkspace') @@ -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'), )); } }