2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* The Plugin Task handles creating an empty plugin, ready to be used
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
|
|
|
* @since CakePHP(tm) v 1.2
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-10-18 03:09:30 +00:00
|
|
|
App::import('Core', 'File');
|
2009-06-09 03:15:05 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Task class for creating a plugin
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class PluginTask extends Shell {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* path to CONTROLLERS directory
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $path = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* initialize
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function initialize() {
|
|
|
|
$this->path = APP . 'plugins' . DS;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Execution method always used for tasks
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
public function execute() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$plugin = null;
|
2009-06-11 04:04:22 +00:00
|
|
|
|
2008-10-23 00:10:44 +00:00
|
|
|
if (isset($this->args[0])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$plugin = Inflector::camelize($this->args[0]);
|
2009-06-11 04:04:22 +00:00
|
|
|
$pluginPath = $this->_pluginPath($plugin);
|
|
|
|
if (is_dir($pluginPath)) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__('Plugin: %s'), $plugin));
|
|
|
|
$this->out(sprintf(__('Path: %s'), $pluginPath));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-04-24 01:57:59 +00:00
|
|
|
$this->_interactive($plugin);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-20 04:20:58 +00:00
|
|
|
} else {
|
2010-04-24 01:57:59 +00:00
|
|
|
return $this->_interactive();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Interactive interface
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
protected function _interactive($plugin = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
while ($plugin === null) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$plugin = $this->in(__('Enter the name of the plugin in CamelCase format'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->bake($plugin)) {
|
2010-10-18 03:09:30 +00:00
|
|
|
$this->error(sprintf(__("An error occured trying to bake: %s in %s"), $plugin, $this->path . Inflector::underscore($pluginPath)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Bake the plugin, create directories and files
|
|
|
|
*
|
|
|
|
* @params $plugin name of the plugin in CamelCased format
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
public function bake($plugin) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$pluginPath = Inflector::underscore($plugin);
|
|
|
|
|
2009-07-09 02:25:27 +00:00
|
|
|
$pathOptions = App::path('plugins');
|
2009-06-11 02:52:38 +00:00
|
|
|
if (count($pathOptions) > 1) {
|
|
|
|
$this->findPath($pathOptions);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
2010-10-18 03:09:30 +00:00
|
|
|
$this->out(sprintf(__("<info>Plugin Name:</info> %s"), $plugin));
|
|
|
|
$this->out(sprintf(__("<info>Plugin Directory:</info> %s"), $this->path . $pluginPath));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
|
|
|
|
2010-04-15 15:43:39 +00:00
|
|
|
$looksGood = $this->in(__('Look okay?'), array('y', 'n', 'q'), 'y');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-06-11 02:19:14 +00:00
|
|
|
if (strtolower($looksGood) == 'y') {
|
2009-08-02 21:26:15 +00:00
|
|
|
$Folder =& new Folder($this->path . $pluginPath);
|
2009-08-02 21:39:59 +00:00
|
|
|
$directories = array(
|
|
|
|
'config' . DS . 'schema',
|
|
|
|
'models' . DS . 'behaviors',
|
|
|
|
'models' . DS . 'datasources',
|
2010-10-18 03:09:30 +00:00
|
|
|
'console' . DS . 'shells' . DS . 'tasks',
|
2009-08-02 21:39:59 +00:00
|
|
|
'controllers' . DS . 'components',
|
2009-10-28 13:21:42 +00:00
|
|
|
'libs',
|
2009-08-02 21:39:59 +00:00
|
|
|
'views' . DS . 'helpers',
|
|
|
|
'tests' . DS . 'cases' . DS . 'components',
|
|
|
|
'tests' . DS . 'cases' . DS . 'helpers',
|
|
|
|
'tests' . DS . 'cases' . DS . 'behaviors',
|
|
|
|
'tests' . DS . 'cases' . DS . 'controllers',
|
|
|
|
'tests' . DS . 'cases' . DS . 'models',
|
|
|
|
'tests' . DS . 'groups',
|
|
|
|
'tests' . DS . 'fixtures',
|
2009-11-26 03:50:50 +00:00
|
|
|
'vendors',
|
|
|
|
'webroot'
|
2009-08-02 21:39:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($directories as $directory) {
|
|
|
|
$dirPath = $this->path . $pluginPath . DS . $directory;
|
2009-08-02 21:26:15 +00:00
|
|
|
$Folder->create($dirPath);
|
|
|
|
$File =& new File($dirPath . DS . 'empty', true);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-10-18 03:09:30 +00:00
|
|
|
foreach ($Folder->messages() as $message) {
|
|
|
|
$this->out($message, 1, Shell::VERBOSE);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$errors = $Folder->errors();
|
|
|
|
if (!empty($errors)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$controllerFileName = $pluginPath . '_app_controller.php';
|
|
|
|
|
|
|
|
$out = "<?php\n\n";
|
|
|
|
$out .= "class {$plugin}AppController extends AppController {\n\n";
|
|
|
|
$out .= "}\n\n";
|
2008-07-16 12:34:03 +00:00
|
|
|
$out .= "?>";
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->createFile($this->path . $pluginPath. DS . $controllerFileName, $out);
|
|
|
|
|
|
|
|
$modelFileName = $pluginPath . '_app_model.php';
|
|
|
|
|
|
|
|
$out = "<?php\n\n";
|
|
|
|
$out .= "class {$plugin}AppModel extends AppModel {\n\n";
|
|
|
|
$out .= "}\n\n";
|
2008-07-16 12:34:03 +00:00
|
|
|
$out .= "?>";
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->createFile($this->path . $pluginPath . DS . $modelFileName, $out);
|
|
|
|
|
|
|
|
$this->hr();
|
2010-10-18 03:09:30 +00:00
|
|
|
$this->out(sprintf(__('<success>Created:</success> %s in %s'), $plugin, $this->path . $pluginPath), 2);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-11-08 02:54:07 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-06-11 02:52:38 +00:00
|
|
|
/**
|
|
|
|
* find and change $this->path to the user selection
|
|
|
|
*
|
2010-04-24 01:57:59 +00:00
|
|
|
* @return string plugin path
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
public function findPath($pathOptions) {
|
2009-06-11 02:52:38 +00:00
|
|
|
$valid = false;
|
|
|
|
$max = count($pathOptions);
|
|
|
|
while (!$valid) {
|
|
|
|
foreach ($pathOptions as $i => $option) {
|
|
|
|
$this->out($i + 1 .'. ' . $option);
|
|
|
|
}
|
2010-04-15 15:43:39 +00:00
|
|
|
$prompt = __('Choose a plugin path from the paths above.');
|
2009-06-11 02:52:38 +00:00
|
|
|
$choice = $this->in($prompt);
|
|
|
|
if (intval($choice) > 0 && intval($choice) <= $max) {
|
|
|
|
$valid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->path = $pathOptions[$choice - 1];
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-18 03:09:30 +00:00
|
|
|
/**
|
|
|
|
* get the option parser for the plugin task
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function getOptionParser() {
|
|
|
|
$parser = parent::getOptionParser();
|
|
|
|
return $parser->description(
|
|
|
|
'Create the directory structure, AppModel and AppController classes for a new plugin. ' .
|
|
|
|
'Can create plugins in any of your bootstrapped plugin paths.'
|
|
|
|
)->addArgument('name', array(
|
|
|
|
'help' => __('CamelCased name of the plugin to create.')
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
2010-10-18 03:31:58 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|