BakeTask.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * Initialize hook.
  56. *
  57. * Populates the connection property, which is useful for tasks of tasks.
  58. *
  59. * @return void
  60. */
  61. public function initialize() {
  62. if (empty($this->connection) && !empty($this->params['connection'])) {
  63. $this->connection = $this->params['connection'];
  64. }
  65. }
  66. /**
  67. * Gets the path for output. Checks the plugin property
  68. * and returns the correct path.
  69. *
  70. * @return string Path to output.
  71. */
  72. public function getPath() {
  73. $path = APP . $this->pathFragment;
  74. if (isset($this->plugin)) {
  75. $path = $this->_pluginPath($this->plugin) . 'src/' . $this->pathFragment;
  76. }
  77. return str_replace('/', DS, $path);
  78. }
  79. /**
  80. * Base execute method parses some parameters and sets some properties on the bake tasks.
  81. * call when overriding execute()
  82. *
  83. * @return void
  84. */
  85. public function main() {
  86. if (isset($this->params['plugin'])) {
  87. $this->plugin = $this->params['plugin'];
  88. }
  89. if (isset($this->params['connection'])) {
  90. $this->connection = $this->params['connection'];
  91. }
  92. }
  93. /**
  94. * Executes an external shell command and pipes its output to the stdout
  95. *
  96. * @param string $command the command to execute
  97. * @return void
  98. * @throws \RuntimeException if any errors occurred during the execution
  99. */
  100. public function callProcess($command) {
  101. $descriptorSpec = [
  102. 0 => ['pipe', 'r'],
  103. 1 => ['pipe', 'w'],
  104. 2 => ['pipe', 'w']
  105. ];
  106. $this->_io->verbose('Running ' . $command);
  107. $process = proc_open(
  108. $command,
  109. $descriptorSpec,
  110. $pipes
  111. );
  112. if (!is_resource($process)) {
  113. $this->error('Could not start subprocess.');
  114. return false;
  115. }
  116. $output = $error = '';
  117. fclose($pipes[0]);
  118. $output = stream_get_contents($pipes[1]);
  119. fclose($pipes[1]);
  120. $error = stream_get_contents($pipes[2]);
  121. fclose($pipes[2]);
  122. proc_close($process);
  123. if ($error) {
  124. throw new \RuntimeException($error);
  125. }
  126. $this->out($output);
  127. }
  128. /**
  129. * Handles splitting up the plugin prefix and classname.
  130. *
  131. * Sets the plugin parameter and plugin property.
  132. *
  133. * @param string $name The name to possibly split.
  134. * @return string The name without the plugin prefix.
  135. */
  136. protected function _getName($name) {
  137. if (strpos($name, '.')) {
  138. list($plugin, $name) = pluginSplit($name);
  139. $this->plugin = $this->params['plugin'] = $plugin;
  140. }
  141. return $name;
  142. }
  143. /**
  144. * Get the option parser for this task.
  145. *
  146. * This base class method sets up some commonly used options.
  147. *
  148. * @return \Cake\Console\ConsoleOptionParser
  149. */
  150. public function getOptionParser() {
  151. $parser = parent::getOptionParser();
  152. $parser->addOption('plugin', [
  153. 'short' => 'p',
  154. 'help' => 'Plugin to bake into.'
  155. ])->addOption('force', [
  156. 'short' => 'f',
  157. 'boolean' => true,
  158. 'help' => 'Force overwriting existing files without prompting.'
  159. ])->addOption('connection', [
  160. 'short' => 'c',
  161. 'default' => 'default',
  162. 'help' => 'The datasource connection to get data from.'
  163. ])->addOption('template', [
  164. 'short' => 't',
  165. 'default' => 'default',
  166. 'help' => 'Template to use when baking code.'
  167. ]);
  168. return $parser;
  169. }
  170. }