CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.7 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.7
      • 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

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • AssetsTask
  • CommandTask
  • ExtractTask
  • LoadTask
  • UnloadTask
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         3.0.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\Shell\Task;
 16: 
 17: use Cake\Console\Shell;
 18: use Cake\Filesystem\File;
 19: 
 20: /**
 21:  * Task for loading plugins.
 22:  */
 23: class LoadTask extends Shell
 24: {
 25: 
 26:     /**
 27:      * Path to the bootstrap file.
 28:      *
 29:      * @var string
 30:      */
 31:     public $bootstrap;
 32: 
 33:     /**
 34:      * Execution method always used for tasks.
 35:      *
 36:      * @param string|null $plugin The plugin name.
 37:      * @return bool
 38:      */
 39:     public function main($plugin = null)
 40:     {
 41:         $filename = 'bootstrap';
 42:         if ($this->params['cli']) {
 43:             $filename .= '_cli';
 44:         }
 45: 
 46:         $this->bootstrap = ROOT . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . $filename . '.php';
 47: 
 48:         if (!$plugin) {
 49:             $this->err('You must provide a plugin name in CamelCase format.');
 50:             $this->err('To load an "Example" plugin, run `cake plugin load Example`.');
 51: 
 52:             return false;
 53:         }
 54: 
 55:         $options = $this->makeOptions();
 56: 
 57:         $app = APP . 'Application.php';
 58:         if (file_exists($app) && !$this->param('no_app')) {
 59:             $this->modifyApplication($app, $plugin, $options);
 60: 
 61:             return true;
 62:         }
 63: 
 64:         return $this->_modifyBootstrap($plugin, $options);
 65:     }
 66: 
 67:     /**
 68:      * Create options string for the load call.
 69:      *
 70:      * @return string
 71:      */
 72:     protected function makeOptions()
 73:     {
 74:         $autoloadString = $this->param('autoload') ? "'autoload' => true" : '';
 75:         $bootstrapString = $this->param('bootstrap') ? "'bootstrap' => true" : '';
 76:         $routesString = $this->param('routes') ? "'routes' => true" : '';
 77: 
 78:         return implode(', ', array_filter([$autoloadString, $bootstrapString, $routesString]));
 79:     }
 80: 
 81:     /**
 82:      * Modify the application class
 83:      *
 84:      * @param string $app The Application file to modify.
 85:      * @param string $plugin The plugin name to add.
 86:      * @param string $options The plugin options to add
 87:      * @return void
 88:      */
 89:     protected function modifyApplication($app, $plugin, $options)
 90:     {
 91:         $file = new File($app, false);
 92:         $contents = $file->read();
 93: 
 94:         $append = "\n        \$this->addPlugin('%s', [%s]);\n";
 95:         $insert = str_replace(', []', '', sprintf($append, $plugin, $options));
 96: 
 97:         if (!preg_match('/function bootstrap\(\)/m', $contents)) {
 98:             $this->abort('Your Application class does not have a bootstrap() method. Please add one.');
 99:         } else {
100:             $contents = preg_replace('/(function bootstrap\(\)(?:\s+)\{)/m', '$1' . $insert, $contents);
101:         }
102:         $file->write($contents);
103: 
104:         $this->out('');
105:         $this->out(sprintf('%s modified', $app));
106:     }
107: 
108:     /**
109:      * Update the applications bootstrap.php file.
110:      *
111:      * @param string $plugin Name of plugin.
112:      * @param string $options The options string
113:      * @return bool If modify passed.
114:      */
115:     protected function _modifyBootstrap($plugin, $options)
116:     {
117:         $bootstrap = new File($this->bootstrap, false);
118:         $contents = $bootstrap->read();
119:         if (!preg_match("@\n\s*Plugin::loadAll@", $contents)) {
120:             $append = "\nPlugin::load('%s', [%s]);\n";
121: 
122:             $bootstrap->append(str_replace(', []', '', sprintf($append, $plugin, $options)));
123:             $this->out('');
124:             $this->out(sprintf('%s modified', $this->bootstrap));
125: 
126:             return true;
127:         }
128: 
129:         return false;
130:     }
131: 
132:     /**
133:      * GetOptionParser method.
134:      *
135:      * @return \Cake\Console\ConsoleOptionParser
136:      */
137:     public function getOptionParser()
138:     {
139:         $parser = parent::getOptionParser();
140: 
141:         $parser->addOption('bootstrap', [
142:                     'short' => 'b',
143:                     'help' => 'Will load bootstrap.php from plugin.',
144:                     'boolean' => true,
145:                     'default' => false,
146:                 ])
147:                 ->addOption('routes', [
148:                     'short' => 'r',
149:                     'help' => 'Will load routes.php from plugin.',
150:                     'boolean' => true,
151:                     'default' => false,
152:                 ])
153:                 ->addOption('autoload', [
154:                     'help' => 'Will autoload the plugin using CakePHP.' .
155:                         'Set to true if you are not using composer to autoload your plugin.',
156:                     'boolean' => true,
157:                     'default' => false,
158:                 ])
159:                 ->addOption('cli', [
160:                     'help' => 'Use the bootstrap_cli file.',
161:                     'boolean' => true,
162:                     'default' => false,
163:                 ])
164:                 ->addOption('no_app', [
165:                     'help' => 'Do not update the Application if it exist. Forces config/bootstrap.php to be updated.',
166:                     'boolean' => true,
167:                     'default' => false,
168:                 ])
169:                 ->addArgument('plugin', [
170:                     'help' => 'Name of the plugin to load.',
171:                 ]);
172: 
173:         return $parser;
174:     }
175: }
176: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs