Skip to content

Instantly share code, notes, and snippets.

@FireFoxIXI
Last active August 29, 2015 14:03
Show Gist options
  • Save FireFoxIXI/7f0af3a3b8aa8bf72c33 to your computer and use it in GitHub Desktop.
Save FireFoxIXI/7f0af3a3b8aa8bf72c33 to your computer and use it in GitHub Desktop.
Extended doctrine info for symfony2

Files are: vendor/doctrine/orm/lib/Doctrine/ORM/Tools/Console/Command/ExtendedInfoCommand.php vendor/doctrine/doctrine-bundle/Doctrine/Bundle/DoctrineBundle/Command/Proxy/ExtendedInfoDoctrineCommand.php

<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Tools\Console\Command;
use Doctrine\ORM\Mapping\MappingException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
/**
* Show information about mapped entities.
*
* @link www.doctrine-project.org
* @since 2.1
* @author Steve Ludovicy <steve@gms.lu>
*/
class ExtendedInfoCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('orm:infox')
->setDescription('Show information and manipulate all mapped entities')
->setHelp(<<<EOT
The <info>%command.name%</info> shows information about which
entities exist and possibly if their mapping information contains errors or
not. Furthermore it allows direct manipulation of an entity like making fields nullable or not and more.
EOT
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/* @var $entityManager \Doctrine\ORM\EntityManager */
$entityManager = $this->getHelper('em')->getEntityManager();
$entityClassNames = $entityManager->getConfiguration()
->getMetadataDriverImpl()
->getAllClassNames();
if (!$entityClassNames) {
throw new \Exception(
'You do not have any mapped Doctrine ORM entities according to the current configuration. '.
'If you have entities or mapping files you should check your mapping configuration for errors.'
);
}
$output->writeln(sprintf("Found <info>%d</info> mapped entities:", count($entityClassNames)));
$failure = false;
foreach ($entityClassNames as $entityClassName) {
try {
$metadata = $entityManager->getClassMetadata($entityClassName);
$output->writeln(sprintf("<info>[OK]</info> %s", $entityClassName));
$this->showInfo($output, $entityClassName, $metadata->fieldMappings);
} catch (MappingException $e) {
$output->writeln("<error>[FAIL]</error> ".$entityClassName);
$output->writeln(sprintf("<comment>%s</comment>", $e->getMessage()));
$output->writeln('');
$failure = true;
}
}
return $failure ? 1 : 0;
}
/**
* {@inheritdoc}
*/
protected function showInfo($output, $entityClassName, $metaData)
{
$table = new Table($output);
$table->setHeaders(array('fieldName', 'type', 'columnName', 'nullable'));
$rows = array();
foreach ($metaData as $info) {
$nullable = isset($info['nullable']) ? '<fg=green>✓</fg=green>' : '';
$rows[] = array($info['fieldName'], $info['type'], $info['columnName'], $nullable);
}
$table->setRows($rows);
$table->render();
}
}
<?php
/*
* This file is part of the Doctrine Bundle
*
* The code was originally distributed inside the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
* (c) Doctrine Project, Benjamin Eberlei <kontakt@beberlei.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\ORM\Tools\Console\Command\ExtendedInfoCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Show information about mapped entities
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class ExtendedInfoDoctrineCommand extends ExtendedInfoCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setName('doctrine:mapping:infox')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command')
->setDescription('Shows basic information about all mapped entities')
->setHelp(<<<EOT
The <info>%command.name%</info> shows information about which entities exist
and possibly if their mapping information contains errors or not.
Furthermore it allows direct manipulation of an entity like making fields nullable or not and more.
<info>php app/console doctrine:mapping:infox</info>
If you are using multiple entity managers you can pick your choice with the
<info>--em</info> option:
<info>php app/console doctrine:mapping:infox --em=default</info>
EOT
);
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
parent::execute($input, $output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment