plugin.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: plugin_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * The Plugin Task handles creating an empty plugin, ready to be used
00005  *
00006  * Long description for file
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.console.libs.tasks
00023  * @since           CakePHP(tm) v 1.2
00024  * @version         $Revision: 580 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 if (!class_exists('File')) {
00030     uses('file');
00031 }
00032 /**
00033  * Task class for creating a plugin
00034  *
00035  * @package     cake
00036  * @subpackage  cake.cake.console.libs.tasks
00037  */
00038 class PluginTask extends Shell {
00039 /**
00040  * Tasks
00041  *
00042  */
00043     var $tasks = array('Model', 'Controller', 'View');
00044 /**
00045  * path to CONTROLLERS directory
00046  *
00047  * @var array
00048  * @access public
00049  */
00050     var $path = null;
00051 /**
00052  * initialize
00053  *
00054  * @return void
00055  */
00056     function initialize() {
00057         $this->path = APP . 'plugins' . DS;
00058     }
00059 /**
00060  * Execution method always used for tasks
00061  *
00062  * @return void
00063  */
00064     function execute() {
00065         if (empty($this->params['skel'])) {
00066             $this->params['skel'] = '';
00067             if (is_dir(CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel') === true) {
00068                 $this->params['skel'] = CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel';
00069             }
00070         }
00071 
00072         $plugin = null;
00073 
00074         if(isset($this->args[0])) {
00075             $plugin = Inflector::camelize($this->args[0]);
00076             $pluginPath = Inflector::underscore($plugin) . DS;
00077             $this->Dispatch->shiftArgs();
00078             if (is_dir($this->path . $pluginPath)) {
00079                 $this->out(sprintf('Plugin: %s', $plugin));
00080                 $this->out(sprintf('Path: %s', $this->path . $pluginPath));
00081                 $this->hr();
00082             } elseif (isset($this->args[0])) {
00083                 $this->err(sprintf('%s in path %s not found.', $plugin, $this->path . $pluginPath));
00084                 $this->_stop();
00085             } else {
00086                 $this->__interactive($plugin);
00087             }
00088         }
00089         
00090         if(isset($this->args[0])) {
00091             $task = Inflector::classify($this->args[0]);
00092             $this->Dispatch->shiftArgs();
00093             if (in_array($task, $this->tasks)) {
00094                 $this->{$task}->plugin = $plugin;
00095                 $this->{$task}->path = $this->path . $pluginPath . Inflector::underscore(Inflector::pluralize($task)) . DS;
00096 
00097                 if (!is_dir($this->{$task}->path)) {
00098                     $this->err(sprintf(__("%s directory could not be found.\nBe sure you have created %s", true), $task, $this->{$task}->path));
00099                 }
00100                 $this->{$task}->loadTasks();
00101                 $this->{$task}->execute();
00102             }
00103         }
00104     }
00105 
00106 /**
00107  * Interactive interface
00108  *
00109  * @access private
00110  * @return void
00111  */
00112     function __interactive($plugin = null) {
00113         while ($plugin === null) {
00114             $plugin = $this->in(__('Enter the name of the plugin in CamelCase format', true));
00115         }
00116 
00117         if (!$this->bake($plugin)) {
00118             $this->err(sprintf(__("An error occured trying to bake: %s in %s", true), $plugin, $this->path . $pluginPath));
00119         }
00120     }
00121 
00122 /**
00123  * Bake the plugin, create directories and files
00124  *
00125  * @params $plugin name of the plugin in CamelCased format
00126  * @access public
00127  * @return bool
00128  */
00129     function bake($plugin) {
00130 
00131         $pluginPath = Inflector::underscore($plugin);
00132 
00133         $this->hr();
00134         $this->out("Plugin Name: $plugin");
00135         $this->out("Plugin Directory: {$this->path}{$pluginPath}");
00136         $this->hr();
00137 
00138 
00139         $looksGood = $this->in('Look okay?', array('y', 'n', 'q'), 'y');
00140 
00141         if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
00142             $verbose = $this->in(__('Do you want verbose output?', true), array('y', 'n'), 'n');
00143 
00144             $Folder = new Folder($this->path . $pluginPath);
00145             $directories = array('models' . DS . 'behaviors', 'controllers' . DS . 'components', 'views' . DS . 'helpers');
00146 
00147             foreach ($directories as $directory) {
00148                 $Folder->create($this->path . $pluginPath . DS . $directory);
00149             }
00150 
00151             if (low($verbose) == 'y' || low($verbose) == 'yes') {
00152                 foreach ($Folder->messages() as $message) {
00153                     $this->out($message);
00154                 }
00155             }
00156 
00157             $errors = $Folder->errors();
00158             if (!empty($errors)) {
00159                 return false;
00160             }
00161 
00162             $controllerFileName = $pluginPath . '_app_controller.php';
00163 
00164             $out = "<?php\n\n";
00165             $out .= "class {$plugin}AppController extends AppController {\n\n";
00166             $out .= "}\n\n";
00167             $out .= "?>\n";
00168             $this->createFile($this->path . $pluginPath. DS . $controllerFileName, $out);
00169 
00170             $modelFileName = $pluginPath . '_app_model.php';
00171 
00172             $out = "<?php\n\n";
00173             $out .= "class {$plugin}AppModel extends AppModel {\n\n";
00174             $out .= "}\n\n";
00175             $out .= "?>\n";
00176             $this->createFile($this->path . $pluginPath . DS . $modelFileName, $out);
00177 
00178             $this->hr();
00179             $this->out(sprintf(__("Created: %s in %s", true), $plugin, $this->path . $pluginPath));
00180             $this->hr();
00181         }
00182         
00183         return true;
00184     }
00185 /**
00186  * Help
00187  *
00188  * @return void
00189  * @access public
00190  */
00191     function help() {
00192         $this->hr();
00193         $this->out("Usage: cake bake plugin <arg1> <arg2>...");
00194         $this->hr();
00195         $this->out('Commands:');
00196         $this->out("\n\tplugin <name>\n\t\tbakes plugin directory structure");
00197         $this->out("\n\tplugin <name> model\n\t\tbakes model. Run 'cake bake model help' for more info.");
00198         $this->out("\n\tplugin <name> controller\n\t\tbakes controller. Run 'cake bake controller help' for more info.");
00199         $this->out("\n\tplugin <name> view\n\t\tbakes view. Run 'cake bake view help' for more info.");
00200         $this->out("");
00201         $this->_stop();
00202     }
00203 }
00204 ?>