Updating DoctrineBundle for latest changes to Doctrine 2 CLI where we switched to Symfony console

This commit is contained in:
Jonathan H. Wage 2010-04-14 15:32:45 -04:00 committed by Fabien Potencier
parent 9cf9430430
commit 4db2caebae
23 changed files with 627 additions and 685 deletions

View File

@ -3,6 +3,7 @@
namespace Symfony\Foundation\Bundle;
use Symfony\Components\DependencyInjection\ContainerInterface;
use Symfony\Components\Console\Application;
@ -20,6 +21,36 @@ abstract class Bundle implements BundleInterface
public function shutdown(ContainerInterface $container)
{
}
public function registerCommands(Application $application)
{
foreach ($application->getKernel()->getBundleDirs() as $dir)
{
$bundleBase = dirname(str_replace('\\', '/', get_class($this)));
$commandDir = $dir.'/'.basename($bundleBase).'/Command';
if (!is_dir($commandDir))
{
continue;
}
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($commandDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file)
{
if ($file->isDir() || substr($file, -4) !== '.php')
{
continue;
}
$class = str_replace('/', '\\', $bundleBase).'\\Command\\'.str_replace(realpath($commandDir).'/', '', basename(realpath($file), '.php'));
$r = new \ReflectionClass($class);
if ($r->isSubclassOf('Symfony\\Components\\Console\\Command\\Command') && !$r->isAbstract())
{
$application->addCommand(new $class());
}
}
}
}
}

View File

