2010-04-08 07:47:42 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
2012-05-26 16:37:00 +04:00
|
|
|
* and is licensed under the MIT license. For more information, see
|
2010-04-08 07:47:42 +04:00
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Tools\Console\Command;
|
|
|
|
|
2012-12-14 16:49:05 +04:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console;
|
|
|
|
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
|
|
|
|
use Doctrine\ORM\Tools\ConvertDoctrine1Schema;
|
|
|
|
use Doctrine\ORM\Tools\EntityGenerator;
|
2012-10-22 03:58:31 +04:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
2010-04-08 07:47:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Command to convert a Doctrine 1 schema to a Doctrine 2 mapping file.
|
|
|
|
*
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
*/
|
2012-10-22 03:58:31 +04:00
|
|
|
class ConvertDoctrine1SchemaCommand extends Command
|
2010-04-08 07:47:42 +04:00
|
|
|
{
|
2010-06-20 02:28:20 +04:00
|
|
|
/**
|
2012-12-14 16:49:05 +04:00
|
|
|
* @var EntityGenerator|null
|
2010-06-20 02:28:20 +04:00
|
|
|
*/
|
|
|
|
private $entityGenerator = null;
|
|
|
|
|
|
|
|
/**
|
2012-12-14 16:49:05 +04:00
|
|
|
* @var ClassMetadataExporter|null
|
2010-06-20 02:28:20 +04:00
|
|
|
*/
|
|
|
|
private $metadataExporter = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return EntityGenerator
|
|
|
|
*/
|
|
|
|
public function getEntityGenerator()
|
|
|
|
{
|
|
|
|
if ($this->entityGenerator == null) {
|
|
|
|
$this->entityGenerator = new EntityGenerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->entityGenerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param EntityGenerator $entityGenerator
|
2012-12-14 16:49:05 +04:00
|
|
|
*
|
|
|
|
* @return void
|
2010-06-20 02:28:20 +04:00
|
|
|
*/
|
|
|
|
public function setEntityGenerator(EntityGenerator $entityGenerator)
|
|
|
|
{
|
|
|
|
$this->entityGenerator = $entityGenerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ClassMetadataExporter
|
|
|
|
*/
|
|
|
|
public function getMetadataExporter()
|
|
|
|
{
|
|
|
|
if ($this->metadataExporter == null) {
|
|
|
|
$this->metadataExporter = new ClassMetadataExporter();
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-06-20 02:28:20 +04:00
|
|
|
return $this->metadataExporter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ClassMetadataExporter $metadataExporter
|
2012-12-14 16:49:05 +04:00
|
|
|
*
|
|
|
|
* @return void
|
2010-06-20 02:28:20 +04:00
|
|
|
*/
|
|
|
|
public function setMetadataExporter(ClassMetadataExporter $metadataExporter)
|
|
|
|
{
|
|
|
|
$this->metadataExporter = $metadataExporter;
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-04-08 07:47:42 +04:00
|
|
|
/**
|
2012-12-14 16:49:05 +04:00
|
|
|
* {@inheritdoc}
|
2010-04-08 07:47:42 +04:00
|
|
|
*/
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('orm:convert-d1-schema')
|
|
|
|
->setDescription('Converts Doctrine 1.X schema into a Doctrine 2.X schema.')
|
|
|
|
->setDefinition(array(
|
|
|
|
new InputArgument(
|
|
|
|
'from-path', InputArgument::REQUIRED, 'The path of Doctrine 1.X schema information.'
|
|
|
|
),
|
|
|
|
new InputArgument(
|
|
|
|
'to-type', InputArgument::REQUIRED, 'The destination Doctrine 2.X mapping type.'
|
|
|
|
),
|
|
|
|
new InputArgument(
|
|
|
|
'dest-path', InputArgument::REQUIRED,
|
|
|
|
'The path to generate your Doctrine 2.X mapping information.'
|
|
|
|
),
|
|
|
|
new InputOption(
|
2010-11-28 11:59:23 +03:00
|
|
|
'from', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
|
2010-04-08 07:47:42 +04:00
|
|
|
'Optional paths of Doctrine 1.X schema information.',
|
|
|
|
array()
|
|
|
|
),
|
|
|
|
new InputOption(
|
2010-11-28 11:59:23 +03:00
|
|
|
'extend', null, InputOption::VALUE_OPTIONAL,
|
2010-04-08 07:47:42 +04:00
|
|
|
'Defines a base class to be extended by generated entity classes.'
|
|
|
|
),
|
|
|
|
new InputOption(
|
2010-11-28 11:59:23 +03:00
|
|
|
'num-spaces', null, InputOption::VALUE_OPTIONAL,
|
2010-04-08 07:47:42 +04:00
|
|
|
'Defines the number of indentation spaces', 4
|
|
|
|
)
|
|
|
|
))
|
|
|
|
->setHelp(<<<EOT
|
|
|
|
Converts Doctrine 1.X schema into a Doctrine 2.X schema.
|
|
|
|
EOT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-12-14 16:49:05 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-10-22 03:58:31 +04:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
2010-04-08 07:47:42 +04:00
|
|
|
{
|
|
|
|
// Process source directories
|
|
|
|
$fromPaths = array_merge(array($input->getArgument('from-path')), $input->getOption('from'));
|
|
|
|
|
2010-06-20 02:28:20 +04:00
|
|
|
// Process destination directory
|
|
|
|
$destPath = realpath($input->getArgument('dest-path'));
|
|
|
|
|
|
|
|
$toType = $input->getArgument('to-type');
|
|
|
|
$extend = $input->getOption('extend');
|
|
|
|
$numSpaces = $input->getOption('num-spaces');
|
|
|
|
|
2012-10-22 03:58:31 +04:00
|
|
|
$this->convertDoctrine1Schema($fromPaths, $destPath, $toType, $numSpaces, $extend, $output);
|
2010-06-20 02:28:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-14 16:49:05 +04:00
|
|
|
* @param array $fromPaths
|
|
|
|
* @param string $destPath
|
|
|
|
* @param string $toType
|
|
|
|
* @param int $numSpaces
|
|
|
|
* @param string|null $extend
|
2012-10-22 03:58:31 +04:00
|
|
|
* @param OutputInterface $output
|
2012-12-14 16:49:05 +04:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException
|
2010-06-20 02:28:20 +04:00
|
|
|
*/
|
2012-10-22 03:58:31 +04:00
|
|
|
public function convertDoctrine1Schema(array $fromPaths, $destPath, $toType, $numSpaces, $extend, OutputInterface $output)
|
2010-06-20 02:28:20 +04:00
|
|
|
{
|
2010-04-08 07:47:42 +04:00
|
|
|
foreach ($fromPaths as &$dirName) {
|
|
|
|
$dirName = realpath($dirName);
|
|
|
|
|
|
|
|
if ( ! file_exists($dirName)) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not exist.", $dirName)
|
|
|
|
);
|
2012-10-22 03:58:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_readable($dirName)) {
|
2010-04-08 07:47:42 +04:00
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not have read permissions.", $dirName)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! file_exists($destPath)) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not exist.", $destPath)
|
|
|
|
);
|
2012-10-22 03:58:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_writable($destPath)) {
|
2010-04-08 07:47:42 +04:00
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not have write permissions.", $destPath)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-06-20 02:28:20 +04:00
|
|
|
$cme = $this->getMetadataExporter();
|
2010-04-08 07:47:42 +04:00
|
|
|
$exporter = $cme->getExporter($toType, $destPath);
|
|
|
|
|
|
|
|
if (strtolower($toType) === 'annotation') {
|
2010-06-20 02:28:20 +04:00
|
|
|
$entityGenerator = $this->getEntityGenerator();
|
2010-04-08 07:47:42 +04:00
|
|
|
$exporter->setEntityGenerator($entityGenerator);
|
|
|
|
|
2010-06-20 02:28:20 +04:00
|
|
|
$entityGenerator->setNumSpaces($numSpaces);
|
2010-04-08 07:47:42 +04:00
|
|
|
|
2010-06-20 02:28:20 +04:00
|
|
|
if ($extend !== null) {
|
2010-04-08 07:47:42 +04:00
|
|
|
$entityGenerator->setClassToExtend($extend);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$converter = new ConvertDoctrine1Schema($fromPaths);
|
2010-04-15 04:39:51 +04:00
|
|
|
$metadata = $converter->getMetadata();
|
2010-04-08 07:47:42 +04:00
|
|
|
|
2010-05-14 20:33:17 +04:00
|
|
|
if ($metadata) {
|
2012-10-22 03:58:31 +04:00
|
|
|
$output->writeln('');
|
2010-04-08 07:47:42 +04:00
|
|
|
|
2010-04-15 04:39:51 +04:00
|
|
|
foreach ($metadata as $class) {
|
2012-10-22 03:58:31 +04:00
|
|
|
$output->writeln(sprintf('Processing entity "<info>%s</info>"', $class->name));
|
2010-04-08 07:47:42 +04:00
|
|
|
}
|
|
|
|
|
2010-04-15 04:39:51 +04:00
|
|
|
$exporter->setMetadata($metadata);
|
2010-04-08 07:47:42 +04:00
|
|
|
$exporter->export();
|
|
|
|
|
2012-10-22 03:58:31 +04:00
|
|
|
$output->writeln(PHP_EOL . sprintf(
|
2010-04-08 07:47:42 +04:00
|
|
|
'Converting Doctrine 1.X schema to "<info>%s</info>" mapping type in "<info>%s</info>"', $toType, $destPath
|
|
|
|
));
|
|
|
|
} else {
|
2012-10-22 03:58:31 +04:00
|
|
|
$output->writeln('No Metadata Classes to process.');
|
2010-04-08 07:47:42 +04:00
|
|
|
}
|
|
|
|
}
|
2010-08-23 10:21:41 +04:00
|
|
|
}
|