SimpleBakeTask.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Console\Command\Task;
  16. use Cake\Console\Command\Task\BakeTask;
  17. use Cake\Core\App;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Utility\Inflector;
  21. /**
  22. * Base class for simple bake tasks code generator.
  23. */
  24. abstract class SimpleBakeTask extends BakeTask {
  25. /**
  26. * Tasks to be loaded by this Task
  27. *
  28. * @var array
  29. */
  30. public $tasks = ['Test', 'Template'];
  31. /**
  32. * Get the generated object's name.
  33. *
  34. * @return string
  35. */
  36. abstract public function name();
  37. /**
  38. * Get the generated object's filename without the leading path.
  39. *
  40. * @param string $name The name of the object being generated
  41. * @return string
  42. */
  43. abstract public function fileName($name);
  44. /**
  45. * Get the template name.
  46. *
  47. * @return string
  48. */
  49. abstract public function template();
  50. /**
  51. * Get template data.
  52. *
  53. * @return array
  54. */
  55. public function templateData() {
  56. $namespace = Configure::read('App.namespace');
  57. if ($this->plugin) {
  58. $namespace = Plugin::getNamespace($this->plugin);
  59. }
  60. return ['namespace' => $namespace];
  61. }
  62. /**
  63. * Execute method
  64. *
  65. * @return void
  66. */
  67. public function execute($name = null) {
  68. parent::execute();
  69. if (empty($name)) {
  70. return $this->error('You must provide a name to bake a ' . $this->name());
  71. }
  72. $name = Inflector::classify($name);
  73. $this->bake($name);
  74. $this->bakeTest($name);
  75. }
  76. /**
  77. * Generate a class stub
  78. *
  79. * @param string $className The classname to generate.
  80. * @return void
  81. */
  82. public function bake($name) {
  83. $this->Template->set('name', $name);
  84. $this->Template->set($this->templateData());
  85. $contents = $this->Template->generate('classes', $this->template());
  86. $filename = $this->getPath() . $this->fileName($name);
  87. $this->createFile($filename, $contents);
  88. return $contents;
  89. }
  90. /**
  91. * Generate a test case.
  92. *
  93. * @return void
  94. */
  95. public function bakeTest($className) {
  96. if (!empty($this->params['no-test'])) {
  97. return;
  98. }
  99. $this->Test->plugin = $this->plugin;
  100. return $this->Test->bake($this->name(), $className);
  101. }
  102. /**
  103. * Gets the option parser instance and configures it.
  104. *
  105. * @return \Cake\Console\ConsoleOptionParser
  106. */
  107. public function getOptionParser() {
  108. $parser = parent::getOptionParser();
  109. $name = $this->name();
  110. $parser->description(
  111. __d('cake_console', 'Bake a %s class file.', $name)
  112. )->addArgument('name', [
  113. 'help' => __d(
  114. 'cake_console',
  115. 'Name of the %s to bake. Can use Plugin.name to bake %s files into plugins.',
  116. $name,
  117. $name
  118. )
  119. ])->addOption('no-test', [
  120. 'boolean' => true,
  121. 'help' => __d('cake_console', 'Do not generate a test skeleton.')
  122. ]);
  123. return $parser;
  124. }
  125. }