plugin.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env php
  2. <?php
  3. use dokuwiki\Extension\PluginController;
  4. use splitbrain\phpcli\CLI;
  5. use splitbrain\phpcli\Colors;
  6. use splitbrain\phpcli\Options;
  7. if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
  8. define('NOSESSION', 1);
  9. require_once(DOKU_INC . 'inc/init.php');
  10. class PluginCLI extends CLI {
  11. /**
  12. * Register options and arguments on the given $options object
  13. *
  14. * @param Options $options
  15. * @return void
  16. */
  17. protected function setup(Options $options) {
  18. $options->setHelp('Excecutes Plugin command line tools');
  19. $options->registerArgument('plugin', 'The plugin CLI you want to run. Leave off to see list', false);
  20. }
  21. /**
  22. * Your main program
  23. *
  24. * Arguments and options have been parsed when this is run
  25. *
  26. * @param Options $options
  27. * @return void
  28. */
  29. protected function main(Options $options) {
  30. global $argv;
  31. $argv = $options->getArgs();
  32. if($argv) {
  33. $plugin = $this->loadPlugin($argv[0]);
  34. if($plugin !== null) {
  35. $plugin->run();
  36. } else {
  37. $this->fatal('Command {cmd} not found.', ['cmd' => $argv[0]]);
  38. }
  39. } else {
  40. echo $options->help();
  41. $this->listPlugins();
  42. }
  43. }
  44. /**
  45. * List available plugins
  46. */
  47. protected function listPlugins() {
  48. /** @var PluginController $plugin_controller */
  49. global $plugin_controller;
  50. echo "\n";
  51. echo "\n";
  52. echo $this->colors->wrap('AVAILABLE PLUGINS:', Colors::C_BROWN);
  53. echo "\n";
  54. $list = $plugin_controller->getList('cli');
  55. sort($list);
  56. if(!count($list)) {
  57. echo $this->colors->wrap(" No plugins providing CLI components available\n", Colors::C_RED);
  58. } else {
  59. $tf = new \splitbrain\phpcli\TableFormatter($this->colors);
  60. foreach($list as $name) {
  61. $plugin = $this->loadPlugin($name);
  62. if($plugin === null) continue;
  63. $info = $plugin->getInfo();
  64. echo $tf->format(
  65. [2, '30%', '*'],
  66. ['', $name, $info['desc']],
  67. ['', Colors::C_CYAN, '']
  68. );
  69. }
  70. }
  71. }
  72. /**
  73. * Instantiate a CLI plugin
  74. *
  75. * @param string $name
  76. * @return \dokuwiki\Extension\CLIPlugin|null
  77. */
  78. protected function loadPlugin($name) {
  79. // execute the plugin CLI
  80. $class = "cli_plugin_$name";
  81. if(class_exists($class)) {
  82. return new $class();
  83. }
  84. return null;
  85. }
  86. }
  87. // Main
  88. $cli = new PluginCLI();
  89. $cli->run();