UnloadTask.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. class UnloadTask extends Shell
  22. {
  23. /**
  24. * Path to the bootstrap file.
  25. *
  26. * @var string
  27. */
  28. public $bootstrap = null;
  29. /**
  30. * Execution method always used for tasks.
  31. *
  32. * @param string|null $plugin The plugin name.
  33. * @return bool if action passed.
  34. */
  35. public function main($plugin = null)
  36. {
  37. $this->bootstrap = ROOT . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'bootstrap.php';
  38. if (empty($plugin)) {
  39. $this->err('You must provide a plugin name in CamelCase format.');
  40. $this->err('To unload an "Example" plugin, run `cake plugin unload Example`.');
  41. return false;
  42. }
  43. return (bool)$this->_modifyBootstrap($plugin);
  44. }
  45. /**
  46. * Update the applications bootstrap.php file.
  47. *
  48. * @param string $plugin Name of plugin.
  49. * @return bool If modify passed.
  50. */
  51. protected function _modifyBootstrap($plugin)
  52. {
  53. $finder = "/\nPlugin::load\((.|.\n|\n\s\s|\n\t|)+'$plugin'(.|.\n|)+\);\n/";
  54. $bootstrap = new File($this->bootstrap, false);
  55. $contents = $bootstrap->read();
  56. if (!preg_match("@\n\s*Plugin::loadAll@", $contents)) {
  57. $contents = preg_replace($finder, "", $contents);
  58. $bootstrap->write($contents);
  59. $this->out('');
  60. $this->out(sprintf('%s modified', $this->bootstrap));
  61. return true;
  62. }
  63. return false;
  64. }
  65. /**
  66. * GetOptionParser method.
  67. *
  68. * @return \Cake\Console\ConsoleOptionParser
  69. */
  70. public function getOptionParser()
  71. {
  72. $parser = parent::getOptionParser();
  73. $parser->addArgument('plugin', [
  74. 'help' => 'Name of the plugin to load.',
  75. ]);
  76. return $parser;
  77. }
  78. }