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

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.4
      • 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:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7:  *
  8:  * Licensed under The MIT License
  9:  * For full copyright and license information, please see the LICENSE.txt
 10:  * Redistributions of files must retain the above copyright notice.
 11:  *
 12:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 13:  * @link          http://cakephp.org CakePHP(tm) Project
 14:  * @since         CakePHP(tm) v 1.2
 15:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 16:  */
 17: 
 18: App::uses('AppShell', 'Console/Command');
 19: App::uses('File', 'Utility');
 20: App::uses('Folder', 'Utility');
 21: 
 22: /**
 23:  * The Plugin Task handles creating an empty plugin, ready to be used
 24:  *
 25:  * @package       Cake.Console.Command.Task
 26:  */
 27: class PluginTask extends AppShell {
 28: 
 29: /**
 30:  * path to plugins directory
 31:  *
 32:  * @var array
 33:  */
 34:     public $path = null;
 35: 
 36: /**
 37:  * Path to the bootstrap file. Changed in tests.
 38:  *
 39:  * @var string
 40:  */
 41:     public $bootstrap = null;
 42: 
 43: /**
 44:  * initialize
 45:  *
 46:  * @return void
 47:  */
 48:     public function initialize() {
 49:         $this->path = current(App::path('plugins'));
 50:         $this->bootstrap = APP . 'Config' . DS . 'bootstrap.php';
 51:     }
 52: 
 53: /**
 54:  * Execution method always used for tasks
 55:  *
 56:  * @return void
 57:  */
 58:     public function execute() {
 59:         if (isset($this->args[0])) {
 60:             $plugin = Inflector::camelize($this->args[0]);
 61:             $pluginPath = $this->_pluginPath($plugin);
 62:             if (is_dir($pluginPath)) {
 63:                 $this->out(__d('cake_console', 'Plugin: %s already exists, no action taken', $plugin));
 64:                 $this->out(__d('cake_console', 'Path: %s', $pluginPath));
 65:                 return false;
 66:             }
 67:             $this->_interactive($plugin);
 68:         } else {
 69:             return $this->_interactive();
 70:         }
 71:     }
 72: 
 73: /**
 74:  * Interactive interface
 75:  *
 76:  * @param string $plugin
 77:  * @return void
 78:  */
 79:     protected function _interactive($plugin = null) {
 80:         while ($plugin === null) {
 81:             $plugin = $this->in(__d('cake_console', 'Enter the name of the plugin in CamelCase format'));
 82:         }
 83: 
 84:         if (!$this->bake($plugin)) {
 85:             $this->error(__d('cake_console', "An error occurred trying to bake: %s in %s", $plugin, $this->path . $plugin));
 86:         }
 87:     }
 88: 
 89: /**
 90:  * Bake the plugin, create directories and files
 91:  *
 92:  * @param string $plugin Name of the plugin in CamelCased format
 93:  * @return boolean
 94:  */
 95:     public function bake($plugin) {
 96:         $pathOptions = App::path('plugins');
 97:         if (count($pathOptions) > 1) {
 98:             $this->findPath($pathOptions);
 99:         }
100:         $this->hr();
101:         $this->out(__d('cake_console', "<info>Plugin Name:</info> %s", $plugin));
102:         $this->out(__d('cake_console', "<info>Plugin Directory:</info> %s", $this->path . $plugin));
103:         $this->hr();
104: 
105:         $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
106: 
107:         if (strtolower($looksGood) === 'y') {
108:             $Folder = new Folder($this->path . $plugin);
109:             $directories = array(
110:                 'Config' . DS . 'Schema',
111:                 'Model' . DS . 'Behavior',
112:                 'Model' . DS . 'Datasource',
113:                 'Console' . DS . 'Command' . DS . 'Task',
114:                 'Controller' . DS . 'Component',
115:                 'Lib',
116:                 'View' . DS . 'Helper',
117:                 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
118:                 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
119:                 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
120:                 'Test' . DS . 'Fixture',
121:                 'Vendor',
122:                 'webroot'
123:             );
124: 
125:             foreach ($directories as $directory) {
126:                 $dirPath = $this->path . $plugin . DS . $directory;
127:                 $Folder->create($dirPath);
128:                 new File($dirPath . DS . 'empty', true);
129:             }
130: 
131:             foreach ($Folder->messages() as $message) {
132:                 $this->out($message, 1, Shell::VERBOSE);
133:             }
134: 
135:             $errors = $Folder->errors();
136:             if (!empty($errors)) {
137:                 foreach ($errors as $message) {
138:                     $this->error($message);
139:                 }
140:                 return false;
141:             }
142: 
143:             $controllerFileName = $plugin . 'AppController.php';
144: 
145:             $out = "<?php\n\n";
146:             $out .= "App::uses('AppController', 'Controller');\n\n";
147:             $out .= "class {$plugin}AppController extends AppController {\n\n";
148:             $out .= "}\n";
149:             $this->createFile($this->path . $plugin . DS . 'Controller' . DS . $controllerFileName, $out);
150: 
151:             $modelFileName = $plugin . 'AppModel.php';
152: 
153:             $out = "<?php\n\n";
154:             $out .= "App::uses('AppModel', 'Model');\n\n";
155:             $out .= "class {$plugin}AppModel extends AppModel {\n\n";
156:             $out .= "}\n";
157:             $this->createFile($this->path . $plugin . DS . 'Model' . DS . $modelFileName, $out);
158: 
159:             $this->_modifyBootstrap($plugin);
160: 
161:             $this->hr();
162:             $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $plugin, $this->path . $plugin), 2);
163:         }
164: 
165:         return true;
166:     }
167: 
168: /**
169:  * Update the app's bootstrap.php file.
170:  *
171:  * @param string $plugin Name of plugin
172:  * @return void
173:  */
174:     protected function _modifyBootstrap($plugin) {
175:         $bootstrap = new File($this->bootstrap, false);
176:         $contents = $bootstrap->read();
177:         if (!preg_match("@\n\s*CakePlugin::loadAll@", $contents)) {
178:             $bootstrap->append("\nCakePlugin::load('$plugin', array('bootstrap' => false, 'routes' => false));\n");
179:             $this->out('');
180:             $this->out(__d('cake_dev', '%s modified', $this->bootstrap));
181:         }
182:     }
183: 
184: /**
185:  * find and change $this->path to the user selection
186:  *
187:  * @param array $pathOptions
188:  * @return void
189:  */
190:     public function findPath($pathOptions) {
191:         $valid = false;
192:         foreach ($pathOptions as $i => $path) {
193:             if (!is_dir($path)) {
194:                 unset($pathOptions[$i]);
195:             }
196:         }
197:         $pathOptions = array_values($pathOptions);
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