LoadTask.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. class LoadTask 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
  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 load an "Example" plugin, run `cake plugin load Example`.');
  41. return false;
  42. }
  43. return $this->_modifyBootstrap(
  44. $plugin,
  45. $this->params['bootstrap'],
  46. $this->params['routes'],
  47. $this->params['autoload']
  48. );
  49. }
  50. /**
  51. * Update the applications bootstrap.php file.
  52. *
  53. * @param string $plugin Name of plugin.
  54. * @param bool $hasBootstrap Whether or not bootstrap should be loaded.
  55. * @param bool $hasRoutes Whether or not routes should be loaded.
  56. * @param bool $hasAutoloader Whether or not there is an autoloader configured for
  57. * the plugin.
  58. * @return bool If modify passed.
  59. */
  60. protected function _modifyBootstrap($plugin, $hasBootstrap, $hasRoutes, $hasAutoloader)
  61. {
  62. $bootstrap = new File($this->bootstrap, false);
  63. $contents = $bootstrap->read();
  64. if (!preg_match("@\n\s*Plugin::loadAll@", $contents)) {
  65. $autoloadString = $hasAutoloader ? "'autoload' => true" : '';
  66. $bootstrapString = $hasBootstrap ? "'bootstrap' => true" : '';
  67. $routesString = $hasRoutes ? "'routes' => true" : '';
  68. $append = "\nPlugin::load('%s', [%s]);\n";
  69. $options = implode(', ', array_filter([$autoloadString, $bootstrapString, $routesString]));
  70. $bootstrap->append(str_replace(', []', '', sprintf($append, $plugin, $options)));
  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. ->addOption('routes', [
  92. 'short' => 'r',
  93. 'help' => 'Will load routes.php from plugin.',
  94. 'boolean' => true,
  95. 'default' => false,
  96. ])
  97. ->addOption('autoload', [
  98. 'help' => 'Will autoload the plugin using CakePHP. ' .
  99. 'Set to true if you are not using composer to autoload your plugin.',
  100. 'boolean' => true,
  101. 'default' => false,
  102. ])
  103. ->addArgument('plugin', [
  104. 'help' => 'Name of the plugin to load.',
  105. ]);
  106. return $parser;
  107. }
  108. }