public function testTest()
 {
     if (!class_exists('Symfony\\Component\\Yaml\\Yaml', true)) {
         $this->markTestSkipped('Please install Symfony YAML Component into the include path of your PHP installation.');
     }
     $cme = new ClassMetadataExporter();
     $converter = new ConvertDoctrine1Schema(__DIR__ . '/doctrine1schema');
     $exporter = $cme->getExporter('yml', __DIR__ . '/convert');
     $exporter->setOverwriteExistingFiles(true);
     $exporter->setMetadata($converter->getMetadata());
     $exporter->export();
     $this->assertTrue(file_exists(__DIR__ . '/convert/User.dcm.yml'));
     $this->assertTrue(file_exists(__DIR__ . '/convert/Profile.dcm.yml'));
     $metadataDriver = new \Doctrine\ORM\Mapping\Driver\YamlDriver(__DIR__ . '/convert');
     $em = $this->_createEntityManager($metadataDriver);
     $cmf = new DisconnectedClassMetadataFactory();
     $cmf->setEntityManager($em);
     $metadata = $cmf->getAllMetadata();
     $profileClass = $cmf->getMetadataFor('Profile');
     $userClass = $cmf->getMetadataFor('User');
     $this->assertEquals(2, count($metadata));
     $this->assertEquals('Profile', $profileClass->name);
     $this->assertEquals('User', $userClass->name);
     $this->assertEquals(4, count($profileClass->fieldMappings));
     $this->assertEquals(5, count($userClass->fieldMappings));
     $this->assertEquals('text', $userClass->fieldMappings['clob']['type']);
     $this->assertEquals('test_alias', $userClass->fieldMappings['theAlias']['columnName']);
     $this->assertEquals('theAlias', $userClass->fieldMappings['theAlias']['fieldName']);
     $this->assertEquals('Profile', $profileClass->associationMappings['User']['sourceEntity']);
     $this->assertEquals('User', $profileClass->associationMappings['User']['targetEntity']);
     $this->assertEquals('username', $userClass->table['uniqueConstraints']['username']['columns'][0]);
 }
 public function testTest()
 {
     $cme = new ClassMetadataExporter();
     $converter = new ConvertDoctrine1Schema(__DIR__ . '/doctrine1schema');
     $exporter = $cme->getExporter('yml', __DIR__ . '/convert');
     $exporter->setMetadatas($converter->getMetadatas());
     $exporter->export();
     $this->assertTrue(file_exists(__DIR__ . '/convert/User.dcm.yml'));
     $this->assertTrue(file_exists(__DIR__ . '/convert/Profile.dcm.yml'));
     $cme->addMappingSource(__DIR__ . '/convert');
     $metadatas = $cme->getMetadatas();
     $this->assertEquals(2, count($metadatas));
     $this->assertEquals('Profile', $metadatas['Profile']->name);
     $this->assertEquals('User', $metadatas['User']->name);
     $this->assertEquals(4, count($metadatas['Profile']->fieldMappings));
     $this->assertEquals(5, count($metadatas['User']->fieldMappings));
     $this->assertEquals('text', $metadatas['User']->fieldMappings['clob']['type']);
     $this->assertEquals('test_alias', $metadatas['User']->fieldMappings['theAlias']['columnName']);
     $this->assertEquals('theAlias', $metadatas['User']->fieldMappings['theAlias']['fieldName']);
     $this->assertEquals('Profile', $metadatas['Profile']->associationMappings['User']->sourceEntityName);
     $this->assertEquals('User', $metadatas['Profile']->associationMappings['User']->targetEntityName);
     $this->assertEquals('username', $metadatas['User']->primaryTable['uniqueConstraints']['username']['columns'][0]);
     unlink(__DIR__ . '/convert/User.dcm.yml');
     unlink(__DIR__ . '/convert/Profile.dcm.yml');
     rmdir(__DIR__ . '/convert');
 }
 /**
  * @see Command
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $bundleClass = null;
     $bundleDirs = $this->container->getKernelService()->getBundleDirs();
     foreach ($this->container->getKernelService()->getBundles() as $bundle) {
         if (strpos(get_class($bundle), $input->getArgument('bundle')) !== false) {
             $tmp = dirname(str_replace('\\', '/', get_class($bundle)));
             $namespace = str_replace('/', '\\', dirname($tmp));
             $class = basename($tmp);
             if (isset($bundleDirs[$namespace])) {
                 $destPath = realpath($bundleDirs[$namespace]) . '/' . $class;
                 $bundleClass = $class;
                 break;
             }
         }
     }
     $type = $input->getArgument('mapping-type') ? $input->getArgument('mapping-type') : 'xml';
     if ($type === 'annotation') {
         $destPath .= '/Entities';
     } else {
         $destPath .= '/Resources/config/doctrine/metadata';
     }
     // adjust so file naming works
     if ($type === 'yaml') {
         $type = 'yml';
     }
     $cme = new ClassMetadataExporter();
     $exporter = $cme->getExporter($type);
     if ($type === 'annotation') {
         $entityGenerator = $this->getEntityGenerator();
         $exporter->setEntityGenerator($entityGenerator);
     }
     $converter = new ConvertDoctrine1Schema($input->getArgument('d1-schema'));
     $metadata = $converter->getMetadata();
     if ($metadata) {
         $output->writeln(sprintf('Converting Doctrine 1 schema "<info>%s</info>"', $input->getArgument('d1-schema')));
         foreach ($metadata as $class) {
             $className = $class->name;
             $class->name = $namespace . '\\' . $bundleClass . '\\Entities\\' . $className;
             if ($type === 'annotation') {
                 $path = $destPath . '/' . $className . '.php';
             } else {
                 $path = $destPath . '/' . str_replace('\\', '.', $class->name) . '.dcm.' . $type;
             }
             $output->writeln(sprintf('  > writing <comment>%s</comment>', $path));
             $code = $exporter->exportClassMetadata($class);
             if (!is_dir($dir = dirname($path))) {
                 mkdir($dir, 0777, true);
             }
             file_put_contents($path, $code);
         }
     } else {
         $output->writeln('Database does not have any mapping information.' . PHP_EOL, 'ERROR');
     }
 }
 public function run()
 {
     $arguments = $this->getArguments();
     $printer = $this->getPrinter();
     $printer->writeln(sprintf('Converting Doctrine 1 schema at "%s" to the "%s" format', $printer->format($arguments['from'], 'KEYWORD'), $printer->format($arguments['to'], 'KEYWORD')));
     $cme = new ClassMetadataExporter();
     $exporter = $cme->getExporter($arguments['to'], $arguments['dest']);
     if ($arguments['to'] === 'annotation') {
         $entityGenerator = new EntityGenerator();
         $exporter->setEntityGenerator($entityGenerator);
     }
     $converter = new ConvertDoctrine1Schema($arguments['from']);
     $metadatas = $converter->getMetadatas();
     foreach ($metadatas as $metadata) {
         $printer->writeln(sprintf('Processing entity "%s"', $printer->format($metadata->name, 'KEYWORD')));
     }
     $exporter->setMetadatas($metadatas);
     $exporter->export();
     $printer->writeln(sprintf('Writing Doctrine 2 mapping files to "%s"', $printer->format($arguments['dest'], 'KEYWORD')));
 }
 /**
  * @param \Doctrine\ORM\EntityManager $em
  * @param array $fromPaths
  * @param string $destPath
  * @param string $toType
  * @param int $numSpaces
  * @param string|null $extend
  * @param Console\Output\OutputInterface $output
  */
 public function convertDoctrine1Schema($em, $fromPaths, $destPath, $toType, $numSpaces, $extend, $output)
 {
     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));
         } else {
             if (!is_readable($dirName)) {
                 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));
     } else {
         if (!is_writable($destPath)) {
             throw new \InvalidArgumentException(sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not have write permissions.", $destPath));
         }
     }
     $cme = $this->getMetadataExporter();
     $exporter = $cme->getExporter($toType, $destPath);
     if (strtolower($toType) === 'annotation') {
         $entityGenerator = $this->getEntityGenerator();
         $exporter->setEntityGenerator($entityGenerator);
         $entityGenerator->setNumSpaces($numSpaces);
         if ($extend !== null) {
             $entityGenerator->setClassToExtend($extend);
         }
     }
     $converter = new ConvertDoctrine1Schema($fromPaths);
     $metadata = $converter->getMetadata();
     if ($metadata) {
         $output->write(PHP_EOL);
         foreach ($metadata as $class) {
             $output->write(sprintf('Processing entity "<info>%s</info>"', $class->name) . PHP_EOL);
         }
         $exporter->setMetadata($metadata);
         $exporter->export();
         $output->write(PHP_EOL . sprintf('Converting Doctrine 1.X schema to "<info>%s</info>" mapping type in "<info>%s</info>"', $toType, $destPath));
     } else {
         $output->write('No Metadata Classes to process.' . PHP_EOL);
     }
 }
 /**
  * @see Console\Command\Command
  */
 protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
 {
     $em = $this->getHelper('em')->getEntityManager();
     // 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("Doctrine 1.X schema directory '<info>%s</info>' does not exist.", $dirName));
         } else {
             if (!is_readable($dirName)) {
                 throw new \InvalidArgumentException(sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not have read permissions.", $dirName));
             }
         }
     }
     // Process destination directory
     $destPath = realpath($input->getArgument('dest-path'));
     if (!file_exists($destPath)) {
         throw new \InvalidArgumentException(sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not exist.", $destPath));
     } else {
         if (!is_writable($destPath)) {
             throw new \InvalidArgumentException(sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not have write permissions.", $destPath));
         }
     }
     $toType = $input->getArgument('to-type');
     $cme = new ClassMetadataExporter();
     $exporter = $cme->getExporter($toType, $destPath);
     if (strtolower($toType) === 'annotation') {
         $entityGenerator = new EntityGenerator();
         $exporter->setEntityGenerator($entityGenerator);
         $entityGenerator->setNumSpaces($input->getOption('num-spaces'));
         if (($extend = $input->getOption('extend')) !== null) {
             $entityGenerator->setClassToExtend($extend);
         }
     }
     $converter = new ConvertDoctrine1Schema($fromPaths);
     $metadata = $converter->getMetadata();
     if ($metadata) {
         $output->write(PHP_EOL);
         foreach ($metadata as $class) {
             $output->write(sprintf('Processing entity "<info>%s</info>"', $class->name) . PHP_EOL);
         }
         $exporter->setMetadata($metadata);
         $exporter->export();
         $output->write(PHP_EOL . sprintf('Converting Doctrine 1.X schema to "<info>%s</info>" mapping type in "<info>%s</info>"', $toType, $destPath));
     } else {
         $output->write('No Metadata Classes to process.' . PHP_EOL);
     }
 }