plugin.php

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

Generated on Sun Nov 22 00:30:52 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7