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 UnloadTask 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 unload an "Example" plugin, run `cake plugin unload Example`.');
51:
52: return false;
53: }
54:
55: $app = APP . 'Application.php';
56: if (file_exists($app) && !$this->param('no_app')) {
57: $this->modifyApplication($app, $plugin);
58:
59: return true;
60: }
61:
62: return (bool)$this->_modifyBootstrap($plugin);
63: }
64:
65: 66: 67: 68: 69: 70: 71:
72: protected function modifyApplication($app, $plugin)
73: {
74: $finder = "@\\\$this\-\>addPlugin\(\s*'$plugin'(.|.\n|)+\);+@";
75:
76: $content = file_get_contents($app);
77: $newContent = preg_replace($finder, '', $content);
78:
79: if ($newContent === $content) {
80: return false;
81: }
82:
83: file_put_contents($app, $newContent);
84:
85: $this->out('');
86: $this->out(sprintf('%s modified', $app));
87:
88: return true;
89: }
90:
91: 92: 93: 94: 95: 96:
97: protected function _modifyBootstrap($plugin)
98: {
99: $finder = "@\nPlugin::load\((.|.\n|\n\s\s|\n\t|)+'$plugin'(.|.\n|)+\);\n@";
100:
101: $bootstrap = new File($this->bootstrap, false);
102: $content = $bootstrap->read();
103:
104: if (!preg_match("@\n\s*Plugin::loadAll@", $content)) {
105: $newContent = preg_replace($finder, '', $content);
106:
107: if ($newContent === $content) {
108: return false;
109: }
110:
111: $bootstrap->write($newContent);
112:
113: $this->out('');
114: $this->out(sprintf('%s modified', $this->bootstrap));
115:
116: return true;
117: }
118:
119: return false;
120: }
121:
122: 123: 124: 125: 126:
127: public function getOptionParser()
128: {
129: $parser = parent::getOptionParser();
130:
131: $parser->addOption('cli', [
132: 'help' => 'Use the bootstrap_cli file.',
133: 'boolean' => true,
134: 'default' => false,
135: ])
136: ->addOption('no_app', [
137: 'help' => 'Do not update the Application if it exist. Forces config/bootstrap.php to be updated.',
138: 'boolean' => true,
139: 'default' => false,
140: ])
141: ->addArgument('plugin', [
142: 'help' => 'Name of the plugin to load.',
143: ]);
144:
145: return $parser;
146: }
147: }
148: