LoadTask.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 loading plugins.
  20. *
  21. */
  22. class LoadTask 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 bool if action passed.
  35. *
  36. */
  37. public function main($plugin = null)
  38. {
  39. $this->bootstrap = ROOT . DS . 'config' . DS . 'bootstrap.php';
  40. if (empty($plugin)) {
  41. $this->err('<error>You must provide a plugin name in CamelCase format.</error>');
  42. $this->err('To load an "Example" plugin, run <info>`cake plugin load Example`</info>.');
  43. return false;
  44. }
  45. $write = $this->_modifyBootstrap($plugin, $this->params['bootstrap'], $this->params['routes'], false);
  46. if ($write) {
  47. return true;
  48. }
  49. return false;
  50. }
  51. /**
  52. * Update the applications bootstrap.php file.
  53. *
  54. * @param string $plugin Name of plugin.
  55. * @param bool $hasBootstrap Whether or not bootstrap should be loaded.
  56. * @param bool $hasRoutes Whether or not routes should be loaded.
  57. * @param bool $hasAutoloader Whether or not there is an autoloader configured for
  58. * the plugin.
  59. * @return bool If modify passed.
  60. */
  61. protected function _modifyBootstrap($plugin, $hasBootstrap, $hasRoutes, $hasAutoloader)
  62. {
  63. $bootstrap = new File($this->bootstrap, false);
  64. $contents = $bootstrap->read();
  65. if (!preg_match("@\n\s*Plugin::loadAll@", $contents)) {
  66. $_autoload = $hasAutoloader ? null : "'autoload' => true, ";
  67. $_bootstrap = $hasBootstrap ? "'bootstrap' => true, " : "'bootstrap' => false, ";
  68. $_routes = $hasRoutes ? "'routes' => true" : "'routes' => false";
  69. $append = "\nPlugin::load('%s', [%s%s%s]);\n";
  70. $bootstrap->append(sprintf($append, $plugin, $_autoload, $_bootstrap, $_routes));
  71. $this->out('');
  72. $this->out(sprintf('%s modified', $this->bootstrap));
  73. return true;
  74. }
  75. return false;
  76. }
  77. /**
  78. * GetOptionParser method.
  79. *
  80. * @return \Cake\Console\ConsoleOptionParser
  81. */
  82. public function getOptionParser()
  83. {
  84. $parser = parent::getOptionParser();
  85. $parser->addOption('bootstrap', [
  86. 'short' => 'b',
  87. 'help' => 'Will load bootstrap.php from plugin.',
  88. 'boolean' => true,
  89. 'default' => false,
  90. ]);
  91. $parser->addOption('routes', [
  92. 'short' => 'r',
  93. 'help' => 'Will load routes.php from plugin.',
  94. 'boolean' => true,
  95. 'default' => false,
  96. ]);
  97. $parser->addArgument('plugin', [
  98. 'help' => 'Name of the plugin to load.',
  99. ]);
  100. return $parser;
  101. }
  102. }