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