GenerateEntitiesCommand.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\EntityGenerator,
  27. Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
  28. /**
  29. * Command to generate entity classes and method stubs from your mapping information.
  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 GenerateEntitiesCommand extends Console\Command\Command
  41. {
  42. /**
  43. * @see Console\Command\Command
  44. */
  45. protected function configure()
  46. {
  47. $this
  48. ->setName('orm:generate-entities')
  49. ->setDescription('Generate entity classes and method stubs from your mapping information.')
  50. ->setDefinition(array(
  51. new InputOption(
  52. 'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  53. 'A string pattern used to match entities that should be processed.'
  54. ),
  55. new InputArgument(
  56. 'dest-path', InputArgument::REQUIRED, 'The path to generate your entity classes.'
  57. ),
  58. new InputOption(
  59. 'generate-annotations', null, InputOption::VALUE_OPTIONAL,
  60. 'Flag to define if generator should generate annotation metadata on entities.', false
  61. ),
  62. new InputOption(
  63. 'generate-methods', null, InputOption::VALUE_OPTIONAL,
  64. 'Flag to define if generator should generate stub methods on entities.', true
  65. ),
  66. new InputOption(
  67. 'regenerate-entities', null, InputOption::VALUE_OPTIONAL,
  68. 'Flag to define if generator should regenerate entity if it exists.', false
  69. ),
  70. new InputOption(
  71. 'update-entities', null, InputOption::VALUE_OPTIONAL,
  72. 'Flag to define if generator should only update entity if it exists.', true
  73. ),
  74. new InputOption(
  75. 'extend', null, InputOption::VALUE_OPTIONAL,
  76. 'Defines a base class to be extended by generated entity classes.'
  77. ),
  78. new InputOption(
  79. 'num-spaces', null, InputOption::VALUE_OPTIONAL,
  80. 'Defines the number of indentation spaces', 4
  81. )
  82. ))
  83. ->setHelp(<<<EOT
  84. Generate entity classes and method stubs from your mapping information.
  85. If you use the <comment>--update-entities</comment> or <comment>--regenerate-entities</comment> flags your exisiting
  86. code gets overwritten. The EntityGenerator will only append new code to your
  87. file and will not delete the old code. However this approach may still be prone
  88. to error and we suggest you use code repositories such as GIT or SVN to make
  89. backups of your code.
  90. It makes sense to generate the entity code if you are using entities as Data
  91. Access Objects only and dont put much additional logic on them. If you are
  92. however putting much more logic on the entities you should refrain from using
  93. the entity-generator and code your entities manually.
  94. <error>Important:</error> Even if you specified Inheritance options in your
  95. XML or YAML Mapping files the generator cannot generate the base and
  96. child classes for you correctly, because it doesn't know which
  97. class is supposed to extend which. You have to adjust the entity
  98. code manually for inheritance to work!
  99. EOT
  100. );
  101. }
  102. /**
  103. * @see Console\Command\Command
  104. */
  105. protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
  106. {
  107. $em = $this->getHelper('em')->getEntityManager();
  108. $cmf = new DisconnectedClassMetadataFactory();
  109. $cmf->setEntityManager($em);
  110. $metadatas = $cmf->getAllMetadata();
  111. $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
  112. // Process destination directory
  113. $destPath = realpath($input->getArgument('dest-path'));
  114. if ( ! file_exists($destPath)) {
  115. throw new \InvalidArgumentException(
  116. sprintf("Entities destination directory '<info>%s</info>' does not exist.", $input->getArgument('dest-path'))
  117. );
  118. } else if ( ! is_writable($destPath)) {
  119. throw new \InvalidArgumentException(
  120. sprintf("Entities destination directory '<info>%s</info>' does not have write permissions.", $destPath)
  121. );
  122. }
  123. if (count($metadatas)) {
  124. // Create EntityGenerator
  125. $entityGenerator = new EntityGenerator();
  126. $entityGenerator->setGenerateAnnotations($input->getOption('generate-annotations'));
  127. $entityGenerator->setGenerateStubMethods($input->getOption('generate-methods'));
  128. $entityGenerator->setRegenerateEntityIfExists($input->getOption('regenerate-entities'));
  129. $entityGenerator->setUpdateEntityIfExists($input->getOption('update-entities'));
  130. $entityGenerator->setNumSpaces($input->getOption('num-spaces'));
  131. if (($extend = $input->getOption('extend')) !== null) {
  132. $entityGenerator->setClassToExtend($extend);
  133. }
  134. foreach ($metadatas as $metadata) {
  135. $output->write(
  136. sprintf('Processing entity "<info>%s</info>"', $metadata->name) . PHP_EOL
  137. );
  138. }
  139. // Generating Entities
  140. $entityGenerator->generate($metadatas, $destPath);
  141. // Outputting information message
  142. $output->write(PHP_EOL . sprintf('Entity classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL);
  143. } else {
  144. $output->write('No Metadata Classes to process.' . PHP_EOL);
  145. }
  146. }
  147. }