Skip to content

Commit 3072845

Browse files
committed
Make commands lazily loaded
1 parent 7d69fb1 commit 3072845

13 files changed

+185
-12
lines changed

Command/ClearMetadataCacheDoctrineODMCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
*/
1515
class ClearMetadataCacheDoctrineODMCommand extends MetadataCommand
1616
{
17+
protected static $defaultName = 'doctrine:mongodb:cache:clear-metadata';
18+
1719
protected function configure()
1820
{
1921
parent::configure();
2022

2123
$this
22-
->setName('doctrine:mongodb:cache:clear-metadata')
2324
->setDescription('Clear all metadata cache for a document manager.')
2425
->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command.')
2526
->setHelp(<<<EOT

Command/CreateSchemaDoctrineODMCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
class CreateSchemaDoctrineODMCommand extends CreateCommand
1717
{
18+
protected static $defaultName = 'doctrine:mongodb:schema:create';
19+
1820
protected function configure()
1921
{
2022
parent::configure();
2123

2224
$this
23-
->setName('doctrine:mongodb:schema:create')
2425
->addOption('dm', null, InputOption::VALUE_REQUIRED, 'The document manager to use for this command.')
2526
->setHelp(<<<EOT
2627
The <info>doctrine:mongodb:schema:create</info> command creates the default document manager's schema:

Command/DropSchemaDoctrineODMCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
class DropSchemaDoctrineODMCommand extends DropCommand
1717
{
18+
protected static $defaultName = 'doctrine:mongodb:schema:drop';
19+
1820
protected function configure()
1921
{
2022
parent::configure();
2123

2224
$this
23-
->setName('doctrine:mongodb:schema:drop')
2425
->addOption('dm', null, InputOption::VALUE_REQUIRED, 'The document manager to use for this command.')
2526
->setHelp(<<<EOT
2627
The <info>doctrine:mongodb:schema:drop</info> command drops the default document manager's schema:
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
4+
namespace Doctrine\Bundle\MongoDBBundle\Command;
5+
6+
use Symfony\Component\Console\Input\InputArgument;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
/**
12+
* Generate document classes from mapping information
13+
*
14+
* @author Fabien Potencier <[email protected]>
15+
* @author Jonathan H. Wage <[email protected]>
16+
*/
17+
class GenerateDocumentsDoctrineODMCommand extends DoctrineODMCommand
18+
{
19+
protected static $defaultName = 'doctrine:mongodb:generate:documents';
20+
21+
protected function configure()
22+
{
23+
$this
24+
->setDescription('Generate document classes and method stubs from your mapping information.')
25+
->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to initialize the document or documents in.')
26+
->addOption('document', null, InputOption::VALUE_OPTIONAL, 'The document class to initialize (shortname without namespace).')
27+
->addOption('no-backup', null, InputOption::VALUE_NONE, 'Do not backup existing entities files.')
28+
->setHelp(<<<EOT
29+
The <info>doctrine:mongodb:generate:documents</info> command generates document classes and method stubs from your mapping information:
30+
31+
You have to limit generation of documents to an individual bundle:
32+
33+
<info>php app/console doctrine:mongodb:generate:documents MyCustomBundle</info>
34+
35+
Alternatively, you can limit generation to a single document within a bundle:
36+
37+
<info>php app/console doctrine:mongodb:generate:documents "MyCustomBundle" --document="User"</info>
38+
39+
You have to specify the shortname (without namespace) of the document you want to filter for.
40+
41+
By default, the unmodified version of each document is backed up and saved
42+
(e.g. ~Product.php). To prevent this task from creating the backup file,
43+
pass the <comment>--no-backup</comment> option:
44+
45+
<info>php app/console doctrine:mongodb:generate:documents MyCustomBundle --no-backup</info>
46+
EOT
47+
);
48+
}
49+
50+
protected function execute(InputInterface $input, OutputInterface $output)
51+
{
52+
$bundleName = $input->getArgument('bundle');
53+
$filterDocument = $input->getOption('document');
54+
55+
$foundBundle = $this->findBundle($bundleName);
56+
57+
if ($metadatas = $this->getBundleMetadatas($foundBundle)) {
58+
$output->writeln(sprintf('Generating documents for "<info>%s</info>"', $foundBundle->getName()));
59+
$documentGenerator = $this->getDocumentGenerator();
60+
$documentGenerator->setBackupExisting(!$input->getOption('no-backup'));
61+
62+
foreach ($metadatas as $metadata) {
63+
if ($filterDocument && $metadata->getReflectionClass()->getShortName() != $filterDocument) {
64+
continue;
65+
}
66+
67+
if (strpos($metadata->name, $foundBundle->getNamespace()) === false) {
68+
throw new \RuntimeException(
69+
"Document " . $metadata->name . " and bundle don't have a common namespace, ".
70+
"generation failed because the target directory cannot be detected.");
71+
}
72+
73+
$output->writeln(sprintf(' > generating <comment>%s</comment>', $metadata->name));
74+
$documentGenerator->generate([$metadata], $this->findBasePathForBundle($foundBundle));
75+
}
76+
} else {
77+
throw new \RuntimeException(
78+
"Bundle " . $bundleName . " does not contain any mapped documents.".
79+
"Did you maybe forget to define a mapping configuration?");
80+
}
81+
}
82+
}

Command/GenerateHydratorsDoctrineODMCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
*/
1515
class GenerateHydratorsDoctrineODMCommand extends GenerateHydratorsCommand
1616
{
17+
protected static $defaultName = 'doctrine:mongodb:generate:hydrators';
18+
1719
protected function configure()
1820
{
1921
parent::configure();
2022

2123
$this
22-
->setName('doctrine:mongodb:generate:hydrators')
2324
->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command.')
2425
->setHelp(<<<EOT
2526
The <info>doctrine:mongodb:generate:hydrators</info> command generates hydrator classes for your documents:

Command/GenerateProxiesDoctrineODMCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
*/
1515
class GenerateProxiesDoctrineODMCommand extends GenerateProxiesCommand
1616
{
17+
protected static $defaultName = 'doctrine:mongodb:generate:proxies';
18+
1719
protected function configure()
1820
{
1921
parent::configure();
2022

2123
$this
22-
->setName('doctrine:mongodb:generate:proxies')
2324
->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command.')
2425
->setHelp(<<<EOT
2526
The <info>doctrine:mongodb:generate:proxies</info> command generates proxy classes for your default document manager:
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
4+
namespace Doctrine\Bundle\MongoDBBundle\Command;
5+
6+
use Doctrine\ODM\MongoDB\Tools\DocumentRepositoryGenerator;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Input\InputOption;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
/**
13+
* Command to generate repository classes for mapping information.
14+
*
15+
* @author Fabien Potencier <[email protected]>
16+
* @author Jonathan H. Wage <[email protected]>
17+
*/
18+
class GenerateRepositoriesDoctrineODMCommand extends DoctrineODMCommand
19+
{
20+
protected static $defaultName = 'doctrine:mongodb:generate:repositories';
21+
22+
protected function configure()
23+
{
24+
$this
25+
->setDescription('Generate repository classes from your mapping information.')
26+
->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to initialize the repositories in.')
27+
->addOption('document', null, InputOption::VALUE_OPTIONAL, 'The document class to generate the repository for (shortname without namespace).')
28+
->setHelp(<<<EOT
29+
The <info>doctrine:mongodb:generate:repositories</info> command generates the configured document repository classes from your mapping information:
30+
31+
<info>./app/console doctrine:mongodb:generate:repositories</info>
32+
EOT
33+
);
34+
}
35+
36+
protected function execute(InputInterface $input, OutputInterface $output)
37+
{
38+
$bundleName = $input->getArgument('bundle');
39+
$filterDocument = $input->getOption('document');
40+
41+
$foundBundle = $this->findBundle($bundleName);
42+
43+
if ($metadatas = $this->getBundleMetadatas($foundBundle)) {
44+
$output->writeln(sprintf('Generating document repositories for "<info>%s</info>"', $foundBundle->getName()));
45+
$generator = new DocumentRepositoryGenerator();
46+
47+
foreach ($metadatas as $metadata) {
48+
if ($filterDocument && $filterDocument !== $metadata->reflClass->getShortname()) {
49+
continue;
50+
}
51+
52+
if ($metadata->customRepositoryClassName) {
53+
if (strpos($metadata->customRepositoryClassName, $foundBundle->getNamespace()) === false) {
54+
throw new \RuntimeException(
55+
"Repository " . $metadata->customRepositoryClassName . " and bundle don't have a common namespace, ".
56+
"generation failed because the target directory cannot be detected.");
57+
}
58+
59+
$output->writeln(sprintf(' > <info>OK</info> generating <comment>%s</comment>', $metadata->customRepositoryClassName));
60+
$generator->writeDocumentRepositoryClass(
61+
$metadata->customRepositoryClassName,
62+
$foundBundle->getPath(),
63+
$foundBundle->getNamespace()
64+
);
65+
} else {
66+
$output->writeln(sprintf(' > <error>SKIP</error> no custom repository for <comment>%s</comment>', $metadata->name));
67+
}
68+
}
69+
} else {
70+
throw new \RuntimeException("Bundle " . $bundleName . " does not contain any mapped documents.");
71+
}
72+
}
73+
}

Command/InfoDoctrineODMCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
*/
1919
class InfoDoctrineODMCommand extends DoctrineODMCommand
2020
{
21+
protected static $defaultName = 'doctrine:mongodb:mapping:info';
22+
2123
protected function configure()
2224
{
2325
$this
24-
->setName('doctrine:mongodb:mapping:info')
2526
->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command.')
2627
->setDescription('Show basic information about all mapped documents.')
2728
->setHelp(<<<EOT

Command/LoadDataFixturesDoctrineODMCommand.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@
2727
*/
2828
class LoadDataFixturesDoctrineODMCommand extends DoctrineODMCommand
2929
{
30-
/** @var SymfonyFixturesLoaderInterface */
30+
protected static $defaultName = 'doctrine:mongodb:fixtures:load';
31+
32+
/**
33+
* @var KernelInterface
34+
*/
35+
private $kernel;
36+
37+
/**
38+
* @var SymfonyFixturesLoaderInterface
39+
*/
3140
private $fixturesLoader;
3241

3342
public function __construct(?ManagerRegistry $registry = null, ?KernelInterface $kernel = null, ?SymfonyFixturesLoaderInterface $fixturesLoader = null)
@@ -48,7 +57,6 @@ public function isEnabled()
4857
protected function configure()
4958
{
5059
$this
51-
->setName('doctrine:mongodb:fixtures:load')
5260
->setDescription('Load data fixtures to your database.')
5361
->addOption('services', null, InputOption::VALUE_NONE, 'Use services as fixtures')
5462
->addOption('group', null, InputOption::VALUE_IS_ARRAY|InputOption::VALUE_REQUIRED, 'Only load fixtures that belong to this group (use with --services)')

Command/QueryDoctrineODMCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
*/
1515
class QueryDoctrineODMCommand extends QueryCommand
1616
{
17+
protected static $defaultName = 'doctrine:mongodb:query';
18+
1719
protected function configure()
1820
{
1921
parent::configure();
2022

2123
$this
22-
->setName('doctrine:mongodb:query')
2324
->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command.');
2425
}
2526

0 commit comments

Comments
 (0)