ConvertDoctrine1SchemaCommand.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\ORM\Tools\Console\Command;
  22. use Symfony\Component\Console\Input\InputArgument,
  23. Symfony\Component\Console\Input\InputOption,
  24. Symfony\Component\Console,
  25. Doctrine\ORM\Tools\Export\ClassMetadataExporter,
  26. Doctrine\ORM\Tools\ConvertDoctrine1Schema,
  27. Doctrine\ORM\Tools\EntityGenerator;
  28. /**
  29. * Command to convert a Doctrine 1 schema to a Doctrine 2 mapping file.
  30. *
  31. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  32. * @link www.doctrine-project.org
  33. * @since 2.0
  34. * @version $Revision$
  35. * @author Benjamin Eberlei <kontakt@beberlei.de>
  36. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  37. * @author Jonathan Wage <jonwage@gmail.com>
  38. * @author Roman Borschel <roman@code-factory.org>
  39. */
  40. class ConvertDoctrine1SchemaCommand extends Console\Command\Command
  41. {
  42. /**
  43. * @var EntityGenerator
  44. */
  45. private $entityGenerator = null;
  46. /**
  47. * @var ClassMetadataExporter
  48. */
  49. private $metadataExporter = null;
  50. /**
  51. * @return EntityGenerator
  52. */
  53. public function getEntityGenerator()
  54. {
  55. if ($this->entityGenerator == null) {
  56. $this->entityGenerator = new EntityGenerator();
  57. }
  58. return $this->entityGenerator;
  59. }
  60. /**
  61. * @param EntityGenerator $entityGenerator
  62. */
  63. public function setEntityGenerator(EntityGenerator $entityGenerator)
  64. {
  65. $this->entityGenerator = $entityGenerator;
  66. }
  67. /**
  68. * @return ClassMetadataExporter
  69. */
  70. public function getMetadataExporter()
  71. {
  72. if ($this->metadataExporter == null) {
  73. $this->metadataExporter = new ClassMetadataExporter();
  74. }
  75. return $this->metadataExporter;
  76. }
  77. /**
  78. * @param ClassMetadataExporter $metadataExporter
  79. */
  80. public function setMetadataExporter(ClassMetadataExporter $metadataExporter)
  81. {
  82. $this->metadataExporter = $metadataExporter;
  83. }
  84. /**
  85. * @see Console\Command\Command
  86. */
  87. protected function configure()
  88. {
  89. $this
  90. ->setName('orm:convert-d1-schema')
  91. ->setDescription('Converts Doctrine 1.X schema into a Doctrine 2.X schema.')
  92. ->setDefinition(array(
  93. new InputArgument(
  94. 'from-path', InputArgument::REQUIRED, 'The path of Doctrine 1.X schema information.'
  95. ),
  96. new InputArgument(
  97. 'to-type', InputArgument::REQUIRED, 'The destination Doctrine 2.X mapping type.'
  98. ),
  99. new InputArgument(
  100. 'dest-path', InputArgument::REQUIRED,
  101. 'The path to generate your Doctrine 2.X mapping information.'
  102. ),
  103. new InputOption(
  104. 'from', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  105. 'Optional paths of Doctrine 1.X schema information.',
  106. array()
  107. ),
  108. new InputOption(
  109. 'extend', null, InputOption::VALUE_OPTIONAL,
  110. 'Defines a base class to be extended by generated entity classes.'
  111. ),
  112. new InputOption(
  113. 'num-spaces', null, InputOption::VALUE_OPTIONAL,
  114. 'Defines the number of indentation spaces', 4
  115. )
  116. ))
  117. ->setHelp(<<<EOT
  118. Converts Doctrine 1.X schema into a Doctrine 2.X schema.
  119. EOT
  120. );
  121. }
  122. /**
  123. * @see Console\Command\Command
  124. */
  125. protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
  126. {
  127. $em = $this->getHelper('em')->getEntityManager();
  128. // Process source directories
  129. $fromPaths = array_merge(array($input->getArgument('from-path')), $input->getOption('from'));
  130. // Process destination directory
  131. $destPath = realpath($input->getArgument('dest-path'));
  132. $toType = $input->getArgument('to-type');
  133. $extend = $input->getOption('extend');
  134. $numSpaces = $input->getOption('num-spaces');
  135. $this->convertDoctrine1Schema($em, $fromPaths, $destPath, $toType, $numSpaces, $extend, $output);
  136. }
  137. /**
  138. * @param \Doctrine\ORM\EntityManager $em
  139. * @param array $fromPaths
  140. * @param string $destPath
  141. * @param string $toType
  142. * @param int $numSpaces
  143. * @param string|null $extend
  144. * @param Console\Output\OutputInterface $output
  145. */
  146. public function convertDoctrine1Schema($em, $fromPaths, $destPath, $toType, $numSpaces, $extend, $output)
  147. {
  148. foreach ($fromPaths as &$dirName) {
  149. $dirName = realpath($dirName);
  150. if ( ! file_exists($dirName)) {
  151. throw new \InvalidArgumentException(
  152. sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not exist.", $dirName)
  153. );
  154. } else if ( ! is_readable($dirName)) {
  155. throw new \InvalidArgumentException(
  156. sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not have read permissions.", $dirName)
  157. );
  158. }
  159. }
  160. if ( ! file_exists($destPath)) {
  161. throw new \InvalidArgumentException(
  162. sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not exist.", $destPath)
  163. );
  164. } else if ( ! is_writable($destPath)) {
  165. throw new \InvalidArgumentException(
  166. sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not have write permissions.", $destPath)
  167. );
  168. }
  169. $cme = $this->getMetadataExporter();
  170. $exporter = $cme->getExporter($toType, $destPath);
  171. if (strtolower($toType) === 'annotation') {
  172. $entityGenerator = $this->getEntityGenerator();
  173. $exporter->setEntityGenerator($entityGenerator);
  174. $entityGenerator->setNumSpaces($numSpaces);
  175. if ($extend !== null) {
  176. $entityGenerator->setClassToExtend($extend);
  177. }
  178. }
  179. $converter = new ConvertDoctrine1Schema($fromPaths);
  180. $metadata = $converter->getMetadata();
  181. if ($metadata) {
  182. $output->write(PHP_EOL);
  183. foreach ($metadata as $class) {
  184. $output->write(sprintf('Processing entity "<info>%s</info>"', $class->name) . PHP_EOL);
  185. }
  186. $exporter->setMetadata($metadata);
  187. $exporter->export();
  188. $output->write(PHP_EOL . sprintf(
  189. 'Converting Doctrine 1.X schema to "<info>%s</info>" mapping type in "<info>%s</info>"', $toType, $destPath
  190. ));
  191. } else {
  192. $output->write('No Metadata Classes to process.' . PHP_EOL);
  193. }
  194. }
  195. }