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: public $bootstrap;
31:
32: 33: 34: 35: 36: 37:
38: public function main($plugin = null)
39: {
40: $filename = 'bootstrap';
41: if ($this->params['cli']) {
42: $filename .= '_cli';
43: }
44:
45: $this->bootstrap = ROOT . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . $filename . '.php';
46:
47: if (!$plugin) {
48: $this->err('You must provide a plugin name in CamelCase format.');
49: $this->err('To load an "Example" plugin, run `cake plugin load Example`.');
50:
51: return false;
52: }
53:
54: $options = $this->makeOptions();
55:
56: $app = APP . 'Application.php';
57: if (file_exists($app) && !$this->param('no_app')) {
58: $this->modifyApplication($app, $plugin, $options);
59:
60: return true;
61: }
62:
63: return $this->_modifyBootstrap($plugin, $options);
64: }
65:
66: 67: 68: 69: 70:
71: protected function makeOptions()
72: {
73: $autoloadString = $this->param('autoload') ? "'autoload' => true" : '';
74: $bootstrapString = $this->param('bootstrap') ? "'bootstrap' => true" : '';
75: $routesString = $this->param('routes') ? "'routes' => true" : '';
76:
77: return implode(', ', array_filter([$autoloadString, $bootstrapString, $routesString]));
78: }
79:
80: 81: 82: 83: 84: 85: 86: 87:
88: protected function modifyApplication($app, $plugin, $options)
89: {
90: $file = new File($app, false);
91: $contents = $file->read();
92:
93: $append = "\n \$this->addPlugin('%s', [%s]);\n";
94: $insert = str_replace(', []', '', sprintf($append, $plugin, $options));
95:
96: if (!preg_match('/function bootstrap\(\)/m', $contents)) {
97: $this->abort('Your Application class does not have a bootstrap() method. Please add one.');
98: } else {
99: $contents = preg_replace('/(function bootstrap\(\)(?:\s+)\{)/m', '$1' . $insert, $contents);
100: }
101: $file->write($contents);
102:
103: $this->out('');
104: $this->out(sprintf('%s modified', $app));
105: }
106:
107: 108: 109: 110: 111: 112: 113:
114: protected function _modifyBootstrap($plugin, $options)
115: {
116: $bootstrap = new File($this->bootstrap, false);
117: $contents = $bootstrap->read();
118: if (!preg_match("@\n\s*Plugin::loadAll@", $contents)) {
119: $append = "\nPlugin::load('%s', [%s]);\n";
120:
121: $bootstrap->append(str_replace(', []', '', sprintf($append, $plugin, $options)));
122: $this->out('');
123: $this->out(sprintf('%s modified', $this->bootstrap));
124:
125: return true;
126: }
127:
128: return false;
129: }
130:
131: 132: 133: 134: 135:
136: public function getOptionParser()
137: {
138: $parser = parent::getOptionParser();
139:
140: $parser->addOption('bootstrap', [
141: 'short' => 'b',
142: 'help' => 'Will load bootstrap.php from plugin.',
143: 'boolean' => true,
144: 'default' => false,
145: ])
146: ->addOption('routes', [
147: 'short' => 'r',
148: 'help' => 'Will load routes.php from plugin.',
149: 'boolean' => true,
150: 'default' => false,
151: ])
152: ->addOption('autoload', [
153: 'help' => 'Will autoload the plugin using CakePHP.' .
154: 'Set to true if you are not using composer to autoload your plugin.',
155: 'boolean' => true,
156: 'default' => false,
157: ])
158: ->addOption('cli', [
159: 'help' => 'Use the bootstrap_cli file.',
160: 'boolean' => true,
161: 'default' => false,
162: ])
163: ->addOption('no_app', [
164: 'help' => 'Do not update the Application if it exist. Forces config/bootstrap.php to be updated.',
165: 'boolean' => true,
166: 'default' => false,
167: ])
168: ->addArgument('plugin', [
169: 'help' => 'Name of the plugin to load.',
170: ]);
171:
172: return $parser;
173: }
174: }
175: