Completed
Push — master ( ba3223...b47a39 )
by Luís
17s
created

Console/Command/ConvertDoctrine1SchemaCommand.php (1 issue)

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

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
            $output->writeln('');
216
217
            foreach ($metadata as $class) {
218
                $output->writeln(sprintf('Processing entity "<info>%s</info>"', $class->name));
219
            }
220
221
            $exporter->setMetadata($metadata);
222
            $exporter->export();
223
224
            $output->writeln(PHP_EOL . sprintf(
225
                'Converting Doctrine 1.X schema to "<info>%s</info>" mapping type in "<info>%s</info>"', $toType, $destPath
226
            ));
227
        } else {
228 1
            $output->writeln('No Metadata Classes to process.');
229
        }
230 1
    }
231
}
232