. */ namespace Doctrine\ORM\Tools\Cli\Tasks; use Doctrine\ORM\Tools\Export\ClassMetadataExporter; 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'; } /** * 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 * @author Jonathan Wage * @author Roman Borschel */ 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:') ->write('--from=', 'REQ_ARG') ->writeln("\tThe path to the mapping information you are converting from.") ->write('--to=', 'REQ_ARG') ->writeln("\tThe format to convert to.") ->write(PHP_EOL) ->write('--dest=', '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') ->write(' --from=', 'REQ_ARG') ->write(' --to=', 'REQ_ARG') ->writeln(' --dest=', 'REQ_ARG'); } /** * @inheritdoc */ public function validate() { if ( ! parent::validate()) { return false; } $args = $this->getArguments(); $printer = $this->getPrinter(); if (!(isset($args['from']) && isset($args['to']) && isset($args['dest']))) { $printer->writeln('You must include a value for all four options: --from, --to and --dest', 'ERROR'); return false; } if ($args['to'] != 'annotation' && isset($args['extend'])) { $printer->writeln('You can only use the --extend argument when converting to annoations.'); return false; } return true; } public function run() { $printer = $this->getPrinter(); $args = $this->getArguments(); $cme = new ClassMetadataExporter(); // Get exporter and configure it $exporter = $cme->getExporter($args['to'], $args['dest']); if (isset($args['extend'])) { $exporter->setClassToExtend($args['extend']); } if (isset($args['num-spaces'])) { $exporter->setNumSpaces($args['num-spaces']); } $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); $exporter->setMetadatas($converter->getMetadatasFromSchema()); } else { foreach ($from as $path) { $type = $this->_determinePathType($path); $printer->writeln(sprintf('Adding %s mapping directory: "%s"', $type, $path), 'INFO'); $cme->addMappingDir($path, $type); } $exporter->setMetadatas($cme->getMetadatasForMappingDirectories()); } $printer->writeln(sprintf('Exporting %s mapping information to directory: "%s"', $args['to'], $args['dest']), 'INFO'); $exporter->export(); } private function _isDoctrine1Schema(array $from) { $files = glob($from[0] . '/*.yml'); if ($files) { $array = \sfYaml::load($files[0]); $first = current($array); return isset($first['columns']); } else { return false; } } private function _determinePathType($path) { $files = glob($path . '/*.*'); if (!$files) { throw new \InvalidArgumentException(sprintf('No schema mapping files found in "%s"', $path)); } $contents = file_get_contents($files[0]); if (preg_match("/class (.*)/", $contents)) { return 'annotation'; } else { $info = pathinfo($files[0]); return $info['extension']; } } }