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.
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>.
20 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;
30 * Command to generate entity classes and method stubs from your mapping information.
33 * @link www.doctrine-project.org
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>
40 class GenerateEntitiesCommand extends Console\Command\Command
43 * @see Console\Command\Command
45 protected function configure()
48 ->setName('orm:generate-entities')
49 ->setDescription('Generate entity classes and method stubs from your mapping information.')
50 ->setDefinition(array(
52 'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
53 'A string pattern used to match entities that should be processed.'
56 'dest-path', InputArgument::REQUIRED, 'The path to generate your entity classes.'
59 'generate-annotations', null, InputOption::VALUE_OPTIONAL,
60 'Flag to define if generator should generate annotation metadata on entities.', false
63 'generate-methods', null, InputOption::VALUE_OPTIONAL,
64 'Flag to define if generator should generate stub methods on entities.', true
67 'regenerate-entities', null, InputOption::VALUE_OPTIONAL,
68 'Flag to define if generator should regenerate entity if it exists.', false
71 'update-entities', null, InputOption::VALUE_OPTIONAL,
72 'Flag to define if generator should only update entity if it exists.', true
75 'extend', null, InputOption::VALUE_OPTIONAL,
76 'Defines a base class to be extended by generated entity classes.'
79 'num-spaces', null, InputOption::VALUE_OPTIONAL,
80 'Defines the number of indentation spaces', 4
84 Generate entity classes and method stubs from your mapping information.
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
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.
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!
107 * @see Console\Command\Command
109 protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
111 $em = $this->getHelper('em')->getEntityManager();
113 $cmf = new DisconnectedClassMetadataFactory();
114 $cmf->setEntityManager($em);
115 $metadatas = $cmf->getAllMetadata();
116 $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
118 // Process destination directory
119 $destPath = realpath($input->getArgument('dest-path'));
121 if ( ! file_exists($destPath)) {
122 throw new \InvalidArgumentException(
123 sprintf("Entities destination directory '<info>%s</info>' does not exist.", $input->getArgument('dest-path'))
125 } else if ( ! is_writable($destPath)) {
126 throw new \InvalidArgumentException(
127 sprintf("Entities destination directory '<info>%s</info>' does not have write permissions.", $destPath)
131 if (count($metadatas)) {
132 // Create EntityGenerator
133 $entityGenerator = new EntityGenerator();
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'));
141 if (($extend = $input->getOption('extend')) !== null) {
142 $entityGenerator->setClassToExtend($extend);
145 foreach ($metadatas as $metadata) {
147 sprintf('Processing entity "<info>%s</info>"', $metadata->name) . PHP_EOL
151 // Generating Entities
152 $entityGenerator->generate($metadatas, $destPath);
154 // Outputting information message
155 $output->write(PHP_EOL . sprintf('Entity classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL);
157 $output->write('No Metadata Classes to process.' . PHP_EOL);