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