CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • BakeTask
  • ControllerTask
  • DbConfigTask
  • ExtractTask
  • FixtureTask
  • ModelTask
  • PluginTask
  • ProjectTask
  • TemplateTask
  • TestTask
  • ViewTask
  1: <?php
  2: /**
  3:  * The Plugin Task handles creating an empty plugin, ready to be used
  4:  *
  5:  * PHP 5
  6:  *
  7:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * Redistributions of files must retain the above copyright notice.
 12:  *
 13:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14:  * @link          http://cakephp.org CakePHP(tm) Project
 15:  * @since         CakePHP(tm) v 1.2
 16:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 17:  */
 18: 
 19: App::uses('AppShell', 'Console/Command');
 20: App::uses('File', 'Utility');
 21: App::uses('Folder', 'Utility');
 22: 
 23: /**
 24:  * The Plugin Task handles creating an empty plugin, ready to be used
 25:  *
 26:  * @package       Cake.Console.Command.Task
 27:  */
 28: class PluginTask extends AppShell {
 29: 
 30: /**
 31:  * path to plugins directory
 32:  *
 33:  * @var array
 34:  */
 35:     public $path = null;
 36: 
 37: /**
 38:  * Path to the bootstrap file. Changed in tests.
 39:  *
 40:  * @var string
 41:  */
 42:     public $bootstrap = null;
 43: 
 44: /**
 45:  * initialize
 46:  *
 47:  * @return void
 48:  */
 49:     public function initialize() {
 50:         $this->path = current(App::path('plugins'));
 51:         $this->bootstrap = APP . 'Config' . DS . 'bootstrap.php';
 52:     }
 53: 
 54: /**
 55:  * Execution method always used for tasks
 56:  *
 57:  * @return void
 58:  */
 59:     public function execute() {
 60:         if (isset($this->args[0])) {
 61:             $plugin = Inflector::camelize($this->args[0]);
 62:             $pluginPath = $this->_pluginPath($plugin);
 63:             if (is_dir($pluginPath)) {
 64:                 $this->out(__d('cake_console', 'Plugin: %s already exists, no action taken', $plugin));
 65:                 $this->out(__d('cake_console', 'Path: %s', $pluginPath));
 66:                 return false;
 67:             } else {
 68:                 $this->_interactive($plugin);
 69:             }
 70:         } else {
 71:             return $this->_interactive();
 72:         }
 73:     }
 74: 
 75: /**
 76:  * Interactive interface
 77:  *
 78:  * @param string $plugin
 79:  * @return void
 80:  */
 81:     protected function _interactive($plugin = null) {
 82:         while ($plugin === null) {
 83:             $plugin = $this->in(__d('cake_console', 'Enter the name of the plugin in CamelCase format'));
 84:         }
 85: 
 86:         if (!$this->bake($plugin)) {
 87:             $this->error(__d('cake_console', "An error occurred trying to bake: %s in %s", $plugin, $this->path . $plugin));
 88:         }
 89:     }
 90: 
 91: /**
 92:  * Bake the plugin, create directories and files
 93:  *
 94:  * @param string $plugin Name of the plugin in CamelCased format
 95:  * @return boolean
 96:  */
 97:     public function bake($plugin) {
 98:         $pathOptions = App::path('plugins');
 99:         if (count($pathOptions) > 1) {
100:             $this->findPath($pathOptions);
101:         }
102:         $this->hr();
103:         $this->out(__d('cake_console', "<info>Plugin Name:</info> %s", $plugin));
104:         $this->out(__d('cake_console', "<info>Plugin Directory:</info> %s", $this->path . $plugin));
105:         $this->hr();
106: 
107:         $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
108: 
109:         if (strtolower($looksGood) == 'y') {
110:             $Folder = new Folder($this->path . $plugin);
111:             $directories = array(
112:                 'Config' . DS . 'Schema',
113:                 'Model' . DS . 'Behavior',
114:                 'Model' . DS . 'Datasource',
115:                 'Console' . DS . 'Command' . DS . 'Task',
116:                 'Controller' . DS . 'Component',
117:                 'Lib',
118:                 'View' . DS . 'Helper',
119:                 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
120:                 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
121:                 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
122:                 'Test' . DS . 'Fixture',
123:                 'Vendor',
124:                 'webroot'
125:             );
126: 
127:             foreach ($directories as $directory) {
128:                 $dirPath = $this->path . $plugin . DS . $directory;
129:                 $Folder->create($dirPath);
130:                 $File = new File($dirPath . DS . 'empty', true);
131:             }
132: 
133:             foreach ($Folder->messages() as $message) {
134:                 $this->out($message, 1, Shell::VERBOSE);
135:             }
136: 
137:             $errors = $Folder->errors();
138:             if (!empty($errors)) {
139:                 foreach ($errors as $message) {
140:                     $this->error($message);
141:                 }
142:                 return false;
143:             }
144: 
145:             $controllerFileName = $plugin . 'AppController.php';
146: 
147:             $out = "<?php\n\n";
148:             $out .= "App::uses('AppController', 'Controller');\n\n";
149:             $out .= "class {$plugin}AppController extends AppController {\n\n";
150:             $out .= "}\n";
151:             $this->createFile($this->path . $plugin . DS . 'Controller' . DS . $controllerFileName, $out);
152: 
153:             $modelFileName = $plugin . 'AppModel.php';
154: 
155:             $out = "<?php\n\n";
156:             $out .= "App::uses('AppModel', 'Model');\n\n";
157:             $out .= "class {$plugin}AppModel extends AppModel {\n\n";
158:             $out .= "}\n";
159:             $this->createFile($this->path . $plugin . DS . 'Model' . DS . $modelFileName, $out);
160: 
161:             $this->_modifyBootstrap($plugin);
162: 
163:             $this->hr();
164:             $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $plugin, $this->path . $plugin), 2);
165:         }
166: 
167:         return true;
168:     }
169: 
170: /**
171:  * Update the app's bootstrap.php file.
172:  *
173:  * @return void
174:  */
175:     protected function _modifyBootstrap($plugin) {
176:         $bootstrap = new File($this->bootstrap, false);
177:         $contents = $bootstrap->read();
178:         if (!preg_match("@\n\s*CakePlugin::loadAll@", $contents)) {
179:             $bootstrap->append("\nCakePlugin::load('$plugin', array('bootstrap' => false, 'routes' => false));\n");
180:             $this->out('');
181:             $this->out(__d('cake_dev', '%s modified', $this->bootstrap));
182:         }
183:     }
184: 
185: /**
186:  * find and change $this->path to the user selection
187:  *
188:  * @param array $pathOptions
189:  * @return string plugin path
190:  */
191:     public function findPath($pathOptions) {
192:         $valid = false;
193:         foreach ($pathOptions as $i => $path) {
194:             if (!is_dir($path)) {
195:                 array_splice($pathOptions, $i, 1);
196:             }
197:         }
198:         $max = count($pathOptions);
199:         while (!$valid) {
200:             foreach ($pathOptions as $i => $option) {
201:                 $this->out($i + 1 . '. ' . $option);
202:             }
203:             $prompt = __d('cake_console', 'Choose a plugin path from the paths above.');
204:             $choice = $this->in($prompt, null, 1);
205:             if (intval($choice) > 0 && intval($choice) <= $max) {
206:                 $valid = true;
207:             }
208:         }
209:         $this->path = $pathOptions[$choice - 1];
210:     }
211: 
212: /**
213:  * get the option parser for the plugin task
214:  *
215:  * @return void
216:  */
217:     public function getOptionParser() {
218:         $parser = parent::getOptionParser();
219:         return $parser->description(__d('cake_console',
220:             'Create the directory structure, AppModel and AppController classes for a new plugin. ' .
221:             'Can create plugins in any of your bootstrapped plugin paths.'
222:         ))->addArgument('name', array(
223:             'help' => __d('cake_console', 'CamelCased name of the plugin to create.')
224:         ));
225:     }
226: 
227: }
228: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs