2009-09-30 08:35:43 +04:00
|
|
|
<?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\Cli\Tasks;
|
|
|
|
|
2009-10-08 02:14:13 +04:00
|
|
|
use Doctrine\ORM\Tools\Export\ClassMetadataExporter,
|
|
|
|
Doctrine\Common\DoctrineException;
|
2009-09-30 08:35:43 +04:00
|
|
|
|
2009-10-07 02:38:34 +04:00
|
|
|
if ( ! class_exists('sfYaml', false)) {
|
|
|
|
require_once __DIR__ . '/../../../../../vendor/sfYaml/sfYaml.class.php';
|
|
|
|
require_once __DIR__ . '/../../../../../vendor/sfYaml/sfYamlDumper.class.php';
|
|
|
|
require_once __DIR__ . '/../../../../../vendor/sfYaml/sfYamlInline.class.php';
|
|
|
|
require_once __DIR__ . '/../../../../../vendor/sfYaml/sfYamlParser.class.php';
|
|
|
|
}
|
|
|
|
|
2009-09-30 08:35:43 +04:00
|
|
|
/**
|
|
|
|
* CLI Task to convert your mapping information between the various formats
|
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
*/
|
|
|
|
class ConvertMappingTask extends AbstractTask
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function extendedHelp()
|
|
|
|
{
|
|
|
|
$printer = $this->getPrinter();
|
|
|
|
|
|
|
|
$printer->write('Task: ')->writeln('convert-mapping', 'KEYWORD')
|
|
|
|
->write('Synopsis: ');
|
|
|
|
$this->_writeSynopsis($printer);
|
|
|
|
|
|
|
|
$printer->writeln('Description: Convert mapping information between supported formats.')
|
|
|
|
->writeln('Options:')
|
2009-10-08 20:21:22 +04:00
|
|
|
->write('--from=<SOURCE>', 'REQ_ARG')
|
|
|
|
->writeln("\tThe path to the mapping information to convert from (yml, xml, php, annotation).")
|
|
|
|
->write('--from-database', 'REQ_ARG')
|
|
|
|
->writeln("\tUse this option if you wish to reverse engineer your database to a set of Doctrine mapping files.")
|
2009-09-30 08:35:43 +04:00
|
|
|
->write('--to=<TYPE>', 'REQ_ARG')
|
2009-10-08 20:21:22 +04:00
|
|
|
->writeln("\tThe format to convert to (yml, xml, php, annotation).")
|
2009-09-30 08:35:43 +04:00
|
|
|
->write(PHP_EOL)
|
|
|
|
->write('--dest=<PATH>', 'REQ_ARG')
|
|
|
|
->writeln("\tThe path to write the converted mapping information.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function basicHelp()
|
|
|
|
{
|
|
|
|
$this->_writeSynopsis($this->getPrinter());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _writeSynopsis($printer)
|
|
|
|
{
|
|
|
|
$printer->write('convert-mapping', 'KEYWORD')
|
2009-10-08 20:21:22 +04:00
|
|
|
->write(' --from=<SOURCE>', 'REQ_ARG')
|
2009-09-30 08:35:43 +04:00
|
|
|
->write(' --to=<TYPE>', 'REQ_ARG')
|
2009-10-08 20:21:22 +04:00
|
|
|
->write(' --dest=<PATH>', 'REQ_ARG')
|
|
|
|
->writeln(' --from-database', 'OPT_ARG');
|
2009-09-30 08:35:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function validate()
|
|
|
|
{
|
|
|
|
$args = $this->getArguments();
|
|
|
|
$printer = $this->getPrinter();
|
|
|
|
|
2009-10-08 20:21:22 +04:00
|
|
|
if (array_key_exists('from-database', $args)) {
|
|
|
|
$args['from'][0] = 'database';
|
|
|
|
$this->setArguments($args);
|
|
|
|
}
|
|
|
|
|
2009-10-05 08:11:29 +04:00
|
|
|
if (!(isset($args['from']) && isset($args['to']) && isset($args['dest']))) {
|
2009-10-08 20:21:22 +04:00
|
|
|
$printer->writeln('You must include a value for all three options: --from, --to and --dest', 'ERROR');
|
2009-09-30 08:35:43 +04:00
|
|
|
return false;
|
|
|
|
}
|
2009-10-05 22:19:00 +04:00
|
|
|
if ($args['to'] != 'annotation' && isset($args['extend'])) {
|
2009-10-23 02:19:17 +04:00
|
|
|
$printer->writeln('You can only use the --extend argument when converting to annoations.', 'ERROR');
|
2009-10-05 08:11:29 +04:00
|
|
|
return false;
|
|
|
|
}
|
2009-10-07 08:07:23 +04:00
|
|
|
if ($args['from'][0] == 'database') {
|
2009-10-30 03:15:53 +03:00
|
|
|
$em = $this->getEntityManager();
|
|
|
|
$config = $em->getConfiguration();
|
|
|
|
$config->setMetadataDriverImpl(new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($em->getConnection()->getSchemaManager()));
|
2009-10-07 08:07:23 +04:00
|
|
|
}
|
2009-09-30 08:35:43 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$printer = $this->getPrinter();
|
|
|
|
$args = $this->getArguments();
|
|
|
|
|
2009-10-05 08:11:29 +04:00
|
|
|
$cme = new ClassMetadataExporter();
|
2009-09-30 08:35:43 +04:00
|
|
|
|
2009-10-07 02:38:34 +04:00
|
|
|
// Get exporter and configure it
|
|
|
|
$exporter = $cme->getExporter($args['to'], $args['dest']);
|
2009-09-30 08:35:43 +04:00
|
|
|
|
2009-10-05 08:11:29 +04:00
|
|
|
if (isset($args['extend'])) {
|
|
|
|
$exporter->setClassToExtend($args['extend']);
|
|
|
|
}
|
|
|
|
if (isset($args['num-spaces'])) {
|
|
|
|
$exporter->setNumSpaces($args['num-spaces']);
|
|
|
|
}
|
2009-09-30 08:35:43 +04:00
|
|
|
|
2009-10-07 02:38:34 +04:00
|
|
|
$from = (array) $args['from'];
|
|
|
|
|
|
|
|
if ($this->_isDoctrine1Schema($from)) {
|
|
|
|
$printer->writeln('Converting Doctrine 1 schema to Doctrine 2 mapping files', 'INFO');
|
|
|
|
|
|
|
|
$converter = new \Doctrine\ORM\Tools\ConvertDoctrine1Schema($from);
|
2009-10-07 08:07:23 +04:00
|
|
|
$metadatas = $converter->getMetadatasFromSchema();
|
2009-10-07 02:38:34 +04:00
|
|
|
} else {
|
2009-10-07 08:07:23 +04:00
|
|
|
foreach ($from as $source) {
|
|
|
|
$sourceArg = $source;
|
|
|
|
|
|
|
|
$type = $this->_determineSourceType($sourceArg);
|
2009-10-08 02:14:13 +04:00
|
|
|
if ( ! $type) {
|
|
|
|
throw DoctrineException::invalidMappingSourceType($sourceArg);
|
|
|
|
}
|
2009-10-07 08:07:23 +04:00
|
|
|
$source = $this->_getSourceByType($type, $sourceArg);
|
2009-10-07 02:38:34 +04:00
|
|
|
|
2009-10-07 11:45:08 +04:00
|
|
|
$printer->writeln(
|
|
|
|
sprintf(
|
2009-10-08 02:14:13 +04:00
|
|
|
'Adding "%s" mapping source which contains the "%s" format',
|
|
|
|
$printer->format($sourceArg, 'KEYWORD'),
|
|
|
|
$printer->format($type, 'KEYWORD')
|
|
|
|
)
|
2009-10-07 11:45:08 +04:00
|
|
|
);
|
2009-09-30 08:35:43 +04:00
|
|
|
|
2009-10-07 08:07:23 +04:00
|
|
|
$cme->addMappingSource($source, $type);
|
2009-10-07 02:38:34 +04:00
|
|
|
}
|
2009-10-07 08:07:23 +04:00
|
|
|
$metadatas = $cme->getMetadatasForMappingSources();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($metadatas as $metadata) {
|
2009-10-07 11:45:08 +04:00
|
|
|
$printer->writeln(
|
|
|
|
sprintf(
|
|
|
|
'Processing entity "%s"',
|
|
|
|
$printer->format($metadata->name, 'KEYWORD')
|
|
|
|
)
|
|
|
|
);
|
2009-10-07 02:38:34 +04:00
|
|
|
}
|
|
|
|
|
2009-10-07 11:45:08 +04:00
|
|
|
$printer->writeln(
|
|
|
|
sprintf(
|
2009-10-08 02:14:13 +04:00
|
|
|
'Exporting "%s" mapping information to directory "%s"',
|
2009-10-07 11:45:08 +04:00
|
|
|
$printer->format($args['to'], 'KEYWORD'),
|
|
|
|
$printer->format($args['dest'], 'KEYWORD')
|
2009-10-08 02:14:13 +04:00
|
|
|
)
|
2009-10-07 11:45:08 +04:00
|
|
|
);
|
2009-10-07 08:07:23 +04:00
|
|
|
|
|
|
|
$exporter->setMetadatas($metadatas);
|
2009-09-30 08:35:43 +04:00
|
|
|
$exporter->export();
|
|
|
|
}
|
|
|
|
|
2009-10-07 02:38:34 +04:00
|
|
|
private function _isDoctrine1Schema(array $from)
|
|
|
|
{
|
2009-10-07 08:07:23 +04:00
|
|
|
$files = glob(current($from) . '/*.yml');
|
2009-10-07 02:38:34 +04:00
|
|
|
if ($files) {
|
|
|
|
$array = \sfYaml::load($files[0]);
|
|
|
|
$first = current($array);
|
2009-10-07 08:07:23 +04:00
|
|
|
// We're dealing with a Doctrine 1 schema if you have
|
|
|
|
// a columns index in the first model array
|
2009-10-07 02:38:34 +04:00
|
|
|
return isset($first['columns']);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-07 08:07:23 +04:00
|
|
|
private function _determineSourceType($source)
|
|
|
|
{
|
|
|
|
// If the --from=<VALUE> is a directory lets determine if it is
|
|
|
|
// annotations, yaml, xml, etc.
|
|
|
|
if (is_dir($source)) {
|
|
|
|
// Find the files in the directory
|
|
|
|
$files = glob($source . '/*.*');
|
|
|
|
if ( ! $files) {
|
2009-10-07 11:45:08 +04:00
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
sprintf('No mapping files found in "%s"', $source)
|
|
|
|
);
|
2009-10-07 08:07:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the contents of the first file
|
|
|
|
$contents = file_get_contents($files[0]);
|
|
|
|
|
|
|
|
// Check if it has a class definition in it for annotations
|
|
|
|
if (preg_match("/class (.*)/", $contents)) {
|
|
|
|
return 'annotation';
|
|
|
|
// Otherwise lets determine the type based on the extension of the
|
|
|
|
// first file in the directory (yml, xml, etc)
|
|
|
|
} else {
|
|
|
|
$info = pathinfo($files[0]);
|
|
|
|
return $info['extension'];
|
|
|
|
}
|
|
|
|
// Nothing special for database
|
|
|
|
} else if ($source == 'database') {
|
|
|
|
return 'database';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _getSourceByType($type, $source)
|
2009-09-30 08:35:43 +04:00
|
|
|
{
|
2009-10-07 08:07:23 +04:00
|
|
|
// If --from==database then the source is an instance of SchemaManager
|
|
|
|
// for the current EntityMAnager
|
|
|
|
if ($type == 'database') {
|
|
|
|
return $this->_em->getConnection()->getSchemaManager();
|
|
|
|
} else {
|
|
|
|
return $source;
|
|
|
|
}
|
2009-09-30 08:35:43 +04:00
|
|
|
}
|
|
|
|
}
|