2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* The Plugin Task handles creating an empty plugin, ready to be used
|
|
|
|
*
|
2010-10-03 12:38:58 -04:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 17:46:59 +11:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-12 22:46:46 -04:00
|
|
|
* Copyright 2005-2012, 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.
|
|
|
|
*
|
2012-03-12 22:46:46 -04:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 17:00:11 +11: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 17:51:51 +11:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-08 23:15:18 -04:30
|
|
|
|
2011-11-26 02:35:00 +05:30
|
|
|
App::uses('AppShell', 'Console/Command');
|
2010-12-08 23:15:18 -04:30
|
|
|
App::uses('File', 'Utility');
|
2011-03-08 00:50:07 -04:30
|
|
|
App::uses('Folder', 'Utility');
|
2009-06-08 23:15:05 -04:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-12-08 07:35:02 -08:00
|
|
|
* The Plugin Task handles creating an empty plugin, ready to be used
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Console.Command.Task
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-11-26 02:35:00 +05:30
|
|
|
class PluginTask extends AppShell {
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-04-17 11:13:04 +02:00
|
|
|
* path to plugins directory
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $path = null;
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* initialize
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function initialize() {
|
2011-05-15 19:28:17 +02:00
|
|
|
$this->path = current(App::path('plugins'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Execution method always used for tasks
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-23 21:57:59 -04: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 00:04:22 -04:00
|
|
|
$pluginPath = $this->_pluginPath($plugin);
|
|
|
|
if (is_dir($pluginPath)) {
|
2011-03-19 18:32:35 +01: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-23 21:57:59 -04:00
|
|
|
$this->_interactive($plugin);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-20 00:20:58 -04:00
|
|
|
} else {
|
2010-04-23 21:57:59 -04:00
|
|
|
return $this->_interactive();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Interactive interface
|
|
|
|
*
|
2011-07-28 22:03:44 -04:00
|
|
|
* @param string $plugin
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-23 21:57:59 -04:00
|
|
|
protected function _interactive($plugin = null) {
|
2011-07-28 23:25:21 -04:00
|
|
|
while ($plugin === null) {
|
2011-03-19 18:32:35 +01: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-12-01 21:58:09 -08:00
|
|
|
$this->error(__d('cake_console', "An error occurred trying to bake: %s in %s", $plugin, $this->path . $plugin));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Bake the plugin, create directories and files
|
|
|
|
*
|
2011-07-28 22:03:44 -04:00
|
|
|
* @param string $plugin Name of the plugin in CamelCased format
|
|
|
|
* @return boolean
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-23 21:57:59 -04:00
|
|
|
public function bake($plugin) {
|
2009-07-08 22:25:27 -04:00
|
|
|
$pathOptions = App::path('plugins');
|
2009-06-10 22:52:38 -04:00
|
|
|
if (count($pathOptions) > 1) {
|
|
|
|
$this->findPath($pathOptions);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
2011-05-15 19:28:17 +02:00
|
|
|
$this->out(__d('cake_console', "<info>Plugin Name:</info> %s", $plugin));
|
2011-05-25 21:14:23 +02: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 18:32:35 +01:00
|
|
|
$looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-06-10 22:19:14 -04:00
|
|
|
if (strtolower($looksGood) == 'y') {
|
2011-05-25 21:14:23 +02:00
|
|
|
$Folder = new Folder($this->path . $plugin);
|
2009-08-02 23:39:59 +02:00
|
|
|
$directories = array(
|
2011-07-28 18:57:10 +02:00
|
|
|
'Config' . DS . 'Schema',
|
2011-04-20 11:32:16 -04:30
|
|
|
'Model' . DS . 'Behavior',
|
|
|
|
'Model' . DS . 'Datasource',
|
|
|
|
'Console' . DS . 'Command' . DS . 'Task',
|
|
|
|
'Controller' . DS . 'Component',
|
|
|
|
'Lib',
|
|
|
|
'View' . DS . 'Helper',
|
2011-05-19 13:06:12 -04: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-25 22:50:50 -05:00
|
|
|
'webroot'
|
2009-08-02 23:39:59 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($directories as $directory) {
|
2011-05-25 21:14:23 +02:00
|
|
|
$dirPath = $this->path . $plugin . DS . $directory;
|
2009-08-02 23:26:15 +02:00
|
|
|
$Folder->create($dirPath);
|
2010-11-12 23:05:44 -05:00
|
|
|
$File = new File($dirPath . DS . 'empty', true);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-10-17 23:09:30 -04: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 11:32:16 -04:30
|
|
|
$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";
|
2012-03-03 18:55:07 -05:00
|
|
|
$this->createFile($this->path . $plugin . DS . 'Controller' . DS . $controllerFileName, $out);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-04-20 11:32:16 -04:30
|
|
|
$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";
|
2011-05-25 21:14:23 +02:00
|
|
|
$this->createFile($this->path . $plugin . DS . 'Model' . DS . $modelFileName, $out);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$this->hr();
|
2011-05-25 21:14:23 +02: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 21:18:37 +02:00
|
|
|
|
2009-06-10 22:52:38 -04:00
|
|
|
/**
|
|
|
|
* find and change $this->path to the user selection
|
|
|
|
*
|
2011-07-28 22:03:44 -04:00
|
|
|
* @param array $pathOptions
|
2010-04-23 21:57:59 -04:00
|
|
|
* @return string plugin path
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2010-04-23 21:57:59 -04:00
|
|
|
public function findPath($pathOptions) {
|
2009-06-10 22:52:38 -04:00
|
|
|
$valid = false;
|
2011-11-30 07:44:11 -08:00
|
|
|
foreach ($pathOptions as $i => $path) {
|
|
|
|
if (!is_dir($path)) {
|
2011-05-22 22:03:51 -04:00
|
|
|
array_splice($pathOptions, $i, 1);
|
|
|
|
}
|
|
|
|
}
|
2009-06-10 22:52:38 -04:00
|
|
|
$max = count($pathOptions);
|
|
|
|
while (!$valid) {
|
|
|
|
foreach ($pathOptions as $i => $option) {
|
2012-03-03 18:55:07 -05:00
|
|
|
$this->out($i + 1 . '. ' . $option);
|
2009-06-10 22:52:38 -04:00
|
|
|
}
|
2011-03-19 18:32:35 +01:00
|
|
|
$prompt = __d('cake_console', 'Choose a plugin path from the paths above.');
|
2009-06-10 22:52:38 -04:00
|
|
|
$choice = $this->in($prompt);
|
|
|
|
if (intval($choice) > 0 && intval($choice) <= $max) {
|
|
|
|
$valid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->path = $pathOptions[$choice - 1];
|
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2010-10-17 23:09:30 -04:00
|
|
|
/**
|
|
|
|
* get the option parser for the plugin task
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function getOptionParser() {
|
|
|
|
$parser = parent::getOptionParser();
|
2011-04-17 11:13:04 +02:00
|
|
|
return $parser->description(__d('cake_console',
|
2010-10-17 23:09:30 -04: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 21:17:59 +02:00
|
|
|
))->addArgument('name', array(
|
2011-03-19 18:32:35 +01:00
|
|
|
'help' => __d('cake_console', 'CamelCased name of the plugin to create.')
|
2010-10-17 23:09:30 -04:00
|
|
|
));
|
|
|
|
}
|
2010-10-17 23:31:58 -04:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|