Skip to content

Commit

Permalink
Make commands lazily loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
kefzce committed Sep 15, 2020
1 parent 7d69fb1 commit 3072845
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Command/ClearMetadataCacheDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<EOT
Expand Down
3 changes: 2 additions & 1 deletion Command/CreateSchemaDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
*/
class CreateSchemaDoctrineODMCommand extends CreateCommand
{
protected static $defaultName = 'doctrine:mongodb:schema:create';

protected function configure()
{
parent::configure();

$this
->setName('doctrine:mongodb:schema:create')
->addOption('dm', null, InputOption::VALUE_REQUIRED, 'The document manager to use for this command.')
->setHelp(<<<EOT
The <info>doctrine:mongodb:schema:create</info> command creates the default document manager's schema:
Expand Down
3 changes: 2 additions & 1 deletion Command/DropSchemaDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<EOT
The <info>doctrine:mongodb:schema:drop</info> command drops the default document manager's schema:
Expand Down
82 changes: 82 additions & 0 deletions Command/GenerateDocumentsDoctrineODMCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php


namespace Doctrine\Bundle\MongoDBBundle\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Generate document classes from mapping information
*
* @author Fabien Potencier <[email protected]>
* @author Jonathan H. Wage <[email protected]>
*/
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(<<<EOT
The <info>doctrine:mongodb:generate:documents</info> command generates document classes and method stubs from your mapping information:
You have to limit generation of documents to an individual bundle:
<info>php app/console doctrine:mongodb:generate:documents MyCustomBundle</info>
Alternatively, you can limit generation to a single document within a bundle:
<info>php app/console doctrine:mongodb:generate:documents "MyCustomBundle" --document="User"</info>
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 <comment>--no-backup</comment> option:
<info>php app/console doctrine:mongodb:generate:documents MyCustomBundle --no-backup</info>
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 "<info>%s</info>"', $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 <comment>%s</comment>', $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?");
}
}
}
3 changes: 2 additions & 1 deletion Command/GenerateHydratorsDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<EOT
The <info>doctrine:mongodb:generate:hydrators</info> command generates hydrator classes for your documents:
Expand Down
3 changes: 2 additions & 1 deletion Command/GenerateProxiesDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<EOT
The <info>doctrine:mongodb:generate:proxies</info> command generates proxy classes for your default document manager:
Expand Down
73 changes: 73 additions & 0 deletions Command/GenerateRepositoriesDoctrineODMCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php


namespace Doctrine\Bundle\MongoDBBundle\Command;

use Doctrine\ODM\MongoDB\Tools\DocumentRepositoryGenerator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command to generate repository classes for mapping information.
*
* @author Fabien Potencier <[email protected]>
* @author Jonathan H. Wage <[email protected]>
*/
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(<<<EOT
The <info>doctrine:mongodb:generate:repositories</info> command generates the configured document repository classes from your mapping information:
<info>./app/console doctrine:mongodb:generate:repositories</info>
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 "<info>%s</info>"', $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(' > <info>OK</info> generating <comment>%s</comment>', $metadata->customRepositoryClassName));
$generator->writeDocumentRepositoryClass(
$metadata->customRepositoryClassName,
$foundBundle->getPath(),
$foundBundle->getNamespace()
);
} 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 documents.");
}
}
}
3 changes: 2 additions & 1 deletion Command/InfoDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<EOT
Expand Down
12 changes: 10 additions & 2 deletions Command/LoadDataFixturesDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@
*/
class LoadDataFixturesDoctrineODMCommand extends DoctrineODMCommand
{
/** @var SymfonyFixturesLoaderInterface */
protected static $defaultName = 'doctrine:mongodb:fixtures:load';

/**
* @var KernelInterface
*/
private $kernel;

/**
* @var SymfonyFixturesLoaderInterface
*/
private $fixturesLoader;

public function __construct(?ManagerRegistry $registry = null, ?KernelInterface $kernel = null, ?SymfonyFixturesLoaderInterface $fixturesLoader = null)
Expand All @@ -48,7 +57,6 @@ public function isEnabled()
protected function configure()
{
$this
->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)')
Expand Down
3 changes: 2 additions & 1 deletion Command/QueryDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

Expand Down
3 changes: 2 additions & 1 deletion Command/ShardDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<EOT
The <info>doctrine:mongodb:schema:shard</info> command shards collections based on their metadata:
Expand Down
3 changes: 2 additions & 1 deletion Command/TailCursorDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
3 changes: 2 additions & 1 deletion Command/UpdateSchemaDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<EOT
The <info>doctrine:mongodb:schema:update</info> command updates the default document manager's schema:
Expand Down

0 comments on commit 3072845

Please sign in to comment.