plugin 4.2 KB

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