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