Commit a7d07fcd authored by Benjamin Eberlei's avatar Benjamin Eberlei

DDC-502, DDC-507 - Refactored SchemaTool Commands

parent cb616956
<?php
/*
* $Id$
*
* 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 LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
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 '<info>%s</info>' does not exist.", $dirName)
);
} else if ( ! is_readable($dirName)) {
throw new \InvalidArgumentException(
sprintf("Mapping directory '<info>%s</info>' 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
...@@ -23,7 +23,9 @@ namespace Doctrine\ORM\Tools\Console\Command\SchemaTool; ...@@ -23,7 +23,9 @@ namespace Doctrine\ORM\Tools\Console\Command\SchemaTool;
use Symfony\Components\Console\Input\InputArgument, use Symfony\Components\Console\Input\InputArgument,
Symfony\Components\Console\Input\InputOption, 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. * 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, ...@@ -37,7 +39,7 @@ use Symfony\Components\Console\Input\InputArgument,
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class CreateCommand extends Console\Command\Command class CreateCommand extends AbstractCommand
{ {
/** /**
* @see Console\Command\Command * @see Console\Command\Command
...@@ -50,14 +52,6 @@ class CreateCommand extends 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.' 'Processes the schema and either create it directly on EntityManager Storage Connection or generate the SQL output.'
) )
->setDefinition(array( ->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( new InputOption(
'dump-sql', null, InputOption::PARAMETER_NONE, 'dump-sql', null, InputOption::PARAMETER_NONE,
'Instead of try to apply generated SQLs into EntityManager Storage Connection, output them.' 'Instead of try to apply generated SQLs into EntityManager Storage Connection, output them.'
...@@ -69,52 +63,15 @@ EOT ...@@ -69,52 +63,15 @@ EOT
); );
} }
/** protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas)
* @see Console\Command\Command
*/
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
{ {
$em = $this->getHelper('em')->getEntityManager(); if ($input->getOption('dump-sql') === true) {
$sqls = $schemaTool->getCreateSchemaSql($metadatas);
$reader = new \Doctrine\ORM\Tools\ClassMetadataReader(); $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL);
$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 '<info>%s</info>' does not exist.", $dirName)
);
} else if ( ! is_readable($dirName)) {
throw new \InvalidArgumentException(
sprintf("Mapping directory '<info>%s</info>' 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);
}
} else { } 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
...@@ -23,7 +23,9 @@ namespace Doctrine\ORM\Tools\Console\Command\SchemaTool; ...@@ -23,7 +23,9 @@ namespace Doctrine\ORM\Tools\Console\Command\SchemaTool;
use Symfony\Components\Console\Input\InputArgument, use Symfony\Components\Console\Input\InputArgument,
Symfony\Components\Console\Input\InputOption, 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. * 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, ...@@ -37,7 +39,7 @@ use Symfony\Components\Console\Input\InputArgument,
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class DropCommand extends Console\Command\Command class DropCommand extends AbstractCommand
{ {
/** /**
* @see Console\Command\Command * @see Console\Command\Command
...@@ -50,14 +52,6 @@ class DropCommand extends 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.' 'Processes the schema and either drop the database schema of EntityManager Storage Connection or generate the SQL output.'
) )
->setDefinition(array( ->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( new InputOption(
'dump-sql', null, InputOption::PARAMETER_NONE, 'dump-sql', null, InputOption::PARAMETER_NONE,
'Instead of try to apply generated SQLs into EntityManager Storage Connection, output them.' 'Instead of try to apply generated SQLs into EntityManager Storage Connection, output them.'
...@@ -70,52 +64,15 @@ EOT ...@@ -70,52 +64,15 @@ EOT
); );
} }
/** protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas)
* @see Console\Command\Command
*/
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
{ {
$em = $this->getHelper('em')->getEntityManager(); if ($input->getOption('dump-sql') === true) {
$sqls = $schemaTool->getDropSchemaSql($metadatas);
$reader = new \Doctrine\ORM\Tools\ClassMetadataReader(); $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL);
$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 '<info>%s</info>' does not exist.", $dirName)
);
} else if ( ! is_readable($dirName)) {
throw new \InvalidArgumentException(
sprintf("Mapping directory '<info>%s</info>' 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);
}
} else { } 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
...@@ -23,7 +23,9 @@ namespace Doctrine\ORM\Tools\Console\Command\SchemaTool; ...@@ -23,7 +23,9 @@ namespace Doctrine\ORM\Tools\Console\Command\SchemaTool;
use Symfony\Components\Console\Input\InputArgument, use Symfony\Components\Console\Input\InputArgument,
Symfony\Components\Console\Input\InputOption, 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. * 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, ...@@ -37,7 +39,7 @@ use Symfony\Components\Console\Input\InputArgument,
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class UpdateCommand extends Console\Command\Command class UpdateCommand extends AbstractCommand
{ {
/** /**
* @see Console\Command\Command * @see Console\Command\Command
...@@ -50,14 +52,6 @@ class UpdateCommand extends 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.' 'Processes the schema and either update the database schema of EntityManager Storage Connection or generate the SQL output.'
) )
->setDefinition(array( ->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( new InputOption(
'complete', null, InputOption::PARAMETER_NONE, 'complete', null, InputOption::PARAMETER_NONE,
'If defined, all assets of the database which are not relevant to the current metadata will be dropped.' 'If defined, all assets of the database which are not relevant to the current metadata will be dropped.'
...@@ -75,55 +69,18 @@ EOT ...@@ -75,55 +69,18 @@ EOT
); );
} }
/** protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas)
* @see Console\Command\Command
*/
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
{ {
$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 '<info>%s</info>' does not exist.", $dirName)
);
} else if ( ! is_readable($dirName)) {
throw new \InvalidArgumentException(
sprintf("Mapping directory '<info>%s</info>' does not have read permissions.", $dirName)
);
}
$reader->addMappingSource($dirName);
}
// Defining if update is complete or not (--complete not defined means $saveMode = true) // 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) { if ($input->getOption('dump-sql') === true) {
$sqls = $tool->getUpdateSchemaSql($metadatas, $saveMode); $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode);
$output->write(implode(';' . PHP_EOL, $sqls)); $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL);
} else {
$output->write('Updating database schema...' . PHP_EOL);
$tool->updateSchema($metadatas, $saveMode);
$output->write('Database schema updated successfully!' . PHP_EOL);
}
} else { } 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
...@@ -44,14 +44,20 @@ class EntityManagerHelper extends Helper ...@@ -44,14 +44,20 @@ class EntityManagerHelper extends Helper
*/ */
protected $_em; protected $_em;
/**
* @var array
*/
protected $_mappingPaths = array();
/** /**
* Constructor * Constructor
* *
* @param Connection $connection Doctrine Database Connection * @param Connection $connection Doctrine Database Connection
*/ */
public function __construct(EntityManager $em) public function __construct(EntityManager $em, $mappingPaths = array())
{ {
$this->_em = $em; $this->_em = $em;
$this->_mappingPaths = $mappingPaths;
} }
/** /**
...@@ -64,6 +70,16 @@ class EntityManagerHelper extends Helper ...@@ -64,6 +70,16 @@ class EntityManagerHelper extends Helper
return $this->_em; return $this->_em;
} }
public function hasAdditionalMappingPathInformation()
{
return count($this->_mappingPaths);
}
public function getMappingPaths()
{
return $this->_mappingPaths;
}
/** /**
* @see Helper * @see Helper
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment