Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Porting doctrine:mapping:info command from ORM to doctrine:mongodb:ma…
…pping:info in the MongoDB ODM.
- Loading branch information
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/Symfony/Bundle/DoctrineMongoDBBundle/Command/InfoDoctrineODMCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\DoctrineMongoDBBundle\Command; | ||
|
||
use Doctrine\ODM\MongoDB\Mapping\MappingException; | ||
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 documents | ||
* | ||
* @author Benjamin Eberlei <kontakt@beberlei.de> | ||
* @author Jonathan H. Wage <jonwage@gmail.com> | ||
*/ | ||
class InfoDoctrineODMCommand extends DoctrineODMCommand | ||
{ | ||
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 | ||
The <info>doctrine:mapping:info</info> shows basic information about which | ||
documents exist and possibly if their mapping information contains errors or not. | ||
<info>./app/console doctrine:mapping:info</info> | ||
If you are using multiple document managers you can pick your choice with the <info>--dm</info> option: | ||
<info>./app/console doctrine:mapping:info --dm=default</info> | ||
EOT | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$documentManagerName = $input->getOption('dm') ? | ||
$input->getOption('dm') : | ||
$this->container->getParameter('doctrine.odm.mongodb.default_document_manager'); | ||
|
||
$documentManagerService = sprintf('doctrine.odm.mongodb.%s_document_manager', $documentManagerName); | ||
|
||
/* @var $documentManager Doctrine\ODM\MongoDB\DocumentManager */ | ||
$documentManager = $this->container->get($documentManagerService); | ||
|
||
$documentClassNames = $documentManager->getConfiguration() | ||
->getMetadataDriverImpl() | ||
->getAllClassNames(); | ||
|
||
if (!$entityClassNames) { | ||
throw new \Exception( | ||
'You do not have any mapped Doctrine MongoDB ODM documents for any of your bundles. '. | ||
'Create a class inside the Document 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> documents mapped in document manager <info>%s</info>:\n", | ||
count($documentClassNames), $documentManagerName), true); | ||
|
||
foreach ($documentClassNames AS $documentClassName) { | ||
try { | ||
$cm = $documentManager->getClassMetadata($documentClassName); | ||
$output->write("<info>[OK]</info> " . $documentClassName, true); | ||
} catch(MappingException $e) { | ||
$output->write("<error>[FAIL]</error> " . $documentClassName, true); | ||
$output->write("<comment>" . $e->getMessage()."</comment>", true); | ||
$output->write("", true); | ||
} | ||
} | ||
} | ||
} |