plugin 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // Set internal encoding.
  28. if (!\extension_loaded('mbstring')) {
  29. die("'mbstring' extension is not loaded. This is required for Grav to run correctly");
  30. }
  31. @ini_set('default_charset', 'UTF-8');
  32. mb_internal_encoding('UTF-8');
  33. if (!file_exists(GRAV_ROOT . '/index.php')) {
  34. exit('FATAL: Must be run from ROOT directory of Grav!');
  35. }
  36. $climate = new League\CLImate\CLImate;
  37. $climate->arguments->add([
  38. 'environment' => [
  39. 'prefix' => 'e',
  40. 'longPrefix' => 'env',
  41. 'description' => 'Configuration Environment',
  42. 'defaultValue' => 'localhost'
  43. ]
  44. ]);
  45. $climate->arguments->parse();
  46. $environment = $climate->arguments->get('environment');
  47. $grav = Grav::instance(array('loader' => $autoload));
  48. $grav->setup($environment);
  49. $grav->initializeCli();
  50. $app = new Application('Grav Plugins Commands', GRAV_VERSION);
  51. $pattern = '([A-Z]\w+Command\.php)';
  52. // get arguments and strip the application name
  53. if (null === $argv) {
  54. $argv = $_SERVER['argv'];
  55. }
  56. $bin = array_shift($argv);
  57. $name = array_shift($argv);
  58. $argv = array_merge([$bin], $argv);
  59. $input = new ArgvInput($argv);
  60. /** @var \Grav\Common\Data\Data $plugin */
  61. $plugin = $grav['plugins']->get($name);
  62. $output = new ConsoleOutput();
  63. $output->getFormatter()->setStyle('red', new OutputFormatterStyle('red', null, array('bold')));
  64. $output->getFormatter()->setStyle('white', new OutputFormatterStyle('white', null, array('bold')));
  65. if (!$name) {
  66. $output->writeln('');
  67. $output->writeln('<red>Usage:</red>');
  68. $output->writeln(" {$bin} [slug] [command] [arguments]");
  69. $output->writeln('');
  70. $output->writeln('<red>Example:</red>');
  71. $output->writeln(" {$bin} error log -l 1 --trace");
  72. $list = Folder::all('plugins://', ['compare' => 'Pathname', 'pattern' => '/\/cli\/' . $pattern . '$/usm', 'levels' => 2]);
  73. $total = 0;
  74. if (count($list)) {
  75. $available = [];
  76. $output->writeln('');
  77. $output->writeln('<red>Plugins with CLI available:</red>');
  78. foreach ($list as $index => $entry) {
  79. $split = explode('/', $entry);
  80. $entry = array_shift($split);
  81. $index = str_pad($index++ + 1, 2, '0', STR_PAD_LEFT);
  82. $total = str_pad($total++ + 1, 2, '0', STR_PAD_LEFT);
  83. if (\in_array($entry, $available, true)) {
  84. $total--;
  85. continue;
  86. }
  87. $available[] = $entry;
  88. $commands_count = $index - $total + 1;
  89. $output->writeln(' ' . $total . '. <red>' . str_pad($entry, 15) . "</red> <white>{$bin} {$entry} list</white>");
  90. }
  91. }
  92. exit;
  93. } else {
  94. if (is_null($plugin)) {
  95. $output->writeln('');
  96. $output->writeln("<red>$name plugin not found</red>");
  97. die;
  98. }
  99. if (!$plugin->enabled) {
  100. $output->writeln('');
  101. $output->writeln("<red>$name not enabled</red>");
  102. die;
  103. }
  104. }
  105. if ($plugin === null) {
  106. $output->writeln("<red>Grav Plugin <white>'{$name}'</white> is not installed</red>");
  107. exit;
  108. }
  109. $path = 'plugins://' . $name . '/cli';
  110. try {
  111. $commands = Folder::all($path, ['compare' => 'Filename', 'pattern' => '/' . $pattern . '$/usm', 'levels' => 1]);
  112. } catch (\RuntimeException $e) {
  113. $output->writeln("<red>No Console Commands for <white>'{$name}'</white> where found in <white>'{$path}'</white></red>");
  114. exit;
  115. }
  116. foreach ($commands as $command_path) {
  117. $full_path = $grav['locator']->findResource("plugins://{$name}/cli/{$command_path}");
  118. require_once $full_path;
  119. $command_class = 'Grav\Plugin\Console\\' . preg_replace('/.php$/', '', $command_path);
  120. $command = new $command_class();
  121. $app->add($command);
  122. }
  123. $app->run($input);