EnvironmentCompletionContext.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace Stecman\Component\Symfony\Console\BashCompletion;
  3. class EnvironmentCompletionContext extends CompletionContext
  4. {
  5. /**
  6. * Set up completion context from the environment variables set by the parent shell
  7. */
  8. public function __construct()
  9. {
  10. $this->commandLine = getenv('CMDLINE_CONTENTS');
  11. $this->charIndex = intval(getenv('CMDLINE_CURSOR_INDEX'));
  12. if ($this->commandLine === false) {
  13. $message = 'Failed to configure from environment; Environment var CMDLINE_CONTENTS not set.';
  14. if (getenv('COMP_LINE')) {
  15. $message .= "\n\nYou appear to be attempting completion using an out-dated hook. If you've just updated,"
  16. . " you probably need to reinitialise the completion shell hook by reloading your shell"
  17. . " profile or starting a new shell session. If you are using a hard-coded (rather than generated)"
  18. . " hook, you will need to update that function with the new environment variable names."
  19. . "\n\nSee here for details: https://github.com/stecman/symfony-console-completion/issues/31";
  20. }
  21. throw new \RuntimeException($message);
  22. }
  23. }
  24. /**
  25. * Use the word break characters set by the parent shell.
  26. *
  27. * @throws \RuntimeException
  28. */
  29. public function useWordBreaksFromEnvironment()
  30. {
  31. $breaks = getenv('CMDLINE_WORDBREAKS');
  32. if (!$breaks) {
  33. throw new \RuntimeException('Failed to read word breaks from environment; Environment var CMDLINE_WORDBREAKS not set');
  34. }
  35. $this->wordBreaks = $breaks;
  36. }
  37. }