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.
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>.
20 namespace Doctrine\ORM\Tools\Console\Command;
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;
30 * Command to convert a Doctrine 1 schema to a Doctrine 2 mapping file.
33 * @link www.doctrine-project.org
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>
40 class ConvertDoctrine1SchemaCommand extends Console\Command\Command
43 * @var EntityGenerator
45 private $entityGenerator = null;
48 * @var ClassMetadataExporter
50 private $metadataExporter = null;
53 * @return EntityGenerator
55 public function getEntityGenerator()
57 if ($this->entityGenerator == null) {
58 $this->entityGenerator = new EntityGenerator();
61 return $this->entityGenerator;
65 * @param EntityGenerator $entityGenerator
67 public function setEntityGenerator(EntityGenerator $entityGenerator)
69 $this->entityGenerator = $entityGenerator;
73 * @return ClassMetadataExporter
75 public function getMetadataExporter()
77 if ($this->metadataExporter == null) {
78 $this->metadataExporter = new ClassMetadataExporter();
81 return $this->metadataExporter;
85 * @param ClassMetadataExporter $metadataExporter
87 public function setMetadataExporter(ClassMetadataExporter $metadataExporter)
89 $this->metadataExporter = $metadataExporter;
93 * @see Console\Command\Command
95 protected function configure()
98 ->setName('orm:convert-d1-schema')
99 ->setDescription('Converts Doctrine 1.X schema into a Doctrine 2.X schema.')
100 ->setDefinition(array(
102 'from-path', InputArgument::REQUIRED, 'The path of Doctrine 1.X schema information.'
105 'to-type', InputArgument::REQUIRED, 'The destination Doctrine 2.X mapping type.'
108 'dest-path', InputArgument::REQUIRED,
109 'The path to generate your Doctrine 2.X mapping information.'
112 'from', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
113 'Optional paths of Doctrine 1.X schema information.',
117 'extend', null, InputOption::VALUE_OPTIONAL,
118 'Defines a base class to be extended by generated entity classes.'
121 'num-spaces', null, InputOption::VALUE_OPTIONAL,
122 'Defines the number of indentation spaces', 4
126 Converts Doctrine 1.X schema into a Doctrine 2.X schema.
132 * @see Console\Command\Command
134 protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
136 $em = $this->getHelper('em')->getEntityManager();
138 // Process source directories
139 $fromPaths = array_merge(array($input->getArgument('from-path')), $input->getOption('from'));
141 // Process destination directory
142 $destPath = realpath($input->getArgument('dest-path'));
144 $toType = $input->getArgument('to-type');
145 $extend = $input->getOption('extend');
146 $numSpaces = $input->getOption('num-spaces');
148 $this->convertDoctrine1Schema($em, $fromPaths, $destPath, $toType, $numSpaces, $extend, $output);
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
160 public function convertDoctrine1Schema($em, $fromPaths, $destPath, $toType, $numSpaces, $extend, $output)
162 foreach ($fromPaths as &$dirName) {
163 $dirName = realpath($dirName);
165 if ( ! file_exists($dirName)) {
166 throw new \InvalidArgumentException(
167 sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not exist.", $dirName)
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)
176 if ( ! file_exists($destPath)) {
177 throw new \InvalidArgumentException(
178 sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not exist.", $destPath)
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)
186 $cme = $this->getMetadataExporter();
187 $exporter = $cme->getExporter($toType, $destPath);
189 if (strtolower($toType) === 'annotation') {
190 $entityGenerator = $this->getEntityGenerator();
191 $exporter->setEntityGenerator($entityGenerator);
193 $entityGenerator->setNumSpaces($numSpaces);
195 if ($extend !== null) {
196 $entityGenerator->setClassToExtend($extend);
200 $converter = new ConvertDoctrine1Schema($fromPaths);
201 $metadata = $converter->getMetadata();
204 $output->write(PHP_EOL);
206 foreach ($metadata as $class) {
207 $output->write(sprintf('Processing entity "<info>%s</info>"', $class->name) . PHP_EOL);
210 $exporter->setMetadata($metadata);
213 $output->write(PHP_EOL . sprintf(
214 'Converting Doctrine 1.X schema to "<info>%s</info>" mapping type in "<info>%s</info>"', $toType, $destPath
217 $output->write('No Metadata Classes to process.' . PHP_EOL);