/cake/console/libs/tasks/plugin.php

https://github.com/hardsshah/bookmarks · PHP · 202 lines · 110 code · 22 blank · 70 comment · 23 complexity · 59ab0a635c801bc5b79f9e1bd01b1b1b MD5 · raw file

  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * The Plugin Task handles creating an empty plugin, ready to be used
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  19. * @package cake
  20. * @subpackage cake.cake.console.libs.tasks
  21. * @since CakePHP(tm) v 1.2
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  26. */
  27. if (!class_exists('File')) {
  28. uses('file');
  29. }
  30. /**
  31. * Task class for creating a plugin
  32. *
  33. * @package cake
  34. * @subpackage cake.cake.console.libs.tasks
  35. */
  36. class PluginTask extends Shell {
  37. /**
  38. * Tasks
  39. *
  40. */
  41. var $tasks = array('Model', 'Controller', 'View');
  42. /**
  43. * path to CONTROLLERS directory
  44. *
  45. * @var array
  46. * @access public
  47. */
  48. var $path = null;
  49. /**
  50. * initialize
  51. *
  52. * @return void
  53. */
  54. function initialize() {
  55. $this->path = APP . 'plugins' . DS;
  56. }
  57. /**
  58. * Execution method always used for tasks
  59. *
  60. * @return void
  61. */
  62. function execute() {
  63. if (empty($this->params['skel'])) {
  64. $this->params['skel'] = '';
  65. if (is_dir(CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel') === true) {
  66. $this->params['skel'] = CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel';
  67. }
  68. }
  69. $plugin = null;
  70. if (isset($this->args[0])) {
  71. $plugin = Inflector::camelize($this->args[0]);
  72. $pluginPath = Inflector::underscore($plugin) . DS;
  73. $this->Dispatch->shiftArgs();
  74. if (is_dir($this->path . $pluginPath)) {
  75. $this->out(sprintf('Plugin: %s', $plugin));
  76. $this->out(sprintf('Path: %s', $this->path . $pluginPath));
  77. $this->hr();
  78. } elseif (isset($this->args[0])) {
  79. $this->err(sprintf('%s in path %s not found.', $plugin, $this->path . $pluginPath));
  80. $this->_stop();
  81. } else {
  82. $this->__interactive($plugin);
  83. }
  84. }
  85. if (isset($this->args[0])) {
  86. $task = Inflector::classify($this->args[0]);
  87. $this->Dispatch->shiftArgs();
  88. if (in_array($task, $this->tasks)) {
  89. $this->{$task}->plugin = $plugin;
  90. $this->{$task}->path = $this->path . $pluginPath . Inflector::underscore(Inflector::pluralize($task)) . DS;
  91. if (!is_dir($this->{$task}->path)) {
  92. $this->err(sprintf(__("%s directory could not be found.\nBe sure you have created %s", true), $task, $this->{$task}->path));
  93. }
  94. $this->{$task}->loadTasks();
  95. $this->{$task}->execute();
  96. }
  97. }
  98. }
  99. /**
  100. * Interactive interface
  101. *
  102. * @access private
  103. * @return void
  104. */
  105. function __interactive($plugin = null) {
  106. while ($plugin === null) {
  107. $plugin = $this->in(__('Enter the name of the plugin in CamelCase format', true));
  108. }
  109. if (!$this->bake($plugin)) {
  110. $this->err(sprintf(__("An error occured trying to bake: %s in %s", true), $plugin, $this->path . $pluginPath));
  111. }
  112. }
  113. /**
  114. * Bake the plugin, create directories and files
  115. *
  116. * @params $plugin name of the plugin in CamelCased format
  117. * @access public
  118. * @return bool
  119. */
  120. function bake($plugin) {
  121. $pluginPath = Inflector::underscore($plugin);
  122. $this->hr();
  123. $this->out("Plugin Name: $plugin");
  124. $this->out("Plugin Directory: {$this->path}{$pluginPath}");
  125. $this->hr();
  126. $looksGood = $this->in('Look okay?', array('y', 'n', 'q'), 'y');
  127. if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
  128. $verbose = $this->in(__('Do you want verbose output?', true), array('y', 'n'), 'n');
  129. $Folder = new Folder($this->path . $pluginPath);
  130. $directories = array('models' . DS . 'behaviors', 'controllers' . DS . 'components', 'views' . DS . 'helpers');
  131. foreach ($directories as $directory) {
  132. $Folder->create($this->path . $pluginPath . DS . $directory);
  133. }
  134. if (low($verbose) == 'y' || low($verbose) == 'yes') {
  135. foreach ($Folder->messages() as $message) {
  136. $this->out($message);
  137. }
  138. }
  139. $errors = $Folder->errors();
  140. if (!empty($errors)) {
  141. return false;
  142. }
  143. $controllerFileName = $pluginPath . '_app_controller.php';
  144. $out = "<?php\n\n";
  145. $out .= "class {$plugin}AppController extends AppController {\n\n";
  146. $out .= "}\n\n";
  147. $out .= "?>";
  148. $this->createFile($this->path . $pluginPath. DS . $controllerFileName, $out);
  149. $modelFileName = $pluginPath . '_app_model.php';
  150. $out = "<?php\n\n";
  151. $out .= "class {$plugin}AppModel extends AppModel {\n\n";
  152. $out .= "}\n\n";
  153. $out .= "?>";
  154. $this->createFile($this->path . $pluginPath . DS . $modelFileName, $out);
  155. $this->hr();
  156. $this->out(sprintf(__("Created: %s in %s", true), $plugin, $this->path . $pluginPath));
  157. $this->hr();
  158. }
  159. return true;
  160. }
  161. /**
  162. * Help
  163. *
  164. * @return void
  165. * @access public
  166. */
  167. function help() {
  168. $this->hr();
  169. $this->out("Usage: cake bake plugin <arg1> <arg2>...");
  170. $this->hr();
  171. $this->out('Commands:');
  172. $this->out("\n\tplugin <name>\n\t\tbakes plugin directory structure");
  173. $this->out("\n\tplugin <name> model\n\t\tbakes model. Run 'cake bake model help' for more info.");
  174. $this->out("\n\tplugin <name> controller\n\t\tbakes controller. Run 'cake bake controller help' for more info.");
  175. $this->out("\n\tplugin <name> view\n\t\tbakes view. Run 'cake bake view help' for more info.");
  176. $this->out("");
  177. $this->_stop();
  178. }
  179. }
  180. ?>