plugin 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env php
  2. <?php
  3. use Grav\Common\Composer;
  4. use Symfony\Component\Console\Application;
  5. use Symfony\Component\Console\Input\ArgvInput;
  6. use Symfony\Component\Console\Output\ConsoleOutput;
  7. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  8. use Grav\Common\Grav;
  9. use Grav\Common\Filesystem\Folder;
  10. \define('GRAV_CLI', true);
  11. \define('GRAV_REQUEST_TIME', microtime(true));
  12. if (!file_exists(__DIR__ . '/../vendor/autoload.php')){
  13. // Before we can even start, we need to run composer first
  14. require_once __DIR__ . '/../system/src/Grav/Common/Composer.php';
  15. $composer = Composer::getComposerExecutor();
  16. echo "Preparing to install vendor dependencies...\n\n";
  17. echo system($composer.' --working-dir="'.__DIR__.'/../" --no-interaction --no-dev --prefer-dist -o install');
  18. echo "\n\n";
  19. }
  20. $autoload = require __DIR__ . '/../vendor/autoload.php';
  21. if (version_compare($ver = PHP_VERSION, $req = GRAV_PHP_MIN, '<')) {
  22. exit(sprintf("You are running PHP %s, but Grav needs at least PHP %s to run.\n", $ver, $req));
  23. }
  24. if (!ini_get('date.timezone')) {
  25. date_default_timezone_set('UTC');
  26. }
  27. if (!file_exists(GRAV_ROOT . '/index.php')) {
  28. exit('FATAL: Must be run from ROOT directory of Grav!');
  29. }
  30. $climate = new League\CLImate\CLImate;
  31. $climate->arguments->add([
  32. 'environment' => [
  33. 'prefix' => 'e',
  34. 'longPrefix' => 'env',
  35. 'description' => 'Configuration Environment',
  36. 'defaultValue' => 'localhost'
  37. ]
  38. ]);
  39. $climate->arguments->parse();
  40. $environment = $climate->arguments->get('environment');
  41. $grav = Grav::instance(array('loader' => $autoload));
  42. $grav->setup($environment);
  43. $grav['config']->init();
  44. $grav['uri']->init();
  45. $grav['users'];
  46. $grav['plugins']->init();
  47. $grav['themes']->init();
  48. $app = new Application('Grav Plugins Commands', GRAV_VERSION);
  49. $pattern = '([A-Z]\w+Command\.php)';
  50. // get arguments and strip the application name
  51. if (null === $argv) {
  52. $argv = $_SERVER['argv'];
  53. }
  54. $bin = array_shift($argv);
  55. $name = array_shift($argv);
  56. $argv = array_merge([$bin], $argv);
  57. $input = new ArgvInput($argv);
  58. /** @var \Grav\Common\Data\Data $plugin */
  59. $plugin = $grav['plugins']->get($name);
  60. $output = new ConsoleOutput();
  61. $output->getFormatter()->setStyle('red', new OutputFormatterStyle('red', null, array('bold')));
  62. $output->getFormatter()->setStyle('white', new OutputFormatterStyle('white', null, array('bold')));
  63. if (is_null($plugin)) {
  64. $output->writeln('');
  65. $output->writeln("<red>$name plugin not found</red>");
  66. die;
  67. }
  68. if (!$plugin->enabled) {
  69. $output->writeln('');
  70. $output->writeln("<red>$name not enabled</red>");
  71. die;
  72. }
  73. if (!$name) {
  74. $output->writeln('');
  75. $output->writeln('<red>Usage:</red>');
  76. $output->writeln(" {$bin} [slug] [command] [arguments]");
  77. $output->writeln('');
  78. $output->writeln('<red>Example:</red>');
  79. $output->writeln(" {$bin} error log -l 1 --trace");
  80. $list = Folder::all('plugins://', ['compare' => 'Pathname', 'pattern' => '/\/cli\/' . $pattern . '$/usm', 'levels' => 2]);
  81. $total = 0;
  82. if (count($list)) {
  83. $available = [];
  84. $output->writeln('');
  85. $output->writeln('<red>Plugins with CLI available:</red>');
  86. foreach ($list as $index => $entry) {
  87. $split = explode('/', $entry);
  88. $entry = array_shift($split);
  89. $index = str_pad($index++ + 1, 2, '0', STR_PAD_LEFT);
  90. $total = str_pad($total++ + 1, 2, '0', STR_PAD_LEFT);
  91. if (\in_array($entry, $available, true)) {
  92. $total--;
  93. continue;
  94. }
  95. $available[] = $entry;
  96. $commands_count = $index - $total + 1;
  97. $output->writeln(' ' . $total . '. <red>' . str_pad($entry, 15) . "</red> <white>{$bin} {$entry} list</white>");
  98. }
  99. }
  100. exit;
  101. }
  102. if ($plugin === null) {
  103. $output->writeln("<red>Grav Plugin <white>'{$name}'</white> is not installed</red>");
  104. exit;
  105. }
  106. $path = 'plugins://' . $name . '/cli';
  107. try {
  108. $commands = Folder::all($path, ['compare' => 'Filename', 'pattern' => '/' . $pattern . '$/usm', 'levels' => 1]);
  109. } catch (\RuntimeException $e) {
  110. $output->writeln("<red>No Console Commands for <white>'{$name}'</white> where found in <white>'{$path}'</white></red>");
  111. exit;
  112. }
  113. foreach ($commands as $command_path) {
  114. $full_path = $grav['locator']->findResource("plugins://{$name}/cli/{$command_path}");
  115. require_once $full_path;
  116. $command_class = 'Grav\Plugin\Console\\' . preg_replace('/.php$/', '', $command_path);
  117. $command = new $command_class();
  118. $app->add($command);
  119. }
  120. $app->run($input);