From ae79a381b8b3c8d2a9ef636dbac059f31add04b5 Mon Sep 17 00:00:00 2001 From: dantleech Date: Sun, 20 Jul 2014 20:59:23 +0200 Subject: [PATCH] [POC] Generate node types command - Very simple command for simply generating the CND files for each document in the given bundle --- Command/CndGenerateCommand.php | 96 ++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Command/CndGenerateCommand.php diff --git a/Command/CndGenerateCommand.php b/Command/CndGenerateCommand.php new file mode 100644 index 00000000..18bb57e4 --- /dev/null +++ b/Command/CndGenerateCommand.php @@ -0,0 +1,96 @@ +. + */ + +namespace Doctrine\Bundle\PHPCRBundle\Command; + +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\HttpKernel\Bundle\BundleInterface; +use Symfony\Component\Console\Input\InputArgument; + +class CndGenerateCommand extends ContainerAwareCommand +{ + public function configure() + { + $this->setName('doctrine:phpcr:generate:cnd'); + $this->addArgument('bundleName', InputArgument::REQUIRED, 'Generate node types for this bundle'); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $cndPath = 'Resources/config/phpcr-node-types'; + + $bundleName = $input->getArgument('bundleName'); + $manager = $this->getContainer()->get('doctrine_phpcr'); + $kernel = $this->getContainer()->get('kernel'); + $fs = new Filesystem(); + + $metas = $manager->getManager()->getMetadataFactory()->getAllMetadata(); + $bundle = $kernel->getBundle($bundleName); + list($ntNs, $ntNsAlias) = $this->getNsInfo($bundle); + + + foreach ($metas as $meta) { + if (0 !== strpos($meta->name, $bundle->getNamespace())) { + continue; + } + + $ntName = $meta->getReflectionClass()->getShortName(); + + $destPath = sprintf('%s/%s/%s.%s.%s', $bundle->getPath(), $cndPath, $ntNsAlias, $ntName, 'cnd'); + $dirPath = dirname($destPath); + + if (!file_exists($dirPath) ){ + $output->writeln('Creating directory: %s', $dirPath); + $fs->mkdir($dirPath); + } + + if (file_exists($destPath)) { + $fs->remove($destPath); + } + + $template = array( + sprintf('// File generated by the DoctrinePHPCRBundle %s', date('C')), + sprintf('<%s = \'%s\'>', $ntNsAlias, $ntNs), + sprintf('[%s:%s] > nt:unstructured', $ntNsAlias, $ntName), + ); + + $output->writeln(sprintf('Writing CND file: %s', $destPath)); + file_put_contents($destPath, join("\n", $template)); + } + } + + public function getNsInfo(BundleInterface $bundle) + { + $extension = $bundle->getContainerExtension(); + + if (null === $extension) { + throw new \Exception(sprintf( + 'No container extension defined for bundle "%s"' . PHP_EOL . + 'A container extension is mandatory for CND generation', + $bundle->getName() + )); + } + + return array($extension->getNamespace(), $extension->getAlias()); + } +}