From 3072845d337d26eddea79aa3ac771ce7b2310acd Mon Sep 17 00:00:00 2001 From: systemfailure Date: Fri, 24 Jul 2020 14:24:53 +0300 Subject: [PATCH] Make commands lazily loaded --- .../ClearMetadataCacheDoctrineODMCommand.php | 3 +- Command/CreateSchemaDoctrineODMCommand.php | 3 +- Command/DropSchemaDoctrineODMCommand.php | 3 +- .../GenerateDocumentsDoctrineODMCommand.php | 82 +++++++++++++++++++ .../GenerateHydratorsDoctrineODMCommand.php | 3 +- Command/GenerateProxiesDoctrineODMCommand.php | 3 +- ...GenerateRepositoriesDoctrineODMCommand.php | 73 +++++++++++++++++ Command/InfoDoctrineODMCommand.php | 3 +- .../LoadDataFixturesDoctrineODMCommand.php | 12 ++- Command/QueryDoctrineODMCommand.php | 3 +- Command/ShardDoctrineODMCommand.php | 3 +- Command/TailCursorDoctrineODMCommand.php | 3 +- Command/UpdateSchemaDoctrineODMCommand.php | 3 +- 13 files changed, 185 insertions(+), 12 deletions(-) create mode 100644 Command/GenerateDocumentsDoctrineODMCommand.php create mode 100644 Command/GenerateRepositoriesDoctrineODMCommand.php diff --git a/Command/ClearMetadataCacheDoctrineODMCommand.php b/Command/ClearMetadataCacheDoctrineODMCommand.php index aa2d38dc..cd34bd49 100644 --- a/Command/ClearMetadataCacheDoctrineODMCommand.php +++ b/Command/ClearMetadataCacheDoctrineODMCommand.php @@ -14,12 +14,13 @@ */ class ClearMetadataCacheDoctrineODMCommand extends MetadataCommand { + protected static $defaultName = 'doctrine:mongodb:cache:clear-metadata'; + protected function configure() { parent::configure(); $this - ->setName('doctrine:mongodb:cache:clear-metadata') ->setDescription('Clear all metadata cache for a document manager.') ->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command.') ->setHelp(<<setName('doctrine:mongodb:schema:create') ->addOption('dm', null, InputOption::VALUE_REQUIRED, 'The document manager to use for this command.') ->setHelp(<<doctrine:mongodb:schema:create command creates the default document manager's schema: diff --git a/Command/DropSchemaDoctrineODMCommand.php b/Command/DropSchemaDoctrineODMCommand.php index 9734b2d1..f4649560 100644 --- a/Command/DropSchemaDoctrineODMCommand.php +++ b/Command/DropSchemaDoctrineODMCommand.php @@ -15,12 +15,13 @@ */ class DropSchemaDoctrineODMCommand extends DropCommand { + protected static $defaultName = 'doctrine:mongodb:schema:drop'; + protected function configure() { parent::configure(); $this - ->setName('doctrine:mongodb:schema:drop') ->addOption('dm', null, InputOption::VALUE_REQUIRED, 'The document manager to use for this command.') ->setHelp(<<doctrine:mongodb:schema:drop command drops the default document manager's schema: diff --git a/Command/GenerateDocumentsDoctrineODMCommand.php b/Command/GenerateDocumentsDoctrineODMCommand.php new file mode 100644 index 00000000..052fcad0 --- /dev/null +++ b/Command/GenerateDocumentsDoctrineODMCommand.php @@ -0,0 +1,82 @@ + + * @author Jonathan H. Wage + */ +class GenerateDocumentsDoctrineODMCommand extends DoctrineODMCommand +{ + protected static $defaultName = 'doctrine:mongodb:generate:documents'; + + protected function configure() + { + $this + ->setDescription('Generate document classes and method stubs from your mapping information.') + ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to initialize the document or documents in.') + ->addOption('document', null, InputOption::VALUE_OPTIONAL, 'The document class to initialize (shortname without namespace).') + ->addOption('no-backup', null, InputOption::VALUE_NONE, 'Do not backup existing entities files.') + ->setHelp(<<doctrine:mongodb:generate:documents command generates document classes and method stubs from your mapping information: + +You have to limit generation of documents to an individual bundle: + + php app/console doctrine:mongodb:generate:documents MyCustomBundle + +Alternatively, you can limit generation to a single document within a bundle: + + php app/console doctrine:mongodb:generate:documents "MyCustomBundle" --document="User" + +You have to specify the shortname (without namespace) of the document you want to filter for. + +By default, the unmodified version of each document is backed up and saved +(e.g. ~Product.php). To prevent this task from creating the backup file, +pass the --no-backup option: + + php app/console doctrine:mongodb:generate:documents MyCustomBundle --no-backup +EOT + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $bundleName = $input->getArgument('bundle'); + $filterDocument = $input->getOption('document'); + + $foundBundle = $this->findBundle($bundleName); + + if ($metadatas = $this->getBundleMetadatas($foundBundle)) { + $output->writeln(sprintf('Generating documents for "%s"', $foundBundle->getName())); + $documentGenerator = $this->getDocumentGenerator(); + $documentGenerator->setBackupExisting(!$input->getOption('no-backup')); + + foreach ($metadatas as $metadata) { + if ($filterDocument && $metadata->getReflectionClass()->getShortName() != $filterDocument) { + continue; + } + + if (strpos($metadata->name, $foundBundle->getNamespace()) === false) { + throw new \RuntimeException( + "Document " . $metadata->name . " and bundle don't have a common namespace, ". + "generation failed because the target directory cannot be detected."); + } + + $output->writeln(sprintf(' > generating %s', $metadata->name)); + $documentGenerator->generate([$metadata], $this->findBasePathForBundle($foundBundle)); + } + } else { + throw new \RuntimeException( + "Bundle " . $bundleName . " does not contain any mapped documents.". + "Did you maybe forget to define a mapping configuration?"); + } + } +} diff --git a/Command/GenerateHydratorsDoctrineODMCommand.php b/Command/GenerateHydratorsDoctrineODMCommand.php index 3dc62c78..16b52a82 100644 --- a/Command/GenerateHydratorsDoctrineODMCommand.php +++ b/Command/GenerateHydratorsDoctrineODMCommand.php @@ -14,12 +14,13 @@ */ class GenerateHydratorsDoctrineODMCommand extends GenerateHydratorsCommand { + protected static $defaultName = 'doctrine:mongodb:generate:hydrators'; + protected function configure() { parent::configure(); $this - ->setName('doctrine:mongodb:generate:hydrators') ->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command.') ->setHelp(<<doctrine:mongodb:generate:hydrators command generates hydrator classes for your documents: diff --git a/Command/GenerateProxiesDoctrineODMCommand.php b/Command/GenerateProxiesDoctrineODMCommand.php index 515ea6b4..dc8637b5 100644 --- a/Command/GenerateProxiesDoctrineODMCommand.php +++ b/Command/GenerateProxiesDoctrineODMCommand.php @@ -14,12 +14,13 @@ */ class GenerateProxiesDoctrineODMCommand extends GenerateProxiesCommand { + protected static $defaultName = 'doctrine:mongodb:generate:proxies'; + protected function configure() { parent::configure(); $this - ->setName('doctrine:mongodb:generate:proxies') ->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command.') ->setHelp(<<doctrine:mongodb:generate:proxies command generates proxy classes for your default document manager: diff --git a/Command/GenerateRepositoriesDoctrineODMCommand.php b/Command/GenerateRepositoriesDoctrineODMCommand.php new file mode 100644 index 00000000..eb48b4bc --- /dev/null +++ b/Command/GenerateRepositoriesDoctrineODMCommand.php @@ -0,0 +1,73 @@ + + * @author Jonathan H. Wage + */ +class GenerateRepositoriesDoctrineODMCommand extends DoctrineODMCommand +{ + protected static $defaultName = 'doctrine:mongodb:generate:repositories'; + + protected function configure() + { + $this + ->setDescription('Generate repository classes from your mapping information.') + ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to initialize the repositories in.') + ->addOption('document', null, InputOption::VALUE_OPTIONAL, 'The document class to generate the repository for (shortname without namespace).') + ->setHelp(<<doctrine:mongodb:generate:repositories command generates the configured document repository classes from your mapping information: + + ./app/console doctrine:mongodb:generate:repositories +EOT + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $bundleName = $input->getArgument('bundle'); + $filterDocument = $input->getOption('document'); + + $foundBundle = $this->findBundle($bundleName); + + if ($metadatas = $this->getBundleMetadatas($foundBundle)) { + $output->writeln(sprintf('Generating document repositories for "%s"', $foundBundle->getName())); + $generator = new DocumentRepositoryGenerator(); + + foreach ($metadatas as $metadata) { + if ($filterDocument && $filterDocument !== $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(' > OK generating %s', $metadata->customRepositoryClassName)); + $generator->writeDocumentRepositoryClass( + $metadata->customRepositoryClassName, + $foundBundle->getPath(), + $foundBundle->getNamespace() + ); + } else { + $output->writeln(sprintf(' > SKIP no custom repository for %s', $metadata->name)); + } + } + } else { + throw new \RuntimeException("Bundle " . $bundleName . " does not contain any mapped documents."); + } + } +} diff --git a/Command/InfoDoctrineODMCommand.php b/Command/InfoDoctrineODMCommand.php index e98f7f74..2eae042c 100644 --- a/Command/InfoDoctrineODMCommand.php +++ b/Command/InfoDoctrineODMCommand.php @@ -18,10 +18,11 @@ */ class InfoDoctrineODMCommand extends DoctrineODMCommand { + protected static $defaultName = 'doctrine:mongodb:mapping:info'; + protected function configure() { $this - ->setName('doctrine:mongodb:mapping:info') ->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command.') ->setDescription('Show basic information about all mapped documents.') ->setHelp(<<setName('doctrine:mongodb:fixtures:load') ->setDescription('Load data fixtures to your database.') ->addOption('services', null, InputOption::VALUE_NONE, 'Use services as fixtures') ->addOption('group', null, InputOption::VALUE_IS_ARRAY|InputOption::VALUE_REQUIRED, 'Only load fixtures that belong to this group (use with --services)') diff --git a/Command/QueryDoctrineODMCommand.php b/Command/QueryDoctrineODMCommand.php index 066aae13..8c9c5c77 100644 --- a/Command/QueryDoctrineODMCommand.php +++ b/Command/QueryDoctrineODMCommand.php @@ -14,12 +14,13 @@ */ class QueryDoctrineODMCommand extends QueryCommand { + protected static $defaultName = 'doctrine:mongodb:query'; + protected function configure() { parent::configure(); $this - ->setName('doctrine:mongodb:query') ->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command.'); } diff --git a/Command/ShardDoctrineODMCommand.php b/Command/ShardDoctrineODMCommand.php index ea3e8b66..737b3e4d 100644 --- a/Command/ShardDoctrineODMCommand.php +++ b/Command/ShardDoctrineODMCommand.php @@ -15,12 +15,13 @@ */ class ShardDoctrineODMCommand extends ShardCommand { + protected static $defaultName = 'doctrine:mongodb:schema:shard'; + protected function configure() { parent::configure(); $this - ->setName('doctrine:mongodb:schema:shard') ->addOption('dm', null, InputOption::VALUE_REQUIRED, 'The document manager to use for this command.') ->setHelp(<<doctrine:mongodb:schema:shard command shards collections based on their metadata: diff --git a/Command/TailCursorDoctrineODMCommand.php b/Command/TailCursorDoctrineODMCommand.php index 65cca98c..8205efea 100644 --- a/Command/TailCursorDoctrineODMCommand.php +++ b/Command/TailCursorDoctrineODMCommand.php @@ -24,12 +24,13 @@ */ class TailCursorDoctrineODMCommand extends Command implements ContainerAwareInterface { + protected static $defaultName = 'doctrine:mongodb:tail-cursor'; + use ContainerAwareTrait; protected function configure() { $this - ->setName('doctrine:mongodb:tail-cursor') ->setDescription('Tails a mongodb cursor and processes the documents that come through') ->addArgument('document', InputArgument::REQUIRED, 'The document we are going to tail the cursor for.') ->addArgument('finder', InputArgument::REQUIRED, 'The repository finder method which returns the cursor to tail.') diff --git a/Command/UpdateSchemaDoctrineODMCommand.php b/Command/UpdateSchemaDoctrineODMCommand.php index 59bb680c..5948ae5c 100644 --- a/Command/UpdateSchemaDoctrineODMCommand.php +++ b/Command/UpdateSchemaDoctrineODMCommand.php @@ -15,12 +15,13 @@ */ class UpdateSchemaDoctrineODMCommand extends UpdateCommand { + protected static $defaultName = 'doctrine:mongodb:schema:update'; + protected function configure() { parent::configure(); $this - ->setName('doctrine:mongodb:schema:update') ->addOption('dm', null, InputOption::VALUE_REQUIRED, 'The document manager to use for this command.') ->setHelp(<<doctrine:mongodb:schema:update command updates the default document manager's schema: