UnloadTask.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Shell\Task;
  16. use Cake\Console\Shell;
  17. use Cake\Filesystem\File;
  18. /**
  19. * Task for unloading plugins.
  20. *
  21. */
  22. class UnloadTask extends Shell
  23. {
  24. /**
  25. * Path to the bootstrap file.
  26. *
  27. * @var string
  28. */
  29. public $bootstrap = null;
  30. /**
  31. * Execution method always used for tasks.
  32. *
  33. * @param string $plugin The plugin name.
  34. * @return boolean if action passed.
  35. */
  36. public function main($plugin = null)
  37. {
  38. $this->bootstrap = ROOT . DS . 'config' . DS . 'bootstrap.php';
  39. if (empty($plugin)) {
  40. $this->err('<error>You must provide a plugin name in CamelCase format.</error>');
  41. $this->err('To unload an "Example" plugin, run <info>`cake plugin unload Example`</info>.');
  42. return false;
  43. }
  44. $write = $this->_modifyBootstrap($plugin);
  45. if ($write) {
  46. return true;
  47. }
  48. return false;
  49. }
  50. /**
  51. * Update the applications bootstrap.php file.
  52. *
  53. * @param string $plugin Name of plugin.
  54. * @return bool If modify passed.
  55. */
  56. protected function _modifyBootstrap($plugin)
  57. {
  58. $finder = "/\nPlugin::load\('$plugin'(.|.\n|)+\);\n/";
  59. $bootstrap = new File($this->bootstrap, false);
  60. $contents = $bootstrap->read();
  61. if (!preg_match("@\n\s*Plugin::loadAll@", $contents)) {
  62. $contents = preg_replace($finder, "", $contents);
  63. $bootstrap->write($contents);
  64. $this->out('');
  65. $this->out(sprintf('%s modified', $this->bootstrap));
  66. return true;
  67. }
  68. return false;
  69. }
  70. /**
  71. * GetOptionParser method.
  72. *
  73. * @return \Cake\Console\ConsoleOptionParser
  74. */
  75. public function getOptionParser()
  76. {
  77. $parser = parent::getOptionParser();
  78. $parser->addArgument('plugin', [
  79. 'help' => 'Name of the plugin to load.',
  80. ]);
  81. return $parser;
  82. }
  83. }