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)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, 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.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, 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
|
|
|
* @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-12-09 03:45:18 +00:00
|
|
|
|
|
|
|
App::uses('File', 'Utility');
|
2011-03-08 05:20:07 +00:00
|
|
|
App::uses('Folder', 'Utility');
|
2009-06-09 03:15:05 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Task class for creating a plugin
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Console.Command.Task
|
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
|
|
|
/**
|
2011-04-17 09:13:04 +00:00
|
|
|
* path to plugins directory
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
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
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function initialize() {
|
2011-05-15 17:28:17 +00:00
|
|
|
$this->path = current(App::path('plugins'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
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-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)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Plugin: %s', $plugin));
|
|
|
|
$this->out(__d('cake_console', '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
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param string $plugin
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
protected function _interactive($plugin = null) {
|
2011-07-29 03:25:21 +00:00
|
|
|
while ($plugin === null) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$plugin = $this->in(__d('cake_console', 'Enter the name of the plugin in CamelCase format'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->bake($plugin)) {
|
2011-05-25 19:14:23 +00:00
|
|
|
$this->error(__d('cake_console', "An error occured trying to bake: %s in %s", $plugin, $this->path . $plugin));
|
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
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param string $plugin Name of the plugin in CamelCased format
|
|
|
|
* @return boolean
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
public function bake($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();
|
2011-05-15 17:28:17 +00:00
|
|
|
$this->out(__d('cake_console', "<info>Plugin Name:</info> %s", $plugin));
|
2011-05-25 19:14:23 +00:00
|
|
|
$this->out(__d('cake_console', "<info>Plugin Directory:</info> %s", $this->path . $plugin));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
|
|
|
|
2011-03-19 17:32:35 +00:00
|
|
|
$looksGood = $this->in(__d('cake_console', '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') {
|
2011-05-25 19:14:23 +00:00
|
|
|
$Folder = new Folder($this->path . $plugin);
|
2009-08-02 21:39:59 +00:00
|
|
|
$directories = array(
|
2011-07-28 16:57:10 +00:00
|
|
|
'Config' . DS . 'Schema',
|
2011-04-20 16:02:16 +00:00
|
|
|
'Model' . DS . 'Behavior',
|
|
|
|
'Model' . DS . 'Datasource',
|
|
|
|
'Console' . DS . 'Command' . DS . 'Task',
|
|
|
|
'Controller' . DS . 'Component',
|
|
|
|
'Lib',
|
|
|
|
'View' . DS . 'Helper',
|
2011-05-19 17:06:12 +00:00
|
|
|
'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
|
|
|
|
'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
|
|
|
|
'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
|
|
|
|
'Test' . DS . 'Fixture',
|
|
|
|
'Vendor',
|
2009-11-26 03:50:50 +00:00
|
|
|
'webroot'
|
2009-08-02 21:39:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($directories as $directory) {
|
2011-05-25 19:14:23 +00:00
|
|
|
$dirPath = $this->path . $plugin . DS . $directory;
|
2009-08-02 21:26:15 +00:00
|
|
|
$Folder->create($dirPath);
|
2010-11-13 04:05:44 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2011-04-20 16:02:16 +00:00
|
|
|
$controllerFileName = $plugin . 'AppController.php';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$out = "<?php\n\n";
|
|
|
|
$out .= "class {$plugin}AppController extends AppController {\n\n";
|
|
|
|
$out .= "}\n\n";
|
2008-07-16 12:34:03 +00:00
|
|
|
$out .= "?>";
|
2011-05-25 19:14:23 +00:00
|
|
|
$this->createFile($this->path . $plugin. DS . 'Controller' . DS . $controllerFileName, $out);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-04-20 16:02:16 +00:00
|
|
|
$modelFileName = $plugin . 'AppModel.php';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$out = "<?php\n\n";
|
|
|
|
$out .= "class {$plugin}AppModel extends AppModel {\n\n";
|
|
|
|
$out .= "}\n\n";
|
2008-07-16 12:34:03 +00:00
|
|
|
$out .= "?>";
|
2011-05-25 19:14:23 +00:00
|
|
|
$this->createFile($this->path . $plugin . DS . 'Model' . DS . $modelFileName, $out);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$this->hr();
|
2011-05-25 19:14:23 +00:00
|
|
|
$this->out(__d('cake_console', '<success>Created:</success> %s in %s', $plugin, $this->path . $plugin), 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
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param array $pathOptions
|
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;
|
2011-05-23 02:03:51 +00:00
|
|
|
foreach ($pathOptions as $i =>$path) {
|
|
|
|
if(!is_dir($path)) {
|
|
|
|
array_splice($pathOptions, $i, 1);
|
|
|
|
}
|
|
|
|
}
|
2009-06-11 02:52:38 +00:00
|
|
|
$max = count($pathOptions);
|
|
|
|
while (!$valid) {
|
|
|
|
foreach ($pathOptions as $i => $option) {
|
|
|
|
$this->out($i + 1 .'. ' . $option);
|
|
|
|
}
|
2011-03-19 17:32:35 +00:00
|
|
|
$prompt = __d('cake_console', '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();
|
2011-04-17 09:13:04 +00:00
|
|
|
return $parser->description(__d('cake_console',
|
2010-10-18 03:09:30 +00:00
|
|
|
'Create the directory structure, AppModel and AppController classes for a new plugin. ' .
|
|
|
|
'Can create plugins in any of your bootstrapped plugin paths.'
|
2011-04-25 19:17:59 +00:00
|
|
|
))->addArgument('name', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'CamelCased name of the plugin to create.')
|
2010-10-18 03:09:30 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
}
|
2010-10-18 03:31:58 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|