Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Tools / Console / Command / ConvertDoctrine1SchemaCommand.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\Export\ClassMetadataExporter,
26     Doctrine\ORM\Tools\ConvertDoctrine1Schema,
27     Doctrine\ORM\Tools\EntityGenerator;
28
29 /**
30  * Command to convert a Doctrine 1 schema to a Doctrine 2 mapping file.
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 ConvertDoctrine1SchemaCommand extends Console\Command\Command
41 {
42     /**
43      * @var EntityGenerator
44      */
45     private $entityGenerator = null;
46
47     /**
48      * @var ClassMetadataExporter
49      */
50     private $metadataExporter = null;
51
52     /**
53      * @return EntityGenerator
54      */
55     public function getEntityGenerator()
56     {
57         if ($this->entityGenerator == null) {
58             $this->entityGenerator = new EntityGenerator();
59         }
60
61         return $this->entityGenerator;
62     }
63
64     /**
65      * @param EntityGenerator $entityGenerator
66      */
67     public function setEntityGenerator(EntityGenerator $entityGenerator)
68     {
69         $this->entityGenerator = $entityGenerator;
70     }
71
72     /**
73      * @return ClassMetadataExporter
74      */
75     public function getMetadataExporter()
76     {
77         if ($this->metadataExporter == null) {
78             $this->metadataExporter = new ClassMetadataExporter();
79         }
80
81         return $this->metadataExporter;
82     }
83
84     /**
85      * @param ClassMetadataExporter $metadataExporter
86      */
87     public function setMetadataExporter(ClassMetadataExporter $metadataExporter)
88     {
89         $this->metadataExporter = $metadataExporter;
90     }
91
92     /**
93      * @see Console\Command\Command
94      */
95     protected function configure()
96     {
97         $this
98         ->setName('orm:convert-d1-schema')
99         ->setDescription('Converts Doctrine 1.X schema into a Doctrine 2.X schema.')
100         ->setDefinition(array(
101             new InputArgument(
102                 'from-path', InputArgument::REQUIRED, 'The path of Doctrine 1.X schema information.'
103             ),
104             new InputArgument(
105                 'to-type', InputArgument::REQUIRED, 'The destination Doctrine 2.X mapping type.'
106             ),
107             new InputArgument(
108                 'dest-path', InputArgument::REQUIRED,
109                 'The path to generate your Doctrine 2.X mapping information.'
110             ),
111             new InputOption(
112                 'from', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
113                 'Optional paths of Doctrine 1.X schema information.',
114                 array()
115             ),
116             new InputOption(
117                 'extend', null, InputOption::VALUE_OPTIONAL,
118                 'Defines a base class to be extended by generated entity classes.'
119             ),
120             new InputOption(
121                 'num-spaces', null, InputOption::VALUE_OPTIONAL,
122                 'Defines the number of indentation spaces', 4
123             )
124         ))
125         ->setHelp(<<<EOT
126 Converts Doctrine 1.X schema into a Doctrine 2.X schema.
127 EOT
128         );
129     }
130
131     /**
132      * @see Console\Command\Command
133      */
134     protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
135     {
136         $em = $this->getHelper('em')->getEntityManager();
137
138         // Process source directories
139         $fromPaths = array_merge(array($input->getArgument('from-path')), $input->getOption('from'));
140
141         // Process destination directory
142         $destPath = realpath($input->getArgument('dest-path'));
143
144         $toType = $input->getArgument('to-type');
145         $extend = $input->getOption('extend');
146         $numSpaces = $input->getOption('num-spaces');
147
148         $this->convertDoctrine1Schema($em, $fromPaths, $destPath, $toType, $numSpaces, $extend, $output);
149     }
150
151     /**
152      * @param \Doctrine\ORM\EntityManager $em
153      * @param array $fromPaths
154      * @param string $destPath
155      * @param string $toType
156      * @param int $numSpaces
157      * @param string|null $extend
158      * @param Console\Output\OutputInterface $output
159      */
160     public function convertDoctrine1Schema($em, $fromPaths, $destPath, $toType, $numSpaces, $extend, $output)
161     {
162         foreach ($fromPaths as &$dirName) {
163             $dirName = realpath($dirName);
164
165             if ( ! file_exists($dirName)) {
166                 throw new \InvalidArgumentException(
167                     sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not exist.", $dirName)
168                 );
169             } else if ( ! is_readable($dirName)) {
170                 throw new \InvalidArgumentException(
171                     sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not have read permissions.", $dirName)
172                 );
173             }
174         }
175
176         if ( ! file_exists($destPath)) {
177             throw new \InvalidArgumentException(
178                 sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not exist.", $destPath)
179             );
180         } else if ( ! is_writable($destPath)) {
181             throw new \InvalidArgumentException(
182                 sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not have write permissions.", $destPath)
183             );
184         }
185
186         $cme = $this->getMetadataExporter();
187         $exporter = $cme->getExporter($toType, $destPath);
188
189         if (strtolower($toType) === 'annotation') {
190             $entityGenerator = $this->getEntityGenerator();
191             $exporter->setEntityGenerator($entityGenerator);
192
193             $entityGenerator->setNumSpaces($numSpaces);
194
195             if ($extend !== null) {
196                 $entityGenerator->setClassToExtend($extend);
197             }
198         }
199
200         $converter = new ConvertDoctrine1Schema($fromPaths);
201         $metadata = $converter->getMetadata();
202
203         if ($metadata) {
204             $output->write(PHP_EOL);
205
206             foreach ($metadata as $class) {
207                 $output->write(sprintf('Processing entity "<info>%s</info>"', $class->name) . PHP_EOL);
208             }
209
210             $exporter->setMetadata($metadata);
211             $exporter->export();
212
213             $output->write(PHP_EOL . sprintf(
214                 'Converting Doctrine 1.X schema to "<info>%s</info>" mapping type in "<info>%s</info>"', $toType, $destPath
215             ));
216         } else {
217             $output->write('No Metadata Classes to process.' . PHP_EOL);
218         }
219     }
220 }