1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: namespace Cake\Shell\Task;
16:
17: use Cake\Console\Shell;
18: use Cake\Filesystem\File;
19:
20: 21: 22:
23: class LoadTask extends Shell
24: {
25:
26: 27: 28: 29: 30:
31: public $bootstrap;
32:
33: 34: 35: 36: 37: 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: 69: 70: 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: 83: 84: 85: 86: 87: 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: 110: 111: 112: 113: 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: 134: 135: 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: