PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/symfony/2.0.0pr1/src/vendor/doctrine/lib/Doctrine/ORM/Tools/Cli/Tasks/ConvertMappingTask.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 244 lines | 155 code | 39 blank | 50 comment | 31 complexity | f840c51eb1747b86063f49d76234dc06 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  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. namespace Doctrine\ORM\Tools\Cli\Tasks;
  22. use Doctrine\Common\Cli\Tasks\AbstractTask,
  23. Doctrine\Common\Cli\CliException,
  24. Doctrine\Common\Cli\Option,
  25. Doctrine\Common\Cli\OptionGroup,
  26. Doctrine\ORM\Tools\Export\ClassMetadataExporter;
  27. /**
  28. * CLI Task to convert your mapping information between the various formats
  29. *
  30. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  31. * @link www.doctrine-project.org
  32. * @since 2.0
  33. * @version $Revision$
  34. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  35. * @author Jonathan Wage <jonwage@gmail.com>
  36. * @author Roman Borschel <roman@code-factory.org>
  37. */
  38. class ConvertMappingTask extends AbstractTask
  39. {
  40. /**
  41. * @inheritdoc
  42. */
  43. public function buildDocumentation()
  44. {
  45. $convertOptions = new OptionGroup(OptionGroup::CARDINALITY_N_N, array(
  46. new OptionGroup(OptionGroup::CARDINALITY_1_1, array(
  47. new Option('from', '<SOURCE>', 'The path to the mapping information to convert from (yml, xml, php, annotation).'),
  48. new Option('from-database', null, 'Use this option if you wish to reverse engineer your database to a set of Doctrine mapping files.')
  49. )),
  50. new Option('to', '<TYPE>', 'The format to convert to (yml, xml, php, annotation).'),
  51. new Option('dest', '<PATH>', 'The path to write the converted mapping information.')
  52. ));
  53. $doc = $this->getDocumentation();
  54. $doc->setName('convert-mapping')
  55. ->setDescription('Convert mapping information between supported formats.')
  56. ->getOptionGroup()
  57. ->addOption($convertOptions);
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function validate()
  63. {
  64. $arguments = $this->getArguments();
  65. if (isset($arguments['from-database']) && $arguments['from-database']) {
  66. $arguments['from'] = 'database';
  67. $this->setArguments($arguments);
  68. }
  69. if (!(isset($arguments['from']) && isset($arguments['to']) && isset($arguments['dest']))) {
  70. throw new CliException(
  71. 'You must include a value for all three options: --from, --to and --dest.'
  72. );
  73. }
  74. if (strtolower($arguments['to']) != 'annotation' && isset($arguments['extend'])) {
  75. throw new CliException(
  76. 'You can only use the --extend argument when converting to annotations.'
  77. );
  78. }
  79. if (strtolower($arguments['from']) == 'database') {
  80. // Check if we have an active EntityManager
  81. $em = $this->getConfiguration()->getAttribute('em');
  82. if ($em === null) {
  83. throw new CliException(
  84. "Attribute 'em' of CLI Configuration is not defined or it is not a valid EntityManager."
  85. );
  86. }
  87. $config = $em->getConfiguration();
  88. $config->setMetadataDriverImpl(
  89. new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
  90. $em->getConnection()->getSchemaManager()
  91. )
  92. );
  93. }
  94. return true;
  95. }
  96. public function run()
  97. {
  98. $arguments = $this->getArguments();
  99. $cme = new ClassMetadataExporter();
  100. $printer = $this->getPrinter();
  101. // Get exporter and configure it
  102. $exporter = $cme->getExporter($arguments['to'], $arguments['dest']);
  103. if (isset($arguments['extend']) && $arguments['extend']) {
  104. $exporter->setClassToExtend($arguments['extend']);
  105. }
  106. if (isset($arguments['num-spaces']) && $arguments['extend']) {
  107. $exporter->setNumSpaces($arguments['num-spaces']);
  108. }
  109. $from = (array) $arguments['from'];
  110. if ($this->_isDoctrine1Schema($from)) {
  111. $printer->writeln('Converting Doctrine 1 schema to Doctrine 2 mapping files', 'INFO');
  112. $converter = new \Doctrine\ORM\Tools\ConvertDoctrine1Schema($from);
  113. $metadatas = $converter->getMetadatasFromSchema();
  114. } else {
  115. foreach ($from as $source) {
  116. $sourceArg = $source;
  117. $type = $this->_determineSourceType($sourceArg);
  118. if ( ! $type) {
  119. throw CliException::invalidMappingSourceType($sourceArg);
  120. }
  121. $source = $this->_getSourceByType($type, $sourceArg);
  122. $printer->writeln(
  123. sprintf(
  124. 'Adding "%s" mapping source which contains the "%s" format',
  125. $printer->format($sourceArg, 'KEYWORD'), $printer->format($type, 'KEYWORD')
  126. )
  127. );
  128. $cme->addMappingSource($source, $type);
  129. }
  130. $metadatas = $cme->getMetadatasForMappingSources();
  131. }
  132. foreach ($metadatas as $metadata) {
  133. $printer->writeln(
  134. sprintf('Processing entity "%s"', $printer->format($metadata->name, 'KEYWORD'))
  135. );
  136. }
  137. $printer->writeln(
  138. sprintf(
  139. 'Exporting "%s" mapping information to directory "%s"',
  140. $printer->format($arguments['to'], 'KEYWORD'),
  141. $printer->format($arguments['dest'], 'KEYWORD')
  142. )
  143. );
  144. $exporter->setMetadatas($metadatas);
  145. $exporter->export();
  146. }
  147. private function _isDoctrine1Schema(array $from)
  148. {
  149. if ( ! class_exists('sfYaml', false)) {
  150. require_once __DIR__ . '/../../../../../vendor/sfYaml/sfYaml.class.php';
  151. require_once __DIR__ . '/../../../../../vendor/sfYaml/sfYamlDumper.class.php';
  152. require_once __DIR__ . '/../../../../../vendor/sfYaml/sfYamlInline.class.php';
  153. require_once __DIR__ . '/../../../../../vendor/sfYaml/sfYamlParser.class.php';
  154. }
  155. $files = glob(current($from) . '/*.yml');
  156. if ($files) {
  157. $array = \sfYaml::load($files[0]);
  158. $first = current($array);
  159. // We're dealing with a Doctrine 1 schema if you have
  160. // a columns index in the first model array
  161. return isset($first['columns']);
  162. } else {
  163. return false;
  164. }
  165. }
  166. private function _determineSourceType($source)
  167. {
  168. // If the --from=<VALUE> is a directory lets determine if it is
  169. // annotations, yaml, xml, etc.
  170. if (is_dir($source)) {
  171. // Find the files in the directory
  172. $files = glob($source . '/*.*');
  173. if ( ! $files) {
  174. throw new \InvalidArgumentException(
  175. sprintf('No mapping files found in "%s"', $source)
  176. );
  177. }
  178. // Get the contents of the first file
  179. $contents = file_get_contents($files[0]);
  180. // Check if it has a class definition in it for annotations
  181. if (preg_match("/class (.*)/", $contents)) {
  182. return 'annotation';
  183. // Otherwise lets determine the type based on the extension of the
  184. // first file in the directory (yml, xml, etc)
  185. } else {
  186. $info = pathinfo($files[0]);
  187. return $info['extension'];
  188. }
  189. // Nothing special for database
  190. } else if ($source == 'database') {
  191. return 'database';
  192. }
  193. }
  194. private function _getSourceByType($type, $source)
  195. {
  196. // If --from==database then the source is an instance of SchemaManager
  197. // for the current EntityMAnager
  198. if ($type == 'database') {
  199. $em = $this->getConfiguration()->getAttribute('em');
  200. return $em->getConnection()->getSchemaManager();
  201. } else {
  202. return $source;
  203. }
  204. }
  205. }