ConvertMappingCommand.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\Console\MetadataFilter,
  26. Doctrine\ORM\Tools\Export\ClassMetadataExporter,
  27. Doctrine\ORM\Tools\EntityGenerator,
  28. Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
  29. /**
  30. * Command to convert your mapping information between the various formats.
  31. *
  32. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  33. * @link www.doctrine-project.org
  34. * @since 2.0
  35. * @version $Revision$
  36. * @author Benjamin Eberlei <kontakt@beberlei.de>
  37. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  38. * @author Jonathan Wage <jonwage@gmail.com>
  39. * @author Roman Borschel <roman@code-factory.org>
  40. */
  41. class ConvertMappingCommand extends Console\Command\Command
  42. {
  43. /**
  44. * @see Console\Command\Command
  45. */
  46. protected function configure()
  47. {
  48. $this
  49. ->setName('orm:convert-mapping')
  50. ->setDescription('Convert mapping information between supported formats.')
  51. ->setDefinition(array(
  52. new InputOption(
  53. 'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  54. 'A string pattern used to match entities that should be processed.'
  55. ),
  56. new InputArgument(
  57. 'to-type', InputArgument::REQUIRED, 'The mapping type to be converted.'
  58. ),
  59. new InputArgument(
  60. 'dest-path', InputArgument::REQUIRED,
  61. 'The path to generate your entities classes.'
  62. ),
  63. new InputOption(
  64. 'force', null, InputOption::VALUE_NONE,
  65. 'Force to overwrite existing mapping files.'
  66. ),
  67. new InputOption(
  68. 'from-database', null, null, 'Whether or not to convert mapping information from existing database.'
  69. ),
  70. new InputOption(
  71. 'extend', null, InputOption::VALUE_OPTIONAL,
  72. 'Defines a base class to be extended by generated entity classes.'
  73. ),
  74. new InputOption(
  75. 'num-spaces', null, InputOption::VALUE_OPTIONAL,
  76. 'Defines the number of indentation spaces', 4
  77. ),
  78. new InputOption(
  79. 'namespace', null, InputOption::VALUE_OPTIONAL,
  80. 'Defines a namespace for the generated entity classes, if converted from database.'
  81. ),
  82. ))
  83. ->setHelp(<<<EOT
  84. Convert mapping information between supported formats.
  85. This is an execute <info>one-time</info> command. It should not be necessary for
  86. you to call this method multiple times, escpecially when using the <comment>--from-database</comment>
  87. flag.
  88. Converting an existing databsae schema into mapping files only solves about 70-80%
  89. of the necessary mapping information. Additionally the detection from an existing
  90. database cannot detect inverse associations, inheritance types,
  91. entities with foreign keys as primary keys and many of the
  92. semantical operations on associations such as cascade.
  93. <comment>Hint:</comment> There is no need to convert YAML or XML mapping files to annotations
  94. every time you make changes. All mapping drivers are first class citizens
  95. in Doctrine 2 and can be used as runtime mapping for the ORM.
  96. EOT
  97. );
  98. }
  99. /**
  100. * @see Console\Command\Command
  101. */
  102. protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
  103. {
  104. $em = $this->getHelper('em')->getEntityManager();
  105. if ($input->getOption('from-database') === true) {
  106. $databaseDriver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
  107. $em->getConnection()->getSchemaManager()
  108. );
  109. $em->getConfiguration()->setMetadataDriverImpl(
  110. $databaseDriver
  111. );
  112. if (($namespace = $input->getOption('namespace')) !== null) {
  113. $databaseDriver->setNamespace($namespace);
  114. }
  115. }
  116. $cmf = new DisconnectedClassMetadataFactory();
  117. $cmf->setEntityManager($em);
  118. $metadata = $cmf->getAllMetadata();
  119. $metadata = MetadataFilter::filter($metadata, $input->getOption('filter'));
  120. // Process destination directory
  121. if ( ! is_dir($destPath = $input->getArgument('dest-path'))) {
  122. mkdir($destPath, 0777, true);
  123. }
  124. $destPath = realpath($destPath);
  125. if ( ! file_exists($destPath)) {
  126. throw new \InvalidArgumentException(
  127. sprintf("Mapping destination directory '<info>%s</info>' does not exist.", $input->getArgument('dest-path'))
  128. );
  129. } else if ( ! is_writable($destPath)) {
  130. throw new \InvalidArgumentException(
  131. sprintf("Mapping destination directory '<info>%s</info>' does not have write permissions.", $destPath)
  132. );
  133. }
  134. $toType = strtolower($input->getArgument('to-type'));
  135. $exporter = $this->getExporter($toType, $destPath);
  136. $exporter->setOverwriteExistingFiles( ($input->getOption('force') !== false) );
  137. if ($toType == 'annotation') {
  138. $entityGenerator = new EntityGenerator();
  139. $exporter->setEntityGenerator($entityGenerator);
  140. $entityGenerator->setNumSpaces($input->getOption('num-spaces'));
  141. if (($extend = $input->getOption('extend')) !== null) {
  142. $entityGenerator->setClassToExtend($extend);
  143. }
  144. }
  145. if (count($metadata)) {
  146. foreach ($metadata as $class) {
  147. $output->write(sprintf('Processing entity "<info>%s</info>"', $class->name) . PHP_EOL);
  148. }
  149. $exporter->setMetadata($metadata);
  150. $exporter->export();
  151. $output->write(PHP_EOL . sprintf(
  152. 'Exporting "<info>%s</info>" mapping information to "<info>%s</info>"' . PHP_EOL, $toType, $destPath
  153. ));
  154. } else {
  155. $output->write('No Metadata Classes to process.' . PHP_EOL);
  156. }
  157. }
  158. protected function getExporter($toType, $destPath)
  159. {
  160. $cme = new ClassMetadataExporter();
  161. return $cme->getExporter($toType, $destPath);
  162. }
  163. }