source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/ORM/Tools/Console/Command/ConvertMappingCommand.php @ 345

Last change on this file since 345 was 345, checked in by quyenla, 11 years ago

collaborator page

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