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