Skip to content

Commit

Permalink
More work to DoctrineBundle Console Commands and updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage authored and fabpot committed Apr 23, 2010
1 parent 4db2cae commit 2c41e93
Show file tree
Hide file tree
Showing 21 changed files with 609 additions and 231 deletions.
Expand Up @@ -29,8 +29,21 @@ class ClearMetadataCacheDoctrineCommand extends MetadataCommand
protected function configure()
{
parent::configure();
$this->setName('doctrine:clear-cache:metadata');
$this->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to clear the cache for.');

$this
->setName('doctrine:cache:clear-metadata')
->setDescription('Clear all metadata cache for a entity manager.')
->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to clear the cache for.')
->setHelp(<<<EOT
The <info>doctrine:cache:clear-metadata</info> command clears all metadata cache for the default entity manager:
<info>./symfony doctrine:cache:clear-metadata</info>
You can also optionally specify the <comment>--em</comment> option to specify which entity manager to clear the cache for:
<info>./symfony doctrine:cache:clear-metadata --em=default</info>
EOT
);
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand Down
Expand Up @@ -29,8 +29,21 @@ class ClearQueryCacheDoctrineCommand extends QueryCommand
protected function configure()
{
parent::configure();
$this->setName('doctrine:clear-cache:query');
$this->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to clear the cache for.');

$this
->setName('doctrine:cache:clear-query')
->setDescription('Clear all query cache for a entity manager.')
->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to clear the cache for.')
->setHelp(<<<EOT
The <info>doctrine:cache:clear-query</info> command clears all query cache for the default entity manager:
<info>./symfony doctrine:cache:clear-query</info>
You can also optionally specify the <comment>--em</comment> option to specify which entity manager to clear the cache for:
<info>./symfony doctrine:cache:clear-query --em=default</info>
EOT
);
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand Down
Expand Up @@ -29,8 +29,33 @@ class ClearResultCacheDoctrineCommand extends ResultCommand
protected function configure()
{
parent::configure();
$this->setName('doctrine:clear-cache:result');
$this->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to clear the cache for.');

$this
->setName('doctrine:cache:clear-result')
->setDescription('Clear result cache for a entity manager.')
->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to clear the cache for.')
->setHelp(<<<EOT
The <info>doctrine:cache:clear-result</info> command clears all result cache for the default entity manager:
<info>./symfony doctrine:cache:clear-result</info>
You can also optionally specify the <comment>--em</comment> option to specify which entity manager to clear the cache for:
<info>./symfony doctrine:cache:clear-result --em=default</info>
If you don't want to clear all result cache you can specify some additional options to control what cache is deleted:
<info>./symfony doctrine:cache:clear-result --id=cache_key</info>
Or you can specify a <comment>--regex</comment> to delete cache entries that match it:
<info>./symfony doctrine:cache:clear-result --regex="user_(.*)"</info>
You can also specify a <comment>--prefix</comment> or <comment>--suffix</comment> to delete cache entries for:
<info>./symfony doctrine:cache:clear-result --prefix="user_" --suffix="_frontend"</info>
EOT
);
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand Down
@@ -0,0 +1,124 @@
<?php

namespace Symfony\Framework\DoctrineBundle\Command;

use Symfony\Components\Console\Input\InputArgument;
use Symfony\Components\Console\Input\InputOption;
use Symfony\Components\Console\Input\InputInterface;
use Symfony\Components\Console\Output\OutputInterface;
use Symfony\Components\Console\Output\Output;
use Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\ORM\Mapping\Driver\DatabaseDriver;
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
use Doctrine\ORM\Tools\ConvertDoctrine1Schema;

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

/**
* Convert a Doctrine 1 schema to Doctrine 2 mapping files
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class ConvertDoctrine1SchemaDoctrineCommand extends DoctrineCommand
{
protected function configure()
{
$this
->setName('doctrine:mapping:convert-d1-schema')
->setDescription('Convert a Doctrine 1 schema to Doctrine 2 mapping files.')
->addArgument('d1-schema', InputArgument::REQUIRED, 'Path to the Doctrine 1 schema files.')
->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to write the converted mapping information to.')
->addArgument('mapping-type', InputArgument::OPTIONAL, 'The mapping type to export the converted mapping information to.')
->setHelp(<<<EOT
The <info>doctrine:mapping:convert-d1-schema</info> command converts a Doctrine 1 schema to Doctrine 2 mapping files:
<info>./symfony doctrine:mapping:convert-d1-schema /path/to/doctrine1schema "Bundle\MyBundle" xml</info>
Each Doctrine 1 model will have its own XML mapping file located in <info>Bundle/MyBundle/config/doctrine/metadata</info>.
EOT
);
}

/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$bundleClass = null;
$bundleDirs = $this->container->getKernelService()->getBundleDirs();
foreach ($this->container->getKernelService()->getBundles() as $bundle)
{
if (strpos(get_class($bundle), $input->getArgument('bundle')) !== false)
{
$tmp = dirname(str_replace('\\', '/', get_class($bundle)));
$namespace = str_replace('/', '\\', dirname($tmp));
$class = basename($tmp);

if (isset($bundleDirs[$namespace]))
{
$destPath = realpath($bundleDirs[$namespace]).'/'.$class;
$bundleClass = $class;
break;
}
}
}

$type = $input->getArgument('mapping-type') ? $input->getArgument('mapping-type') : 'xml';
if ($type === 'annotation')
{
$destPath .= '/Entities';
}
else
{
$destPath .= '/Resources/config/doctrine/metadata';
}

$cme = new ClassMetadataExporter();
$exporter = $cme->getExporter($type);

if ($type === 'annotation')
{
$entityGenerator = $this->getEntityGenerator();
$exporter->setEntityGenerator($entityGenerator);
}

$converter = new ConvertDoctrine1Schema($input->getArgument('d1-schema'));
$metadata = $converter->getMetadata();

if ($metadata)
{
$output->writeln(sprintf('Converting Doctrine 1 schema "<info>%s</info>"', $input->getArgument('d1-schema')));
foreach ($metadata as $class)
{
$className = $class->name;
$class->name = $namespace.'\\'.$bundleClass.'\\Entities\\'.$className;
if ($type === 'annotation')
{
$path = $destPath.'/'.$className.'.php';
}
else
{
$path = $destPath.'/'.str_replace('\\', '.', $class->name).'.dcm.xml';
}
$output->writeln(sprintf(' > writing <comment>%s</comment>', $path));
$code = $exporter->exportClassMetadata($class);
file_put_contents($path, $code);
}
} else {
$output->writeln('Database does not have any mapping information.'.PHP_EOL, 'ERROR');
}
}
}
Expand Up @@ -29,19 +29,20 @@
*/
class ConvertMappingDoctrineCommand extends ConvertMappingCommand
{
/**
* @see Command
*/
protected function configure()
{
parent::configure();
$this->setName('doctrine:convert-mapping');
$this->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to convert the mapping information from.');
$this
->setName('doctrine:mapping:convert')
->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to convert the mapping information from.')
->setHelp(<<<EOT
The <info>doctrine:mapping:convert</info> command converts mapping information between supported formats:
<info>./symfony doctrine:mapping:convert xml /path/to/output</info>
EOT
);
}

/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
Expand Down
Expand Up @@ -29,39 +29,28 @@
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class DatabaseToolDoctrineCommand extends DoctrineCommand
class CreateDatabaseDoctrineCommand extends DoctrineCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('doctrine:database-tool')
->setDescription('Create and drop the configured databases.')
->addOption('re-create', null, null, 'Drop and re-create your databases.')
->addOption('drop', null, null, 'Drop your databases.')
->addOption('create', null, null, 'Create your databases.')
->addOption('connection', null, null, 'The connection name to work on.')
;
->setName('doctrine:database:create')
->setDescription('Create the configured databases.')
->addOption('connection', null, null, 'The connection name to create the database for.')
->setHelp(<<<EOT
The <info>doctrine:database:create</info> command creates the default connections database:
<info>./symfony doctrine:database:create</info>
You can also optionally specify the name of a connection to create the database for:
<info>./symfony doctrine:database:create --connection=default</info>
EOT
);
}

/**
* @see Command
*
* @throws \InvalidArgumentException When neither of --drop or --create is specified
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('re-create'))
{
$input->setOption('drop', true);
$input->setOption('create', true);
}
if (!$input->getOption('drop') && !$input->getOption('create'))
{
throw new \InvalidArgumentException('You must specify one of the --drop and --create options or both.');
}
$found = false;
$connections = $this->getDoctrineConnections();
foreach ($connections as $name => $connection)
Expand All @@ -70,14 +59,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
continue;
}
if ($input->getOption('drop'))
{
$this->dropDatabaseForConnection($connection, $output);
}
if ($input->getOption('create'))
{
$this->createDatabaseForConnection($connection, $output);
}
$this->createDatabaseForConnection($connection, $output);
$found = true;
}
if ($found === false)
Expand All @@ -93,20 +75,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

protected function dropDatabaseForConnection(Connection $connection, OutputInterface $output)
{
$params = $connection->getParams();
$name = isset($params['path']) ? $params['path']:$params['dbname'];

try {
$connection->getSchemaManager()->dropDatabase($name);
$output->writeln(sprintf('<info>Dropped database for connection named <comment>%s</comment></info>', $name));
} catch (\Exception $e) {
$output->writeln(sprintf('<error>Could not drop database for connection named <comment>%s</comment></error>', $name));
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
}
}

protected function createDatabaseForConnection(Connection $connection, OutputInterface $output)
{
$params = $connection->getParams();
Expand Down
Expand Up @@ -28,19 +28,25 @@
*/
class CreateSchemaDoctrineCommand extends CreateCommand
{
/**
* @see Command
*/
protected function configure()
{
parent::configure();
$this->setName('doctrine:create-schema');
$this->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to create the schema for.');

$this
->setName('doctrine:schema:create')
->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to create the schema for.')
->setHelp(<<<EOT
The <info>doctrine:schema:create</info> command creates the default entity managers schema:
<info>./symfony doctrine:schema:create</info>
You can also optionally specify the name of a entity manager to create the schema for:
<info>./symfony doctrine:schema:create --em=default</info>
EOT
);
}

/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Framework/DoctrineBundle/Command/DoctrineCommand.php
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Tools\EntityGenerator;

/*
* This file is part of the Symfony framework.
Expand Down Expand Up @@ -66,6 +67,18 @@ public static function setApplicationConnection(Application $application, $connN
$helperSet->set(new ConnectionHelper($connection), 'db');
}

protected function getEntityGenerator()
{
$entityGenerator = new EntityGenerator();

$entityGenerator->setGenerateAnnotations(false);
$entityGenerator->setGenerateStubMethods(true);
$entityGenerator->setRegenerateEntityIfExists(false);
$entityGenerator->setUpdateEntityIfExists(true);
$entityGenerator->setNumSpaces(2);
return $entityGenerator;
}

protected function getEntityManager($name = null)
{
$name = $name ? $name : 'default';
Expand Down

0 comments on commit 2c41e93

Please sign in to comment.