Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Commands.

  • Loading branch information...
commit 9559766ce1106ceaeb01c5c8375c570199e1eccb 1 parent 7248f7f
@golovanov authored
View
43 Command/ClearMetadataCacheDoctrineOXMCommand.php
@@ -0,0 +1,43 @@
+<?php
+
+namespace Doctrine\Bundle\OXMBundle\Command;
+
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Doctrine\OXM\Tools\Console\Command\ClearCache\MetadataCommand;
+
+/**
+ * Command to clear the metadata cache of the various cache drivers.
+ *
+ * @author Igor Golovanov <igor.golovanov@gmail.com>
+ */
+class ClearMetadataCacheDoctrineOXMCommand extends MetadataCommand
+{
+ protected function configure()
+ {
+ parent::configure();
+
+ $this
+ ->setName('doctrine:oxm:cache:clear-metadata')
+ ->setDescription('Clear all metadata cache for a xml-entity manager.')
+ ->addOption('xem', null, InputOption::VALUE_OPTIONAL, 'The xml-entity manager to use for this command.')
+ ->setHelp(<<<EOT
+The <info>doctrine:oxm:cache:clear-metadata</info> command clears all metadata cache for the default xml-entity manager:
+
+ <info>./app/console doctrine:oxm:cache:clear-metadata</info>
+
+You can also optionally specify the <comment>--xem</comment> option to specify which xml-entity manager to clear the cache for:
+
+ <info>./app/console doctrine:oxm:cache:clear-metadata --xem=default</info>
+EOT
+ );
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ DoctrineOXMCommand::setApplicationXmlEntityManager($this->getApplication(), $input->getOption('xem'));
+
+ return parent::execute($input, $output);
+ }
+}
View
111 Command/DoctrineOXMCommand.php
@@ -0,0 +1,111 @@
+<?php
+
+namespace Doctrine\Bundle\OXMBundle\Command;
+
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Bundle\FrameworkBundle\Console\Application;
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+use Doctrine\OXM\Tools\Console\Helper\XmlEntityManagerHelper;
+use Doctrine\OXM\Tools\DisconnectedClassMetadataFactory;
+use Doctrine\OXM\Tools\XmlEntityGenerator;
+
+/**
+ * Base class for Doctrine OXM console commands to extend.
+ *
+ * @author Igor Golovanov <igor.golovanov@gmail.com>
+ */
+abstract class DoctrineOXMCommand extends ContainerAwareCommand
+{
+ public static function setApplicationXmlEntityManager(Application $application, $xemName)
+ {
+ $container = $application->getKernel()->getContainer();
+ $xemName = $xemName ? $xemName : 'default';
+ $xemServiceName = sprintf('doctrine.oxm.%s_xml_entity_manager', $xemName);
+ if (!$container->has($xemServiceName)) {
+ throw new \InvalidArgumentException(sprintf('Could not find Doctrine OXM XmlEntityManager named "%s"', $xemName));
+ }
+
+ $xem = $container->get($xemServiceName);
+ $helperSet = $application->getHelperSet();
+ $helperSet->set(new XmlEntityManagerHelper($xem), 'xem');
+ }
+
+ protected function getXmlEntityGenerator()
+ {
+ $xmlEntityGenerator = new XmlEntityGenerator();
+ $xmlEntityGenerator->setGenerateAnnotations(false);
+ $xmlEntityGenerator->setGenerateStubMethods(true);
+ $xmlEntityGenerator->setRegenerateXmlEntityIfExists(false);
+ $xmlEntityGenerator->setUpdateXmlEntityIfExists(true);
+ $xmlEntityGenerator->setNumSpaces(4);
+ return $xmlEntityGenerator;
+ }
+
+ protected function getDoctrineXmlEntityManagers()
+ {
+ $xmlEntityManagerNames = $this->getContainer()->getParameter('doctrine.oxm.xml_entity_managers');
+ $xmlEntityManagers = array();
+ foreach ($xmlEntityManagerNames as $xmlEntityManagerName) {
+ $xem = $this->getContainer()->get(sprintf('doctrine.oxm.%s_xml_entity_manager', $xmlEntityManagerName));
+ $xmlEntityManagers[] = $xem;
+ }
+ return $xmlEntityManagers;
+ }
+
+ protected function getBundleMetadatas(Bundle $bundle)
+ {
+ $namespace = $bundle->getNamespace();
+ $bundleMetadatas = array();
+ $xmlEntityManagers = $this->getDoctrineXmlEntityManagers();
+ foreach ($xmlEntityManagers as $key => $xem) {
+ $cmf = new DisconnectedClassMetadataFactory();
+ $cmf->setXmlEntityManager($xem);
+ $cmf->setConfiguration($xem->getConfiguration());
+ $metadatas = $cmf->getAllMetadata();
+ foreach ($metadatas as $metadata) {
+ if (strpos($metadata->name, $namespace) === 0) {
+ $bundleMetadatas[$metadata->name] = $metadata;
+ }
+ }
+ }
+
+ return $bundleMetadatas;
+ }
+
+ protected function findBundle($bundleName)
+ {
+ $foundBundle = false;
+ foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) {
+ /* @var $bundle Bundle */
+ if (strtolower($bundleName) == strtolower($bundle->getName())) {
+ $foundBundle = $bundle;
+ break;
+ }
+ }
+
+ if (!$foundBundle) {
+ throw new \InvalidArgumentException("No bundle " . $bundleName . " was found.");
+ }
+
+ return $foundBundle;
+ }
+
+ /**
+ * Transform classname to a path $foundBundle substract it to get the destination
+ *
+ * @param Bundle $bundle
+ * @return string
+ */
+ protected function findBasePathForBundle($bundle)
+ {
+ $path = str_replace('\\', '/', $bundle->getNamespace());
+ $search = str_replace('\\', '/', $bundle->getPath());
+ $destination = str_replace('/'.$path, '', $search, $c);
+
+ if ($c != 1) {
+ throw new \RuntimeException(sprintf('Can\'t find base path for bundle (path: "%s", destination: "%s").', $path, $destination));
+ }
+
+ return $destination;
+ }
+}
View
44 Command/GenerateProxiesDoctrineOXMCommand.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace Doctrine\Bundle\OXMBundle\Command;
+
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Output\Output;
+use Doctrine\OXM\Tools\Console\Command\GenerateProxiesCommand;
+
+/**
+ * Generate the Doctrine OXM xml-entitty proxies to your cache directory.
+ *
+ * @author Igor Golovanov <igor.golovanov@gmail.com>
+ */
+class GenerateProxiesDoctrineOXMCommand extends GenerateProxiesCommand
+{
+ protected function configure()
+ {
+ parent::configure();
+
+ $this
+ ->setName('doctrine:oxm:generate:proxies')
+ ->addOption('xem', null, InputOption::VALUE_OPTIONAL, 'The xml-entity manager to use for this command.')
+ ->setHelp(<<<EOT
+The <info>doctrine:mongodb:generate:proxies</info> command generates proxy classes for your default xml-entity manager:
+
+ <info>./app/console doctrine:oxm:generate:proxies</info>
+
+You can specify the xml-entity manager you want to generate the proxies for:
+
+ <info>./app/console doctrine:oxm:generate:proxies --xem=name</info>
+EOT
+ );
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ DoctrineOXMCommand::setApplicationXmlEntityManager($this->getApplication(), $input->getOption('xem'));
+
+ return parent::execute($input, $output);
+ }
+}
View
67 Command/GenerateRepositoriesDoctrineOXMCommand.php
@@ -0,0 +1,67 @@
+<?php
+
+namespace Doctrine\Bundle\OXMBundle\Command;
+
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Output\Output;
+use Doctrine\OXM\Tools\XmlEntityRepositoryGenerator;
+
+/**
+ * Command to generate repository classes for mapping information.
+ *
+ * @author Igor Golovanov <igor.golovanov@gmail.com>
+ */
+class GenerateRepositoriesDoctrineOXMCommand extends DoctrineOXMCommand
+{
+ protected function configure()
+ {
+ $this
+ ->setName('doctrine:oxm:generate:repositories')
+ ->setDescription('Generate repository classes from your mapping information.')
+ ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to initialize the repositories in.')
+ ->addOption('xm-entity', null, InputOption::VALUE_OPTIONAL, 'The xml-entity class to generate the repository for (shortname without namespace).')
+ ->setHelp(<<<EOT
+The <info>doctrine:oxm:generate:repositories</info> command generates the configured dxml-entity repository classes from your mapping information:
+
+ <info>./app/console doctrine:oxm:generate:repositories</info>
+EOT
+ );
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $bundleName = $input->getArgument('bundle');
+ $filterXmlEntity = $input->getOption('xml-entity');
+
+ $foundBundle = $this->findBundle($bundleName);
+
+ if ($metadatas = $this->getBundleMetadatas($foundBundle)) {
+ $output->writeln(sprintf('Generating xml-entity repositories for "<info>%s</info>"', $foundBundle->getName()));
+ $generator = new XmlEntityRepositoryGenerator();
+
+ foreach ($metadatas as $metadata) {
+ if ($filterDocument && $filterXmlEntity !== $metadata->reflClass->getShortname()) {
+ continue;
+ }
+
+ if ($metadata->customRepositoryClassName) {
+ if (strpos($metadata->customRepositoryClassName, $foundBundle->getNamespace()) === false) {
+ throw new \RuntimeException(
+ "Repository " . $metadata->customRepositoryClassName . " and bundle don't have a common namespace, ".
+ "generation failed because the target directory cannot be detected.");
+ }
+
+ $output->writeln(sprintf(' > <info>OK</info> generating <comment>%s</comment>', $metadata->customRepositoryClassName));
+ $generator->writeXmlEntityRepositoryClass($metadata->customRepositoryClassName, $this->findBasePathForBundle($foundBundle));
+ } else {
+ $output->writeln(sprintf(' > <error>SKIP</error> no custom repository for <comment>%s</comment>', $metadata->name));
+ }
+ }
+ } else {
+ throw new \RuntimeException("Bundle " . $bundleName . " does not contain any mapped xml-entities.");
+ }
+ }
+}
View
74 Command/InfoDoctrineOXMCommand.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace Doctrine\Bundle\OXMBundle\Command;
+
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+/**
+ * Show information about mapped xml-entities.
+ *
+ * @author Igor Golovanov <igor.golovanov@gmail.com>
+ */
+class InfoDoctrineOXMCommand extends DoctrineOXMCommand
+{
+ protected function configure()
+ {
+ $this
+ ->setName('doctrine:oxm:mapping:info')
+ ->addOption('xem', null, InputOption::VALUE_OPTIONAL, 'The xml-entity manager to use for this command.')
+ ->setDescription('Show basic information about all mapped xml-entities.')
+ ->setHelp(<<<EOT
+The <info>doctrine:oxm:mapping:info</info> shows basic information about which
+xml-entities exist and possibly if their mapping information contains errors or not.
+
+ <info>./app/console doctrine:oxm:mapping:info</info>
+
+If you are using multiple xml-entity managers you can pick your choice with the <info>--xem</info> option:
+
+ <info>./app/console doctrine:oxm:mapping:info --xem=default</info>
+EOT
+ );
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $xmlEntityManagerName = $input->getOption('xem') ?
+ $input->getOption('xem') :
+ $this->getContainer()->getParameter('doctrine.oxm.default_xml_entity_manager');
+
+ $xmlEntityManagerService = sprintf('doctrine.oxm.%s_xml_entity_manager', $xmlEntityManagerName);
+
+ /* @var $xmlEntityManager Doctrine\OXM\XmlEntityManager */
+ $xmlEntityManager = $this->getContainer()->get($xmlEntityManagerService);
+
+ $xmlEntityClassNames = $xmlEntityManager->getConfiguration()
+ ->getMetadataDriverImpl()
+ ->getAllClassNames();
+
+ if (!$xmlEntityClassNames) {
+ throw new \Exception(
+ 'You do not have any mapped Doctrine OXM xml-entities for any of your bundles. '.
+ 'Create a class inside the XmlEntity namespace of any of your bundles and provide '.
+ 'mapping information for it with Annotations directly in the classes doc blocks '.
+ 'or with XML/YAML in your bundles Resources/config/doctrine/metadata/mongodb directory.'
+ );
+ }
+
+ $output->write(sprintf("Found <info>%d</info> xml-entities mapped in xml-entity manager <info>%s</info>:\n",
+ count($xmlEntityClassNames), $xmlEntityManagerName), true);
+
+ foreach ($xmlEntityClassNames AS $xmlEntityClassName) {
+ try {
+ $cm = $xmlEntityManager->getClassMetadata($xmlEntityClassName);
+ $output->write("<info>[OK]</info> " . $xmlEntityClassName, true);
+ } catch(\Exception $e) {
+ $output->write("<error>[FAIL]</error> " . $xmlEntityClassName, true);
+ $output->write("<comment>" . $e->getMessage()."</comment>", true);
+ $output->write("", true);
+ }
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.