Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Tools / Console / Command / GenerateEntitiesCommand.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\ORM\Tools\Console\Command;
21
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 /**
30  * Command to generate entity classes and method stubs from your mapping information.
31  *
32  * 
33  * @link    www.doctrine-project.org
34  * @since   2.0
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
86 If you use the <comment>--update-entities</comment> or <comment>--regenerate-entities</comment> flags your existing
87 code gets overwritten. The EntityGenerator will only append new code to your
88 file and will not delete the old code. However this approach may still be prone
89 to error and we suggest you use code repositories such as GIT or SVN to make
90 backups of your code.
91
92 It makes sense to generate the entity code if you are using entities as Data
93 Access Objects only and dont put much additional logic on them. If you are
94 however putting much more logic on the entities you should refrain from using
95 the entity-generator and code your entities manually.
96
97 <error>Important:</error> Even if you specified Inheritance options in your
98 XML or YAML Mapping files the generator cannot generate the base and
99 child classes for you correctly, because it doesn't know which
100 class is supposed to extend which. You have to adjust the entity
101 code manually for inheritance to work!
102 EOT
103         );
104     }
105
106     /**
107      * @see Console\Command\Command
108      */
109     protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
110     {
111         $em = $this->getHelper('em')->getEntityManager();
112
113         $cmf = new DisconnectedClassMetadataFactory();
114         $cmf->setEntityManager($em);
115         $metadatas = $cmf->getAllMetadata();
116         $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
117
118         // Process destination directory
119         $destPath = realpath($input->getArgument('dest-path'));
120
121         if ( ! file_exists($destPath)) {
122             throw new \InvalidArgumentException(
123                 sprintf("Entities destination directory '<info>%s</info>' does not exist.", $input->getArgument('dest-path'))
124             );
125         } else if ( ! is_writable($destPath)) {
126             throw new \InvalidArgumentException(
127                 sprintf("Entities destination directory '<info>%s</info>' does not have write permissions.", $destPath)
128             );
129         }
130
131         if (count($metadatas)) {
132             // Create EntityGenerator
133             $entityGenerator = new EntityGenerator();
134
135             $entityGenerator->setGenerateAnnotations($input->getOption('generate-annotations'));
136             $entityGenerator->setGenerateStubMethods($input->getOption('generate-methods'));
137             $entityGenerator->setRegenerateEntityIfExists($input->getOption('regenerate-entities'));
138             $entityGenerator->setUpdateEntityIfExists($input->getOption('update-entities'));
139             $entityGenerator->setNumSpaces($input->getOption('num-spaces'));
140
141             if (($extend = $input->getOption('extend')) !== null) {
142                 $entityGenerator->setClassToExtend($extend);
143             }
144
145             foreach ($metadatas as $metadata) {
146                 $output->write(
147                     sprintf('Processing entity "<info>%s</info>"', $metadata->name) . PHP_EOL
148                 );
149             }
150
151             // Generating Entities
152             $entityGenerator->generate($metadatas, $destPath);
153
154             // Outputting information message
155             $output->write(PHP_EOL . sprintf('Entity classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL);
156         } else {
157             $output->write('No Metadata Classes to process.' . PHP_EOL);
158         }
159     }
160 }