diff --git a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/AbstractCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/AbstractCommand.php new file mode 100644 index 000000000..a3333c8e0 --- /dev/null +++ b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/AbstractCommand.php @@ -0,0 +1,88 @@ +. +*/ + +namespace Doctrine\ORM\Tools\Console\Command\SchemaTool; + +use Symfony\Components\Console\Input\InputArgument, + Symfony\Components\Console\Input\InputOption, + Symfony\Components\Console\Input\InputInterface, + Symfony\Components\Console\Output\OutputInterface, + Symfony\Components\Console\Command\Command, + Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper, + Doctrine\ORM\Tools\SchemaTool; + +abstract class AbstractCommand extends Command +{ + /** + * @param InputInterface $input + * @param OutputInterface $output + * @param SchemaTool $schemaTool + * @param array $metadatas + */ + abstract protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas); + + /** + * @see Console\Command\Command + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $emHelper = $this->getHelper('em'); + + /* @var $em \Doctrine\ORM\EntityManager */ + $em = $emHelper->getEntityManager(); + + $reader = new \Doctrine\ORM\Tools\ClassMetadataReader(); + $reader->setEntityManager($em); + $reader->addMappingSource($em->getConfiguration()->getMetadataDriverImpl()); + + // Process source directories + if ($emHelper->hasAdditionalMappingPathInformation()) { + + foreach ($emHelper->getMappingPaths() as $dirName) { + $dirName = realpath($dirName); + + if ( ! file_exists($dirName)) { + throw new \InvalidArgumentException( + sprintf("Mapping directory '%s' does not exist.", $dirName) + ); + } else if ( ! is_readable($dirName)) { + throw new \InvalidArgumentException( + sprintf("Mapping directory '%s' does not have read permissions.", $dirName) + ); + } + + $reader->addMappingSource($dirName); + } + } + + // Retrieving ClassMetadatas + $metadatas = $reader->getMetadatas(); + + if ( ! empty($metadatas)) { + // Create SchemaTool + $tool = new \Doctrine\ORM\Tools\SchemaTool($em); + + $this->executeSchemaCommand($input, $output, $tool, $metadatas); + } else { + $output->write('No Metadata Classes to process.' . PHP_EOL); + } + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/CreateCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/CreateCommand.php index fd724c2df..06332a3eb 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/CreateCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/CreateCommand.php @@ -23,7 +23,9 @@ namespace Doctrine\ORM\Tools\Console\Command\SchemaTool; use Symfony\Components\Console\Input\InputArgument, Symfony\Components\Console\Input\InputOption, - Symfony\Components\Console; + Symfony\Components\Console\Input\InputInterface, + Symfony\Components\Console\Output\OutputInterface, + Doctrine\ORM\Tools\SchemaTool; /** * Command to create the database schema for a set of classes based on their mappings. @@ -37,7 +39,7 @@ use Symfony\Components\Console\Input\InputArgument, * @author Jonathan Wage * @author Roman Borschel */ -class CreateCommand extends Console\Command\Command +class CreateCommand extends AbstractCommand { /** * @see Console\Command\Command @@ -50,14 +52,6 @@ class CreateCommand extends Console\Command\Command 'Processes the schema and either create it directly on EntityManager Storage Connection or generate the SQL output.' ) ->setDefinition(array( - new InputArgument( - 'from-path', InputArgument::REQUIRED, 'The path of mapping information.' - ), - new InputOption( - 'from', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY, - 'Optional paths of mapping information.', - array() - ), new InputOption( 'dump-sql', null, InputOption::PARAMETER_NONE, 'Instead of try to apply generated SQLs into EntityManager Storage Connection, output them.' @@ -69,52 +63,15 @@ EOT ); } - /** - * @see Console\Command\Command - */ - protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) + protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { - $em = $this->getHelper('em')->getEntityManager(); - - $reader = new \Doctrine\ORM\Tools\ClassMetadataReader(); - $reader->setEntityManager($em); - - // Process source directories - $fromPaths = array_merge(array($input->getArgument('from-path')), $input->getOption('from')); - - foreach ($fromPaths as $dirName) { - $dirName = realpath($dirName); - - if ( ! file_exists($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not exist.", $dirName) - ); - } else if ( ! is_readable($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not have read permissions.", $dirName) - ); - } - - $reader->addMappingSource($dirName); - } - - // Retrieving ClassMetadatas - $metadatas = $reader->getMetadatas(); - - if ( ! empty($metadatas)) { - // Create SchemaTool - $tool = new \Doctrine\ORM\Tools\SchemaTool($em); - - if ($input->getOption('dump-sql') === null) { - $sqls = $tool->getCreateSchemaSql($metadatas); - $output->write(implode(';' . PHP_EOL, $sqls)); - } else { - $output->write('Creating database schema...' . PHP_EOL); - $tool->createSchema($metadatas); - $output->write('Database schema created successfully!' . PHP_EOL); - } + if ($input->getOption('dump-sql') === true) { + $sqls = $schemaTool->getCreateSchemaSql($metadatas); + $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL); } else { - $output->write('No Metadata Classes to process.' . PHP_EOL); + $output->write('Creating database schema...' . PHP_EOL); + $schemaTool->createSchema($metadatas); + $output->write('Database schema created successfully!' . PHP_EOL); } } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/DropCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/DropCommand.php index 9bcf280e7..1c6d0c57a 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/DropCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/DropCommand.php @@ -23,7 +23,9 @@ namespace Doctrine\ORM\Tools\Console\Command\SchemaTool; use Symfony\Components\Console\Input\InputArgument, Symfony\Components\Console\Input\InputOption, - Symfony\Components\Console; + Symfony\Components\Console\Input\InputInterface, + Symfony\Components\Console\Output\OutputInterface, + Doctrine\ORM\Tools\SchemaTool; /** * Command to drop the database schema for a set of classes based on their mappings. @@ -37,7 +39,7 @@ use Symfony\Components\Console\Input\InputArgument, * @author Jonathan Wage * @author Roman Borschel */ -class DropCommand extends Console\Command\Command +class DropCommand extends AbstractCommand { /** * @see Console\Command\Command @@ -50,14 +52,6 @@ class DropCommand extends Console\Command\Command 'Processes the schema and either drop the database schema of EntityManager Storage Connection or generate the SQL output.' ) ->setDefinition(array( - new InputArgument( - 'from-path', InputArgument::REQUIRED, 'The path of mapping information.' - ), - new InputOption( - 'from', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY, - 'Optional paths of mapping information.', - array() - ), new InputOption( 'dump-sql', null, InputOption::PARAMETER_NONE, 'Instead of try to apply generated SQLs into EntityManager Storage Connection, output them.' @@ -70,52 +64,15 @@ EOT ); } - /** - * @see Console\Command\Command - */ - protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) + protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { - $em = $this->getHelper('em')->getEntityManager(); - - $reader = new \Doctrine\ORM\Tools\ClassMetadataReader(); - $reader->setEntityManager($em); - - // Process source directories - $fromPaths = array_merge(array($input->getArgument('from-path')), $input->getOption('from')); - - foreach ($fromPaths as $dirName) { - $dirName = realpath($dirName); - - if ( ! file_exists($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not exist.", $dirName) - ); - } else if ( ! is_readable($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not have read permissions.", $dirName) - ); - } - - $reader->addMappingSource($dirName); - } - - // Retrieving ClassMetadatas - $metadatas = $reader->getMetadatas(); - - if ( ! empty($metadatas)) { - // Create SchemaTool - $tool = new \Doctrine\ORM\Tools\SchemaTool($em); - - if ($input->getOption('dump-sql') === null) { - $sqls = $tool->getDropSchemaSql($metadatas); - $output->write(implode(';' . PHP_EOL, $sqls)); - } else { - $output->write('Dropping database schema...' . PHP_EOL); - $tool->dropSchema($metadatas); - $output->write('Database schema dropped successfully!' . PHP_EOL); - } + if ($input->getOption('dump-sql') === true) { + $sqls = $schemaTool->getDropSchemaSql($metadatas); + $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL); } else { - $output->write('No Metadata Classes to process.' . PHP_EOL); + $output->write('Dropping database schema...' . PHP_EOL); + $schemaTool->dropSchema($metadatas); + $output->write('Database schema dropped successfully!' . PHP_EOL); } } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php index 7d85bb6a9..32d8fd0f7 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php @@ -23,7 +23,9 @@ namespace Doctrine\ORM\Tools\Console\Command\SchemaTool; use Symfony\Components\Console\Input\InputArgument, Symfony\Components\Console\Input\InputOption, - Symfony\Components\Console; + Symfony\Components\Console\Input\InputInterface, + Symfony\Components\Console\Output\OutputInterface, + Doctrine\ORM\Tools\SchemaTool; /** * Command to update the database schema for a set of classes based on their mappings. @@ -37,7 +39,7 @@ use Symfony\Components\Console\Input\InputArgument, * @author Jonathan Wage * @author Roman Borschel */ -class UpdateCommand extends Console\Command\Command +class UpdateCommand extends AbstractCommand { /** * @see Console\Command\Command @@ -50,14 +52,6 @@ class UpdateCommand extends Console\Command\Command 'Processes the schema and either update the database schema of EntityManager Storage Connection or generate the SQL output.' ) ->setDefinition(array( - new InputArgument( - 'from-path', InputArgument::REQUIRED, 'The path of mapping information.' - ), - new InputOption( - 'from', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY, - 'Optional paths of mapping information.', - array() - ), new InputOption( 'complete', null, InputOption::PARAMETER_NONE, 'If defined, all assets of the database which are not relevant to the current metadata will be dropped.' @@ -75,55 +69,18 @@ EOT ); } - /** - * @see Console\Command\Command - */ - protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) + protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { - $em = $this->getHelper('em')->getEntityManager(); - - $reader = new \Doctrine\ORM\Tools\ClassMetadataReader(); - $reader->setEntityManager($em); - - // Process source directories - $fromPaths = array_merge(array($input->getArgument('from-path')), $input->getOption('from')); - - foreach ($fromPaths as $dirName) { - $dirName = realpath($dirName); - - if ( ! file_exists($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not exist.", $dirName) - ); - } else if ( ! is_readable($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not have read permissions.", $dirName) - ); - } - - $reader->addMappingSource($dirName); - } - // Defining if update is complete or not (--complete not defined means $saveMode = true) - $saveMode = ($input->getOption('complete') === null); + $saveMode = ($input->getOption('complete') === true); - // Retrieving ClassMetadatas - $metadatas = $reader->getMetadatas(); - - if ( ! empty($metadatas)) { - // Create SchemaTool - $tool = new \Doctrine\ORM\Tools\SchemaTool($em); - - if ($input->getOption('dump-sql') === null) { - $sqls = $tool->getUpdateSchemaSql($metadatas, $saveMode); - $output->write(implode(';' . PHP_EOL, $sqls)); - } else { - $output->write('Updating database schema...' . PHP_EOL); - $tool->updateSchema($metadatas, $saveMode); - $output->write('Database schema updated successfully!' . PHP_EOL); - } + if ($input->getOption('dump-sql') === true) { + $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode); + $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL); } else { - $output->write('No Metadata Classes to process.' . PHP_EOL); + $output->write('Updating database schema...' . PHP_EOL); + $schemaTool->updateSchema($metadatas, $saveMode); + $output->write('Database schema updated successfully!' . PHP_EOL); } } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php b/lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php index 2636006e3..e706d69ac 100644 --- a/lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php +++ b/lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php @@ -44,14 +44,20 @@ class EntityManagerHelper extends Helper */ protected $_em; + /** + * @var array + */ + protected $_mappingPaths = array(); + /** * Constructor * * @param Connection $connection Doctrine Database Connection */ - public function __construct(EntityManager $em) + public function __construct(EntityManager $em, $mappingPaths = array()) { $this->_em = $em; + $this->_mappingPaths = $mappingPaths; } /** @@ -64,6 +70,16 @@ class EntityManagerHelper extends Helper return $this->_em; } + public function hasAdditionalMappingPathInformation() + { + return count($this->_mappingPaths); + } + + public function getMappingPaths() + { + return $this->_mappingPaths; + } + /** * @see Helper */