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.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.3
      • 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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * For full copyright and license information, please see the LICENSE.txt
 12:  * Redistributions of files must retain the above copyright notice.
 13:  *
 14:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 15:  * @link          http://cakephp.org CakePHP(tm) Project
 16:  * @since         CakePHP(tm) v 1.2
 17:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 18:  */
 19: 
 20: App::uses('AppShell', 'Console/Command');
 21: App::uses('File', 'Utility');
 22: App::uses('Folder', 'Utility');
 23: 
 24: /**
 25:  * The Plugin Task handles creating an empty plugin, ready to be used
 26:  *
 27:  * @package       Cake.Console.Command.Task
 28:  */
 29: class PluginTask extends AppShell {
 30: 
 31: /**
 32:  * path to plugins directory
 33:  *
 34:  * @var array
 35:  */
 36:     public $path = null;
 37: 
 38: /**
 39:  * Path to the bootstrap file. Changed in tests.
 40:  *
 41:  * @var string
 42:  */
 43:     public $bootstrap = null;
 44: 
 45: /**
 46:  * initialize
 47:  *
 48:  * @return void
 49:  */
 50:     public function initialize() {
 51:         $this->path = current(App::path('plugins'));
 52:         $this->bootstrap = APP . 'Config' . DS . 'bootstrap.php';
 53:     }
 54: 
 55: /**
 56:  * Execution method always used for tasks
 57:  *
 58:  * @return void
 59:  */
 60:     public function execute() {
 61:         if (isset($this->args[0])) {
 62:             $plugin = Inflector::camelize($this->args[0]);
 63:             $pluginPath = $this->_pluginPath($plugin);
 64:             if (is_dir($pluginPath)) {
 65:                 $this->out(__d('cake_console', 'Plugin: %s already exists, no action taken', $plugin));
 66:                 $this->out(__d('cake_console', 'Path: %s', $pluginPath));
 67:                 return false;
 68:             }
 69:             $this->_interactive($plugin);
 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:                 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:  * @param string $plugin Name of plugin
174:  * @return void
175:  */
176:     protected function _modifyBootstrap($plugin) {
177:         $bootstrap = new File($this->bootstrap, false);
178:         $contents = $bootstrap->read();
179:         if (!preg_match("@\n\s*CakePlugin::loadAll@", $contents)) {
180:             $bootstrap->append("\nCakePlugin::load('$plugin', array('bootstrap' => false, 'routes' => false));\n");
181:             $this->out('');
182:             $this->out(__d('cake_dev', '%s modified', $this->bootstrap));
183:         }
184:     }
185: 
186: /**
187:  * find and change $this->path to the user selection
188:  *
189:  * @param array $pathOptions
190:  * @return void
191:  */
192:     public function findPath($pathOptions) {
193:         $valid = false;
194:         foreach ($pathOptions as $i => $path) {
195:             if (!is_dir($path)) {
196:                 array_splice($pathOptions, $i, 1);
197:             }
198:         }
199:         $max = count($pathOptions);
200:         while (!$valid) {
201:             foreach ($pathOptions as $i => $option) {
202:                 $this->out($i + 1 . '. ' . $option);
203:             }
204:             $prompt = __d('cake_console', 'Choose a plugin path from the paths above.');
205:             $choice = $this->in($prompt, null, 1);
206:             if (intval($choice) > 0 && intval($choice) <= $max) {
207:                 $valid = true;
208:             }
209:         }
210:         $this->path = $pathOptions[$choice - 1];
211:     }
212: 
213: /**
214:  * get the option parser for the plugin task
215:  *
216:  * @return void
217:  */
218:     public function getOptionParser() {
219:         $parser = parent::getOptionParser();
220:         return $parser->description(__d('cake_console',
221:             'Create the directory structure, AppModel and AppController classes for a new plugin. ' .
222:             'Can create plugins in any of your bootstrapped plugin paths.'
223:         ))->addArgument('name', array(
224:             'help' => __d('cake_console', 'CamelCased name of the plugin to create.')
225:         ));
226:     }
227: 
228: }
229: 
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