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