@ -7,6 +7,7 @@ use Symfony\Components\DependencyInjection\ContainerInterface;
use Symfony\Components\DependencyInjection\Loader\Loader;
use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Framework\DoctrineBundle\DependencyInjection\DoctrineExtension;
use Symfony\Components\Console\Application;
/*
* This file is part of the Symfony framework.
@ -55,4 +56,4 @@ class Bundle extends BaseBundle
$container->setParameter('doctrine.orm.metadata_driver.mapping_dirs', $metadataDirs);
$container->setParameter('doctrine.orm.entity_dirs', $entityDirs);
}
}
}

View File

@ -1,162 +0,0 @@
<?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 Symfony\Framework\WebBundle\Util\Filesystem;
use Doctrine\Common\Cli\Configuration;
use Doctrine\Common\Cli\CliController as DoctrineCliController;
use Doctrine\DBAL\Connection;
/*
* 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.
*/
/**
* Build command allows you to easily build and re-build your Doctrine development environment
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Kris Wallsmith <kris.wallsmith@symfony-project.org>
*/
class BuildDoctrineCommand extends DoctrineCommand
{
const
BUILD_ENTITIES = 1,
BUILD_DB = 16,
OPTION_ENTITIES = 1,
OPTION_DB = 16,
OPTION_ALL = 31;
/**
* @see Command
*/
protected function configure()
{
$this
->setName('doctrine:build')
->setDescription('Build task for easily re-building your Doctrine development environment.')
->addOption('all', null, null, 'Build everything and reset the database')
->addOption('entities', null, null, 'Build model classes')
->addOption('db', null, null, 'Drop database, create database and create schema.')
->addOption('and-load', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY, 'Load data fixtures')
->addOption('and-append', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY, 'Load data fixtures and append to existing data')
->addOption('and-update-schema', null, null, 'Update schema after rebuilding all classes')
->addOption('connection', null, null, 'The connection to use.')
->setHelp('
The <info>doctrine:build</info> task builds your Doctrine development environment.
<info>php console doctrine:build --all</info>
The above command will re-build your entities and re-create your database schema.
If you wanted to only update your schema instead of re-creating it you can run
the following:
<info>php console doctrine:build --entities --and-update-schema</info>
Now your entities are re-built and your database schema is up to date!
You can also use the <info>--and-load</info> and <info>--and-append</info> to
load data fixtures after running another build option.
<info>php console doctrine:build --all --and-load</info>
The above will re-build everything and load all bundle data fixtures.
')
;
}
/**
* @see Command
*
* @throws \InvalidArgumentException When neither of any build options is included
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (!$mode = $this->calculateMode($input))
{
throw new \InvalidArgumentException(sprintf("You must include one or more of the following build options:\n--%s\n\nSee this task's help page for more information:\n\n php console help doctrine:build", join(', --', array_keys($this->getBuildOptions()))));
}
if (self::BUILD_ENTITIES == (self::BUILD_ENTITIES & $mode))
{
$this->runCommand('doctrine:build-entities');
}
if (self::BUILD_DB == (self::BUILD_DB & $mode))
{
$this->runCommand('doctrine:schema-tool', array('--re-create' => true, '--connection' => $input->getOption('connection')));
}
if ($input->getOption('and-update-schema'))
{
$this->runCommand('doctrine:schema-tool', array('--update' => true, '--connection' => $input->getOption('connection')));
$this->runCommand('doctrine:schema-tool', array('--complete-update' => true, '--connection' => $input->getOption('connection')));
}
if ($input->hasOption('and-load'))
{
$dirOrFile = $input->getOption('and-load');
$this->runCommand('doctrine:load-data-fixtures',
array('--dir-or-file' => $dirOrFile, '--append' => false)
);
}
else if ($input->hasOption('and-append'))
{
$dirOrFile = $input->getOption('and-append');
$this->runCommand('doctrine:load-data-fixtures', array('--dir-or-file' => $dirOrFile, '--append' => true));
}
}
/**
* Calculates a bit mode based on the supplied options.
*
* @param InputInterface $input
* @return integer
*/
protected function calculateMode(InputInterface $input)
{
$mode = 0;
foreach ($this->getBuildOptions() as $name => $value)
{
if ($input->getOption($name) === true)
{
$mode = $mode | $value;
}
}
return $mode;
}
/**
* Returns an array of valid build options.
*
* @return array An array of option names and their mode
*/
protected function getBuildOptions()
{
$options = array();
foreach ($this->getDefinition()->getOptions() as $option)
{
if (defined($constant = __CLASS__.'::OPTION_'.str_replace('-', '_', strtoupper($option->getName()))))
{
$options[$option->getName()] = constant($constant);
}
}
return $options;
}
}

View File

@ -1,62 +0,0 @@
<?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 Symfony\Framework\WebBundle\Util\Filesystem;
use Doctrine\Common\Cli\Configuration;
use Doctrine\Common\Cli\CliController as DoctrineCliController;
/*
* 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.
*/
/**
* Manage the cache clearing of the Doctrine ORM.
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class ClearCacheDoctrineCommand extends DoctrineCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('doctrine:clear-cache')
->setDescription('Clear cache from configured query, result and metadata drivers.')
->setAliases(array('doctrine:cc'))
->addOption('query', null, null, 'Clear the query cache.')
->addOption('metadata', null, null, 'Clear the metadata cache.')
->addOption('result', null, null, 'Clear the result cache.')
->addOption('id', null, null, 'Clear a cache entry by its id.')
->addOption('regex', null, null, 'Clear cache entries that match a regular expression.')
->addOption('prefix', null, null, 'Clear cache entries that match a prefix.')
->addOption('suffix', null, null, 'Clear cache entries that match a suffix.')
;
}
/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$options = $this->buildDoctrineCliTaskOptions($input, array(
'query', 'metadata', 'result', 'id', 'regex', 'prefix', 'suffix'
));
$this->runDoctrineCliTask('orm:clear-cache', $options);
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace Symfony\Framework\DoctrineBundle\Command;
use Symfony\Components\Console\Input\InputOption;
use Symfony\Components\Console\Input\InputInterface;
use Symfony\Components\Console\Output\OutputInterface;
use Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand;
/*
* 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.
*/
/**
* Command to clear the metadata cache of the various cache drivers.
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
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.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
return parent::execute($input, $output);
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace Symfony\Framework\DoctrineBundle\Command;
use Symfony\Components\Console\Input\InputOption;
use Symfony\Components\Console\Input\InputInterface;
use Symfony\Components\Console\Output\OutputInterface;
use Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand;
/*
* 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.
*/
/**
* Command to clear the query cache of the various cache drivers.
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
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.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
return parent::execute($input, $output);
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace Symfony\Framework\DoctrineBundle\Command;
use Symfony\Components\Console\Input\InputOption;
use Symfony\Components\Console\Input\InputInterface;
use Symfony\Components\Console\Output\OutputInterface;
use Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand;
/*
* 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.
*/
/**
* Command to clear the result cache of the various cache drivers.
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
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.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
return parent::execute($input, $output);
}
}

View File

@ -7,9 +7,7 @@ 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 Symfony\Framework\WebBundle\Util\Filesystem;
use Doctrine\Common\Cli\Configuration;
use Doctrine\Common\Cli\CliController as DoctrineCliController;
use Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand;
/*
* This file is part of the Symfony framework.
@ -29,20 +27,16 @@ use Doctrine\Common\Cli\CliController as DoctrineCliController;
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class ConvertMappingDoctrineCommand extends DoctrineCommand
class ConvertMappingDoctrineCommand extends ConvertMappingCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('doctrine:convert-mapping')
->setDescription('Convert mapping information between supported formats.')
->addOption('from', null, null, 'The source to convert from.')
->addOption('to', null, null, 'The type of mapping to convert to.')
->addOption('dest', null, null, 'Where to output the converted source.')
;
parent::configure();
$this->setName('doctrine:convert-mapping');
$this->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to convert the mapping information from.');
}
/**
@ -50,9 +44,8 @@ class ConvertMappingDoctrineCommand extends DoctrineCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$options = $this->buildDoctrineCliTaskOptions($input, array(
'from', 'to', 'dest'
));
$this->runDoctrineCliTask('orm:convert-mapping', $options);
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
return parent::execute($input, $output);
}
}

View File

@ -0,0 +1,50 @@
<?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\SchemaTool\CreateCommand;
/*
* 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.
*/
/**
* Command to create the database schema for a set of classes based on their mappings.
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
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.');
}
/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
parent::execute($input, $output);
}
}

View File

@ -10,9 +10,12 @@ use Symfony\Components\Console\Input\InputInterface;
use Symfony\Components\Console\Output\OutputInterface;
use Symfony\Components\Console\Output\Output;
use Symfony\Framework\WebBundle\Console\Application;
use Symfony\Framework\WebBundle\Util\Filesystem;
use Doctrine\Common\Cli\Configuration;
use Doctrine\Common\Cli\CliController as DoctrineCliController;
use Symfony\Foundation\Bundle\Bundle;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
/*
* This file is part of the Symfony framework.
@ -32,65 +35,57 @@ use Doctrine\Common\Cli\CliController as DoctrineCliController;
*/
abstract class DoctrineCommand extends Command
{
protected
$application,
$cli,
$em;
protected function getDoctrineCli()
public static function setApplicationEntityManager(Application $application, $emName)
{
if ($this->cli === null)
$container = $application->getKernel()->getContainer();
$emName = $emName ? $emName : 'default';
$emServiceName = sprintf('doctrine.orm.%s_entity_manager', $emName);
if (!$container->hasService($emServiceName))
{
$configuration = new Configuration();
$this->cli = new DoctrineCliController($configuration);
throw new \InvalidArgumentException(sprintf('Could not find Doctrine EntityManager named "%s"', $emName));
}
$em = $this->em ? $this->em : $this->container->getDoctrine_Orm_EntityManagerService();
$this->cli->getConfiguration()->setAttribute('em', $em);
return $this->cli;
$em = $container->getService($emServiceName);
$helperSet = $application->getHelperSet();
$helperSet->set(new ConnectionHelper($em->getConnection()), 'db');
$helperSet->set(new EntityManagerHelper($em), 'em');
}
protected function runDoctrineCliTask($name, $options = array())
public static function setApplicationConnection(Application $application, $connName)
{
$builtOptions = array();
foreach ($options as $key => $value)
$container = $application->getKernel()->getContainer();
$connName = $connName ? $connName : 'default';
$connServiceName = sprintf('doctrine.dbal.%s_connection', $connName);
if (!$container->hasService($connServiceName))
{
if ($value === null)
{
$builtOptions[] = sprintf('--%s', $key);
}
else
{
$builtOptions[] = sprintf('--%s=%s', $key, $value);
}
throw new \InvalidArgumentException(sprintf('Could not find Doctrine Connection named "%s"', $connName));
}
return $this->getDoctrineCli()->run(array_merge(array('doctrine', $name), $builtOptions));
$connection = $container->getService($connServiceName);
$helperSet = $application->getHelperSet();
$helperSet->set(new ConnectionHelper($connection), 'db');
}
protected function buildDoctrineCliTaskOptions(InputInterface $input, array $options)
protected function getEntityManager($name = null)
{
$taskOptions = array();
foreach ($options as $option)
$name = $name ? $name : 'default';
$serviceName = sprintf('doctrine.orm.%s_entity_manager', $name);
if (!$this->container->hasService($serviceName))
{
if ($value = $input->getOption($option))
{
$options[$option] = $value;
}
throw new \InvalidArgumentException(sprintf('Could not find Doctrine EntityManager named "%s"', $name));
}
return $options;
return $this->container->getService($serviceName);
}
protected function runCommand($name, array $input = array())
{
if ($this->application === null)
{
$this->application = new Application($this->container->getKernelService());
}
$application = new Application($this->container->getKernelService());
$arguments = array();
$arguments = array_merge(array($name), $input);
$input = new ArrayInput($arguments);
$this->application->setAutoExit(false);
$this->application->run($input);
$application->setAutoExit(false);
$application->run($input);
}
/**
@ -127,4 +122,45 @@ abstract class DoctrineCommand extends Command
}
return $entityManagers;
}
protected function getBundleMetadatas(Bundle $bundle)
{
$tmp = dirname(str_replace('\\', '/', get_class($bundle)));
$namespace = str_replace('/', '\\', dirname($tmp));
$class = basename($tmp);
$bundleMetadatas = array();
$entityManagers = $this->getDoctrineEntityManagers();
foreach ($entityManagers as $key => $em)
{
$cmf = new SymfonyDisconnectedClassMetadataFactory($em);
$metadatas = $cmf->getAllMetadata();
foreach ($metadatas as $metadata)
{
if (strpos($metadata->name, $namespace) !== false)
{
$bundleMetadatas[] = $metadata;
}
}
}
return $bundleMetadatas;
}
}
class SymfonyDisconnectedClassMetadataFactory extends DisconnectedClassMetadataFactory
{
/**
* @override
*/
protected function _newClassMetadataInstance($className)
{
if (class_exists($className))
{
return new ClassMetadata($className);
}
else
{
return new ClassMetadataInfo($className);
}
}
}

View File

@ -7,9 +7,7 @@ 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 Symfony\Framework\WebBundle\Util\Filesystem;
use Doctrine\Common\Cli\Configuration;
use Doctrine\Common\Cli\CliController as DoctrineCliController;
use Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand;
/*
* This file is part of the Symfony framework.
@ -21,24 +19,23 @@ use Doctrine\Common\Cli\CliController as DoctrineCliController;
*/
/**
* Check what version of the Doctrine ORM being used.
* Command to drop the database schema for a set of classes based on their mappings.
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class VersionDoctrineCommand extends DoctrineCommand
class DropSchemaDoctrineCommand extends DropCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('doctrine:version')
->setDescription('Displays the current installed Doctrine version.')
;
parent::configure();
$this->setName('doctrine:drop-schema');
$this->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to drop the schema for.');
}
/**
@ -46,6 +43,8 @@ class VersionDoctrineCommand extends DoctrineCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->runDoctrineCliTask('orm:version');
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
parent::execute($input, $output);
}
}

View File

@ -7,9 +7,7 @@ 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 Symfony\Framework\WebBundle\Util\Filesystem;
use Doctrine\Common\Cli\Configuration;
use Doctrine\Common\Cli\CliController as DoctrineCliController;
use Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand;
/*
* This file is part of the Symfony framework.
@ -28,17 +26,16 @@ use Doctrine\Common\Cli\CliController as DoctrineCliController;
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class EnsureProductionSettingsDoctrineCommand extends DoctrineCommand
class EnsureProductionSettingsDoctrineCommand extends EnsureProductionSettingsCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('doctrine:ensure-production-settings')
->setDescription('Verify that Doctrine is properly configured for a production environment.')
;
parent::configure();
$this->setName('doctrine:ensure-production-settings');
$this->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to ensure production settings for.');
}
/**
@ -46,6 +43,8 @@ class EnsureProductionSettingsDoctrineCommand extends DoctrineCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->runDoctrineCliTask('orm:ensure-production-settings');
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
parent::execute($input, $output);
}
}

View File

@ -7,9 +7,8 @@ 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 Symfony\Framework\WebBundle\Util\Filesystem;
use Doctrine\Common\Cli\Configuration;
use Doctrine\Common\Cli\CliController as DoctrineCliController;
use Symfony\Foundation\Bundle\Bundle;
use Doctrine\ORM\Tools\EntityGenerator;
/*
* This file is part of the Symfony framework.
@ -21,32 +20,36 @@ use Doctrine\Common\Cli\CliController as DoctrineCliController;
*/
/**
* Build all Bundle entity classes from mapping information.
* Generate entity classes from mapping information
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class BuildEntitiesDoctrineCommand extends DoctrineCommand
class GenerateEntitiesDoctrineCommand extends DoctrineCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('doctrine:build-entities')
->setDescription('Build all Bundle entity classes from mapping information.')
;
$this
->setName('doctrine:generate-entities')
->setDescription('Generate entity classes and method stubs from your mapping information.')
->setHelp(<<<EOT
Generate entity classes and method stubs from your mapping information.
EOT
);
}
/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dirs = array();
$entityGenerator = new EntityGenerator();
$entityGenerator->setGenerateAnnotations(false);
$entityGenerator->setGenerateStubMethods(true);
$entityGenerator->setRegenerateEntityIfExists(false);
$entityGenerator->setUpdateEntityIfExists(true);
$entityGenerator->setNumSpaces(2);
$bundleDirs = $this->container->getKernelService()->getBundleDirs();
foreach ($this->container->getKernelService()->getBundles() as $bundle)
{
@ -56,24 +59,17 @@ class BuildEntitiesDoctrineCommand extends DoctrineCommand
if (isset($bundleDirs[$namespace]))
{
if (is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Resources/config/doctrine/metadata'))
$destination = realpath($bundleDirs[$namespace].'/..');
if ($metadatas = $this->getBundleMetadatas($bundle))
{
$this->convertMapping($dir, $bundleDirs[$namespace].'/..');
}
else if (is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Entities'))
{
$this->convertMapping($dir, $bundleDirs[$namespace].'/..');
$output->writeln(sprintf('Generating entities for "<info>%s</info>"', $class));
foreach ($metadatas as $metadata)
{
$output->writeln(sprintf(' > generating <comment>%s</comment>', $metadata->name));
$entityGenerator->generate(array($metadata), $destination);
}
}
}
}
}
protected function convertMapping($mappingPath, $dest)
{
$opts = array();
$opts['--from'] = $mappingPath;
$opts['--to'] = 'annotation';
$opts['--dest'] = realpath($dest);
$this->runCommand('doctrine:convert-mapping', $opts);
}
}

View File

@ -0,0 +1,144 @@
<?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\Export\ClassMetadataExporter;
use Doctrine\ORM\Tools\EntityGenerator;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
/*
* 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.
*/
/**
* Initialize a new Doctrine entity inside a bundle.
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class GenerateEntityDoctrineCommand extends DoctrineCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('doctrine:generate-entity')
->setDescription('Generate a new Doctrine entity inside a bundle.')
->addArgument('bundle', null, InputArgument::REQUIRED, 'The bundle to initialize the entity in.')
->addArgument('entity', null, InputArgument::REQUIRED, 'The entity class to initialize.')
->addOption('mapping-type', null, InputOption::PARAMETER_OPTIONAL, 'The mapping type to to use for the entity.')
->addOption('fields', null, InputOption::PARAMETER_OPTIONAL, 'The fields to create with the new entity.')
->setHelp('
The <info>doctrine:generate-entity</info> task initializes a new Doctrine entity inside a bundle:
<comment>php console doctrine:generate-entity "Bundle\MyCustomBundle" "User\Group"</comment>
The above would initialize a new entity in the following entity namespace <info>Bundle\MyCustomBundle\Entities\User\Group</info>.
You can also optionally specify the fields you want to generate in the new entity:
<comment>php console doctrine:generate-entity "Bundle\MyCustomBundle" "User\Group" --fields="name:string(255) description:text"</comment>
')
;
}
/**
* @see Command
*
* @throws \InvalidArgumentException When the bundle doesn't end with Bundle (Example: "Bundle\MySampleBundle")
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (!preg_match('/Bundle$/', $bundle = $input->getArgument('bundle')))
{
throw new \InvalidArgumentException('The bundle name must end with Bundle. Example: "Bundle\MySampleBundle".');
}
$dirs = $this->container->getKernelService()->getBundleDirs();
$tmp = str_replace('\\', '/', $bundle);
$namespace = str_replace('/', '\\', dirname($tmp));
$bundle = basename($tmp);
if (!isset($dirs[$namespace]))
{
throw new \InvalidArgumentException(sprintf('Unable to initialize the bundle entity (%s not defined).', $namespace));
}
$entity = $input->getArgument('entity');
$entityNamespace = $namespace.'\\'.$bundle.'\\Entities';
$fullEntityClassName = $entityNamespace.'\\'.$entity;
$tmp = str_replace('\\', '/', $fullEntityClassName);
$tmp = str_replace('/', '\\', dirname($tmp));
$className = basename($tmp);
$mappingType = $input->getOption('mapping-type');
$mappingType = $mappingType ? $mappingType : 'xml';
$class = new ClassMetadataInfo($fullEntityClassName);
$class->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
$class->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
// Map the specified fields
$fields = $input->getOption('fields');
$e = explode(' ', $fields);
foreach ($e as $value)
{
$e = explode(':', $value);
$name = $e[0];
$type = isset($e[1]) ? $e[1] : 'string';
preg_match_all('/(.*)\((.*)\)/', $type, $matches);
$type = isset($matches[1][0]) ? $matches[1][0] : 'string';
$length = isset($matches[2][0]) ? $matches[2][0] : null;
$class->mapField(array(
'fieldName' => $name,
'type' => $type,
'length' => $length
));
}
// Setup a new exporter for the mapping type specified
$cme = new ClassMetadataExporter();
$exporter = $cme->getExporter($mappingType);
if ($mappingType === 'annotation')
{
$path = $dirs[$namespace].'/'.$bundle.'/Entities/'.str_replace($entityNamespace.'\\', null, $fullEntityClassName).'.php';
$entityGenerator = new EntityGenerator();
$entityGenerator->setGenerateAnnotations(false);
$entityGenerator->setGenerateStubMethods(true);
$entityGenerator->setRegenerateEntityIfExists(false);
$entityGenerator->setUpdateEntityIfExists(false);
$entityGenerator->setNumSpaces(2);
$exporter->setEntityGenerator($entityGenerator);
} else {
$path = $dirs[$namespace].'/'.$bundle.'/Resources/config/doctrine/metadata/'.str_replace('\\', '.', $fullEntityClassName).'.dcm.xml';
}
$code = $exporter->exportClassMetadata($class);
if (!is_dir($dir = dirname($path)))
{
mkdir($dir, 0777, true);
}
file_put_contents($path, $code);
$this->runCommand('doctrine:generate-entities');
}
}

View File

@ -7,7 +7,7 @@ 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 Symfony\Framework\WebBundle\Util\Filesystem;
use Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand;
/*
* This file is part of the Symfony framework.
@ -24,18 +24,18 @@ use Symfony\Framework\WebBundle\Util\Filesystem;
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class GenerateProxiesDoctrineCommand extends DoctrineCommand
class GenerateProxiesDoctrineCommand extends GenerateProxiesCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('doctrine:generate-proxies')
->setDescription('Generates proxy classes for entity classes.')
;
parent::configure();
$this->setName('doctrine:generate-proxies');
$this->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to generate proxies for.');
}
/**
@ -43,28 +43,8 @@ class GenerateProxiesDoctrineCommand extends DoctrineCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dirs = array();
$bundleDirs = $this->container->getKernelService()->getBundleDirs();
foreach ($this->container->getKernelService()->getBundles() as $bundle)
{
$tmp = dirname(str_replace('\\', '/', get_class($bundle)));
$namespace = str_replace('/', '\\', dirname($tmp));
$class = basename($tmp);
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
if (isset($bundleDirs[$namespace]) && is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Entities'))
{
$dirs[] = $dir;
}
}
if (!is_dir($dir = $this->container->getParameter('kernel.cache_dir').'/doctrine/Proxies'))
{
mkdir($dir, 0777, true);
}
foreach ($dirs as $dir)
{
$this->runDoctrineCliTask('orm:generate-proxies', array('class-dir' => $dir));
}
return parent::execute($input, $output);
}
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace Symfony\Framework\DoctrineBundle\Command;
use Symfony\Components\Console\Input\InputOption;
use Symfony\Components\Console\Input\InputInterface;
use Symfony\Components\Console\Output\OutputInterface;
use Doctrine\ORM\Tools\EntityRepositoryGenerator;
/*
* 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.
*/
/**
* Command to generate repository classes for mapping information.
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class GenerateRepositoriesDoctrineCommand extends DoctrineCommand
{
protected function configure()
{
$this->setName('doctrine:generate-repositories');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$generator = new EntityRepositoryGenerator();
$kernel = $this->application->getKernel();
$bundleDirs = $kernel->getBundleDirs();
foreach ($kernel->getBundles() as $bundle)
{
$tmp = dirname(str_replace('\\', '/', get_class($bundle)));
$namespace = str_replace('/', '\\', dirname($tmp));
$class = basename($tmp);
if (isset($bundleDirs[$namespace]))
{
$destination = realpath($bundleDirs[$namespace].'/..');
if ($metadatas = $this->getBundleMetadatas($bundle))
{
$output->writeln(sprintf('Generating entity repositories for "<info>%s</info>"', $class));
foreach ($metadatas as $metadata)
{
if ($metadata->customRepositoryClassName)
{
$output->writeln(sprintf(' > generating <comment>%s</comment>', $metadata->customRepositoryClassName));
$generator->writeEntityRepositoryClass($metadata->customRepositoryClassName, $destination);
}
}
}
}
}
}
}

View File

@ -1,83 +0,0 @@
<?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;
/*
* 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.
*/
/**
* Import the initial mapping information for entities from an existing database
* into a bundle.
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class ImportMappingDoctrineCommand extends DoctrineCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('doctrine:import-mapping')
->setDescription('Import the initial mapping information for entities from an existing database.')
->addOption('connection', null, InputOption::PARAMETER_REQUIRED, 'The connection import from.')
->addOption('bundle', null, InputOption::PARAMETER_REQUIRED, 'The bundle to import the mapping information to.')
->addOption('type', null, InputOption::PARAMETER_OPTIONAL, 'The mapping format type to generate (defaults to xml).', 'xml')
;
}
/**
* @see Command
*
* @throws \InvalidArgumentException When the bundle doesn't end with Bundle (Example: "Bundle\MySampleBundle")
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (!preg_match('/Bundle$/', $bundle = $input->getOption('bundle')))
{
throw new \InvalidArgumentException('The bundle must end with Bundle.');
}
$dirs = $this->container->getKernelService()->getBundleDirs();
$tmp = str_replace('\\', '/', $bundle);
$namespace = str_replace('/', '\\', dirname($tmp));
$bundle = basename($tmp);
if (!isset($dirs[$namespace]))
{
throw new \InvalidArgumentException(sprintf('Could not find namespace "%s" for bundle "%s".', $namespace, $bundle));
}
$path = $dirs[$namespace].'/'.$bundle.'/Resources/config/doctrine/metadata';
if (!is_dir($path))
{
mkdir($path, 0777, true);
}
$this->em = $this->container->getService(sprintf('doctrine.orm.%s_entity_manager', $input->getOption('connection')));
$this->runCommand('doctrine:convert-mapping', array(
'--from' => 'database',
'--to' => $input->getOption('type'),
'--dest' => $path
)
);
}
}

View File

@ -1,118 +0,0 @@
<?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 Symfony\Framework\WebBundle\Util\Filesystem;
use Doctrine\Common\Cli\Configuration;
use Doctrine\Common\Cli\CliController as DoctrineCliController;
/*
* 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.
*/
/**
* Initialize a new Doctrine entity inside a bundle.
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class InitEntityDoctrineCommand extends DoctrineCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('doctrine:init-entity')
->setDescription('Initialize a new Doctrine entity inside a bundle.')
->addOption('bundle', null, InputOption::PARAMETER_REQUIRED, 'The bundle to initialize the entity in.')
->addOption('entity', null, InputOption::PARAMETER_REQUIRED, 'The entity class to initialize.')
->setHelp('
The <info>doctrine:init-entity</info> task initializes a new Doctrine entity inside a bundle:
<comment>php console doctrine:init-entity --bundle="Bundle\MyCustomBundle" --entity="User\Group"</comment>
The above would initialize a new entity in the following entity namespace <info>Bundle\MyCustomBundle\Entities\User\Group</info>.
You can now build your entities and update your database schema:
<comment>php console doctrine:build --entities --and-update-schema</comment>
Now you have a new entity and your database has been updated.
')
;
}
/**
* @see Command
*
* @throws \InvalidArgumentException When the bundle doesn't end with Bundle (Example: "Bundle\MySampleBundle")
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (!preg_match('/Bundle$/', $bundle = $input->getOption('bundle')))
{
throw new \InvalidArgumentException('The bundle name must end with Bundle. Example: "Bundle\MySampleBundle".');
}
$dirs = $this->container->getKernelService()->getBundleDirs();
$tmp = str_replace('\\', '/', $bundle);
$namespace = str_replace('/', '\\', dirname($tmp));
$bundle = basename($tmp);
if (!isset($dirs[$namespace]))
{
throw new \InvalidArgumentException(sprintf('Unable to initialize the bundle entity (%s not defined).', $namespace));
}
$entity = $input->getOption('entity');
$entityNamespace = $namespace.'\\'.$bundle.'\\Entities';
$fullEntityClassName = $entityNamespace.'\\'.$entity;
$tmp = str_replace('\\', '/', $fullEntityClassName);
$tmp = str_replace('/', '\\', dirname($tmp));
$className = basename($tmp);
$extends = null;
$path = $dirs[$namespace].'/'.$bundle.'/Resources/config/doctrine/metadata/'.str_replace('\\', '.', $fullEntityClassName).'.dcm.xml';
$xml = sprintf('<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="%s" table="%s">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
</entity>
</doctrine-mapping>',
$fullEntityClassName,
str_replace('\\', '_', strtolower($entity))
);
if (!is_dir($dir = dirname($path)))
{
mkdir($dir, 0777, true);
}
file_put_contents($path, $xml);
$this->runCommand('doctrine:build-entities');
}
}

View File

@ -7,9 +7,7 @@ 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 Symfony\Framework\WebBundle\Util\Filesystem;
use Doctrine\Common\Cli\Configuration;
use Doctrine\Common\Cli\CliController as DoctrineCliController;
use Doctrine\ORM\Tools\Console\Command\RunDqlCommand;
/*
* This file is part of the Symfony framework.
@ -28,20 +26,16 @@ use Doctrine\Common\Cli\CliController as DoctrineCliController;
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class RunDqlDoctrineCommand extends DoctrineCommand
class RunDqlDoctrineCommand extends RunDqlCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('doctrine:run-dql')
->setDescription('Executes arbitrary DQL directly from the command line.')
->addOption('dql', null, null, 'The DQL query to run.')
->addOption('depth', null, null, 'The depth to output the data to.')
->addOption('connection', null, null, 'The connection to use.')
;
parent::configure();
$this->setName('doctrine:run-dql');
$this->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to execute the DQL query on.');
}
/**
@ -49,9 +43,8 @@ class RunDqlDoctrineCommand extends DoctrineCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$options = $this->buildDoctrineCliTaskOptions($input, array(
'dql', 'depth'
));
$this->runDoctrineCliTask('orm:run-dql', $options);
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
return parent::execute($input, $output);
}
}

View File

@ -7,9 +7,7 @@ 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 Symfony\Framework\WebBundle\Util\Filesystem;
use Doctrine\Common\Cli\Configuration;
use Doctrine\Common\Cli\CliController as DoctrineCliController;
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
/*
* This file is part of the Symfony framework.
@ -28,31 +26,25 @@ use Doctrine\Common\Cli\CliController as DoctrineCliController;
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class RunSqlDoctrineCommand extends DoctrineCommand
class RunSqlDoctrineCommand extends RunSqlCommand
{
/**
* @see Command
* @see RunSqlCommand
*/
protected function configure()
{
$this
->setName('doctrine:run-sql')
->setDescription('Executes arbitrary SQL from a file or directly from the command line.')
->addOption('sql', null, null, 'The SQL query to run.')
->addOption('file', null, null, 'Path to a SQL file to run.')
->addOption('depth', null, null, 'The depth to output the data to.')
->addOption('connection', null, null, 'The connection to use.')
;
parent::configure();
$this->setName('doctrine:run-sql');
$this->addOption('connection', null, null, 'The connection to execute the SQL query on.');
}
/**
* @see Command
* @see RunSqlCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$options = $this->buildDoctrineCliTaskOptions($input, array(
'sql', 'file', 'depth'
));
$this->runDoctrineCliTask('dbal:run-sql', $options);
DoctrineCommand::setApplicationConnection($this->application, $input->getOption('connection'));
return parent::execute($input, $output);
}
}

View File

@ -1,88 +0,0 @@
<?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 Symfony\Framework\WebBundle\Util\Filesystem;
use Doctrine\Common\Cli\Configuration;
use Doctrine\Common\Cli\CliController as DoctrineCliController;
/*
* 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.
*/
/**
* Create, drop, and update your Doctrine ORM schema in the DBMS.
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class SchemaToolDoctrineCommand extends DoctrineCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('doctrine:schema-tool')
->setDescription('Processes the schema and either apply it directly on EntityManager or generate the SQL output.')
->addOption('create', null, null, 'Create your database schema.')
->addOption('drop', null, null, 'Drop your database schema.')
->addOption('update', null, null, 'Update your database schema and add anything that is not in your database but exists in your schema.')
->addOption('complete-update', null, null, 'Complete update and drop anything that is not in your schema.')
->addOption('re-create', null, null, 'Drop and re-create your database schema.')
->addOption('dump-sql', null, null, 'Dump the SQL instead of executing it.')
->addOption('connection', null, null, 'The connection to use.')
;
}
/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$options = $this->buildDoctrineCliTaskOptions($input, array(
'create', 'drop', 'update', 'complete-update', 're-create', 'dump-sql'
));
$entityDirs = $this->container->getParameter('doctrine.orm.entity_dirs');
$options['class-dir'] = implode(', ', $entityDirs);
$found = false;
$ems = $this->getDoctrineEntityManagers();
foreach ($ems as $name => $em)
{
if ($input->getOption('connection') && $name !== $input->getOption('connection'))
{
continue;
}
$this->em = $em;
$this->runDoctrineCliTask('orm:schema-tool', $options);
$found = true;
}
if ($found === false)
{
if ($input->getOption('connection'))
{
$output->writeln(sprintf('<error>Could not find a connection named <comment>%s</comment></error>', $input->getOption('connection')));
}
else
{
$output->writeln(sprintf('<error>Could not find any configured connections</error>', $input->getOption('connection')));
}
}
}
}

View File

@ -0,0 +1,50 @@
<?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\SchemaTool\UpdateCommand;
/*
* 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.
*/
/**
* Command to update the database schema for a set of classes based on their mappings.
*
* @package Symfony
* @subpackage Framework_DoctrineBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class UpdateSchemaDoctrineCommand extends UpdateCommand
{
/**
* @see Command
*/
protected function configure()
{
parent::configure();
$this->setName('doctrine:update-schema');
$this->addOption('em', null, InputOption::PARAMETER_OPTIONAL, 'The entity manager to update the schema for.');
}
/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
parent::execute($input, $output);
}
}

View File

@ -220,8 +220,10 @@ class DoctrineExtension extends LoaderExtension
if (is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Entities'))
{
$type = 'annotation';
if ($type === false)
{
$type = 'annotation';
}
$aliasMap[$class] = $namespace.'\\'.$class.'\\Entities';
}