BakeTask.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Console\Command\Task;
  16. use Cake\Cache\Cache;
  17. use Cake\Console\Shell;
  18. use Cake\Core\Configure;
  19. use Cake\Utility\ConventionsTrait;
  20. /**
  21. * Base class for Bake Tasks.
  22. *
  23. */
  24. class BakeTask extends Shell {
  25. use ConventionsTrait;
  26. /**
  27. * The pathFragment appended to the plugin/app path.
  28. *
  29. * @var string
  30. */
  31. public $pathFragment;
  32. /**
  33. * Name of plugin
  34. *
  35. * @var string
  36. */
  37. public $plugin = null;
  38. /**
  39. * The db connection being used for baking
  40. *
  41. * @var string
  42. */
  43. public $connection = null;
  44. /**
  45. * Disable caching and enable debug for baking.
  46. * This forces the most current database schema to be used.
  47. *
  48. * @return void
  49. */
  50. public function startup() {
  51. Configure::write('debug', true);
  52. Cache::disable();
  53. }
  54. /**
  55. * Gets the path for output. Checks the plugin property
  56. * and returns the correct path.
  57. *
  58. * @return string Path to output.
  59. */
  60. public function getPath() {
  61. $path = APP . $this->pathFragment;
  62. if (isset($this->plugin)) {
  63. $path = $this->_pluginPath($this->plugin) . $this->pathFragment;
  64. }
  65. return str_replace('/', DS, $path);
  66. }
  67. /**
  68. * Base execute method parses some parameters and sets some properties on the bake tasks.
  69. * call when overriding execute()
  70. *
  71. * @return void
  72. */
  73. public function main() {
  74. if (isset($this->params['plugin'])) {
  75. $this->plugin = $this->params['plugin'];
  76. }
  77. if (isset($this->params['connection'])) {
  78. $this->connection = $this->params['connection'];
  79. }
  80. }
  81. /**
  82. * Handles splitting up the plugin prefix and classname.
  83. *
  84. * Sets the plugin parameter and plugin property.
  85. *
  86. * @param string $name The name to possibly split.
  87. * @return string The name without the plugin prefix.
  88. */
  89. protected function _getName($name) {
  90. if (strpos($name, '.')) {
  91. list($plugin, $name) = pluginSplit($name);
  92. $this->plugin = $this->params['plugin'] = $plugin;
  93. }
  94. return $name;
  95. }
  96. /**
  97. * Get the option parser for this task.
  98. *
  99. * This base class method sets up some commonly used options.
  100. *
  101. * @return \Cake\Console\ConsoleOptionParser
  102. */
  103. public function getOptionParser() {
  104. $parser = parent::getOptionParser();
  105. $parser->addOption('plugin', [
  106. 'short' => 'p',
  107. 'help' => __d('cake_console', 'Plugin to bake into.')
  108. ])->addOption('force', [
  109. 'short' => 'f',
  110. 'boolean' => true,
  111. 'help' => __d('cake_console', 'Force overwriting existing files without prompting.')
  112. ])->addOption('connection', [
  113. 'short' => 'c',
  114. 'default' => 'default',
  115. 'help' => __d('cake_console', 'The datasource connection to get data from.')
  116. ])->addOption('theme', [
  117. 'short' => 't',
  118. 'default' => 'default',
  119. 'help' => __d('cake_console', 'Theme to use when baking code.')
  120. ]);
  121. return $parser;
  122. }
  123. }