PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/plugins/sfPropelPlugin/lib/task/sfPropelBuildTask.class.php

https://github.com/bheneka/gitta
PHP | 257 lines | 212 code | 24 blank | 21 comment | 3 complexity | c31078fadcd81490a1b54f982775b61c MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. require_once(dirname(__FILE__).'/sfPropelBaseTask.class.php');
  10. /**
  11. * Generates code based on your schema.
  12. *
  13. * @package sfPropelPlugin
  14. * @subpackage task
  15. * @author Kris Wallsmith <kris.wallsmith@symfony-project.com>
  16. * @version SVN: $Id$
  17. */
  18. class sfPropelBuildTask extends sfPropelBaseTask
  19. {
  20. const
  21. BUILD_MODEL = 1,
  22. BUILD_FORMS = 2,
  23. BUILD_FILTERS = 4,
  24. BUILD_SQL = 8,
  25. BUILD_DB = 16,
  26. OPTION_MODEL = 1,
  27. OPTION_FORMS = 3, // model, forms
  28. OPTION_FILTERS = 5, // model, filters
  29. OPTION_SQL = 8,
  30. OPTION_DB = 24, // sql, db
  31. OPTION_ALL_CLASSES = 7, // model, forms, filters
  32. OPTION_ALL = 31; // model, forms, filters, sql, db
  33. /**
  34. * @see sfTask
  35. */
  36. protected function configure()
  37. {
  38. $this->addOptions(array(
  39. new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true),
  40. new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
  41. new sfCommandOption('no-confirmation', null, sfCommandOption::PARAMETER_NONE, 'Whether to force dropping of the database'),
  42. new sfCommandOption('all', null, sfCommandOption::PARAMETER_NONE, 'Build everything and reset the database'),
  43. new sfCommandOption('all-classes', null, sfCommandOption::PARAMETER_NONE, 'Build all classes'),
  44. new sfCommandOption('model', null, sfCommandOption::PARAMETER_NONE, 'Build model classes'),
  45. new sfCommandOption('forms', null, sfCommandOption::PARAMETER_NONE, 'Build form classes'),
  46. new sfCommandOption('filters', null, sfCommandOption::PARAMETER_NONE, 'Build filter classes'),
  47. new sfCommandOption('sql', null, sfCommandOption::PARAMETER_NONE, 'Build SQL'),
  48. new sfCommandOption('db', null, sfCommandOption::PARAMETER_NONE, 'Drop, create, and insert SQL'),
  49. new sfCommandOption('and-load', null, sfCommandOption::PARAMETER_OPTIONAL | sfCommandOption::IS_ARRAY, 'Load fixture data'),
  50. new sfCommandOption('and-append', null, sfCommandOption::PARAMETER_OPTIONAL | sfCommandOption::IS_ARRAY, 'Append fixture data'),
  51. ));
  52. $this->namespace = 'propel';
  53. $this->name = 'build';
  54. $this->briefDescription = 'Generate code based on your schema';
  55. $this->detailedDescription = <<<EOF
  56. The [propel:build|INFO] task generates code based on your schema:
  57. [./symfony propel:build|INFO]
  58. You must specify what you would like built. For instance, if you want model
  59. and form classes built use the [--model|COMMENT] and [--forms|COMMENT] options:
  60. [./symfony propel:build --model --forms|INFO]
  61. You can use the [--all|COMMENT] shortcut option if you would like all classes and
  62. SQL files generated and the database rebuilt:
  63. [./symfony propel:build --all|INFO]
  64. This is equivalent to running the following tasks:
  65. [./symfony propel:build-model|INFO]
  66. [./symfony propel:build-forms|INFO]
  67. [./symfony propel:build-filters|INFO]
  68. [./symfony propel:build-sql|INFO]
  69. [./symfony propel:insert-sql|INFO]
  70. You can also generate only class files by using the [--all-classes|COMMENT] shortcut
  71. option. When this option is used alone, the database will not be modified.
  72. [./symfony propel:build --all-classes|INFO]
  73. The [--and-load|COMMENT] option will load data from the project and plugin
  74. [data/fixtures/|COMMENT] directories:
  75. [./symfony propel:build --db --and-load|INFO]
  76. To specify what fixtures are loaded, add a parameter to the [--and-load|COMMENT] option:
  77. [./symfony propel:build --all --and-load="data/fixtures/dev/"|INFO]
  78. To append fixture data without erasing any records from the database, include
  79. the [--and-append|COMMENT] option:
  80. [./symfony propel:build --all --and-append|INFO]
  81. EOF;
  82. }
  83. /**
  84. * @see sfTask
  85. */
  86. protected function execute($arguments = array(), $options = array())
  87. {
  88. if (!$mode = $this->calculateMode($options))
  89. {
  90. throw new InvalidArgumentException(sprintf("You must include one or more of the following build options:\n--%s\n\nSee this task's help page for more information:\n\n php symfony help propel:build", join(', --', array_keys($this->getBuildOptions()))));
  91. }
  92. if (self::BUILD_MODEL == (self::BUILD_MODEL & $mode))
  93. {
  94. $task = new sfPropelBuildModelTask($this->dispatcher, $this->formatter);
  95. $task->setCommandApplication($this->commandApplication);
  96. $task->setConfiguration($this->configuration);
  97. $ret = $task->run();
  98. if ($ret)
  99. {
  100. return $ret;
  101. }
  102. }
  103. if (self::BUILD_FORMS == (self::BUILD_FORMS & $mode))
  104. {
  105. $task = new sfPropelBuildFormsTask($this->dispatcher, $this->formatter);
  106. $task->setCommandApplication($this->commandApplication);
  107. $task->setConfiguration($this->configuration);
  108. $ret = $task->run();
  109. if ($ret)
  110. {
  111. return $ret;
  112. }
  113. }
  114. if (self::BUILD_FILTERS == (self::BUILD_FILTERS & $mode))
  115. {
  116. $task = new sfPropelBuildFiltersTask($this->dispatcher, $this->formatter);
  117. $task->setCommandApplication($this->commandApplication);
  118. $task->setConfiguration($this->configuration);
  119. $ret = $task->run();
  120. if ($ret)
  121. {
  122. return $ret;
  123. }
  124. }
  125. if (self::BUILD_SQL == (self::BUILD_SQL & $mode))
  126. {
  127. $task = new sfPropelBuildSqlTask($this->dispatcher, $this->formatter);
  128. $task->setCommandApplication($this->commandApplication);
  129. $task->setConfiguration($this->configuration);
  130. $ret = $task->run();
  131. if ($ret)
  132. {
  133. return $ret;
  134. }
  135. }
  136. if (self::BUILD_DB == (self::BUILD_DB & $mode))
  137. {
  138. $task = new sfPropelInsertSqlTask($this->dispatcher, $this->formatter);
  139. $task->setCommandApplication($this->commandApplication);
  140. $task->setConfiguration($this->configuration);
  141. $ret = $task->run(array(), array(
  142. 'no-confirmation' => $options['no-confirmation'],
  143. ));
  144. if ($ret)
  145. {
  146. return $ret;
  147. }
  148. }
  149. if (count($options['and-load']) || count($options['and-append']))
  150. {
  151. $task = new sfPropelDataLoadTask($this->dispatcher, $this->formatter);
  152. $task->setCommandApplication($this->commandApplication);
  153. $task->setConfiguration($this->configuration);
  154. if (count($options['and-load']))
  155. {
  156. $ret = $task->run(array(
  157. 'dir_or_file' => in_array(array(), $options['and-load'], true) ? null : $options['and-load'],
  158. ));
  159. if ($ret)
  160. {
  161. return $ret;
  162. }
  163. }
  164. if (count($options['and-append']))
  165. {
  166. $ret = $task->run(array(
  167. 'dir_or_file' => in_array(array(), $options['and-append'], true) ? null : $options['and-append'],
  168. ), array(
  169. 'append' => true,
  170. ));
  171. if ($ret)
  172. {
  173. return $ret;
  174. }
  175. }
  176. }
  177. }
  178. /**
  179. * Calculates a bit mode based on the supplied options.
  180. *
  181. * @param array $options
  182. *
  183. * @return integer
  184. */
  185. protected function calculateMode($options = array())
  186. {
  187. $mode = 0;
  188. foreach ($this->getBuildOptions() as $name => $value)
  189. {
  190. if (isset($options[$name]) && true === $options[$name])
  191. {
  192. $mode = $mode | $value;
  193. }
  194. }
  195. return $mode;
  196. }
  197. /**
  198. * Returns an array of valid build options.
  199. *
  200. * @return array An array of option names and their mode
  201. */
  202. protected function getBuildOptions()
  203. {
  204. $options = array();
  205. foreach ($this->options as $option)
  206. {
  207. if (defined($constant = __CLASS__.'::OPTION_'.str_replace('-', '_', strtoupper($option->getName()))))
  208. {
  209. $options[$option->getName()] = constant($constant);
  210. }
  211. }
  212. return $options;
  213. }
  214. }