plugin 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 (!$name) {
  64. $output->writeln('');
  65. $output->writeln('<red>Usage:</red>');
  66. $output->writeln(" {$bin} [slug] [command] [arguments]");
  67. $output->writeln('');
  68. $output->writeln('<red>Example:</red>');
  69. $output->writeln(" {$bin} error log -l 1 --trace");
  70. $list = Folder::all('plugins://', ['compare' => 'Pathname', 'pattern' => '/\/cli\/' . $pattern . '$/usm', 'levels' => 2]);
  71. $total = 0;
  72. if (count($list)) {
  73. $available = [];
  74. $output->writeln('');
  75. $output->writeln('<red>Plugins with CLI available:</red>');
  76. foreach ($list as $index => $entry) {
  77. $split = explode('/', $entry);
  78. $entry = array_shift($split);
  79. $index = str_pad($index++ + 1, 2, '0', STR_PAD_LEFT);
  80. $total = str_pad($total++ + 1, 2, '0', STR_PAD_LEFT);
  81. if (\in_array($entry, $available, true)) {
  82. $total--;
  83. continue;
  84. }
  85. $available[] = $entry;
  86. $commands_count = $index - $total + 1;
  87. $output->writeln(' ' . $total . '. <red>' . str_pad($entry, 15) . "</red> <white>{$bin} {$entry} list</white>");
  88. }
  89. }
  90. exit;
  91. } else {
  92. if (is_null($plugin)) {
  93. $output->writeln('');
  94. $output->writeln("<red>$name plugin not found</red>");
  95. die;
  96. }
  97. if (!$plugin->enabled) {
  98. $output->writeln('');
  99. $output->writeln("<red>$name not enabled</red>");
  100. die;
  101. }
  102. }
  103. if ($plugin === null) {
  104. $output->writeln("<red>Grav Plugin <white>'{$name}'</white> is not installed</red>");
  105. exit;
  106. }
  107. $path = 'plugins://' . $name . '/cli';
  108. try {
  109. $commands = Folder::all($path, ['compare' => 'Filename', 'pattern' => '/' . $pattern . '$/usm', 'levels' => 1]);
  110. } catch (\RuntimeException $e) {
  111. $output->writeln("<red>No Console Commands for <white>'{$name}'</white> where found in <white>'{$path}'</white></red>");
  112. exit;
  113. }
  114. foreach ($commands as $command_path) {
  115. $full_path = $grav['locator']->findResource("plugins://{$name}/cli/{$command_path}");
  116. require_once $full_path;
  117. $command_class = 'Grav\Plugin\Console\\' . preg_replace('/.php$/', '', $command_path);
  118. $command = new $command_class();
  119. $app->add($command);
  120. }
  121. $app->run($input);