You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

111 lines
2.8 KiB

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