[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/console/Tester/ -> CommandTester.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Console\Tester;
  13  
  14  use Symfony\Component\Console\Command\Command;
  15  use Symfony\Component\Console\Input\ArrayInput;
  16  use Symfony\Component\Console\Input\InputInterface;
  17  use Symfony\Component\Console\Output\OutputInterface;
  18  use Symfony\Component\Console\Output\StreamOutput;
  19  
  20  /**
  21   * Eases the testing of console commands.
  22   *
  23   * @author Fabien Potencier <fabien@symfony.com>
  24   * @author Robin Chalas <robin.chalas@gmail.com>
  25   */
  26  class CommandTester
  27  {
  28      private $command;
  29      private $input;
  30      private $output;
  31      private $inputs = [];
  32      private $statusCode;
  33  
  34      public function __construct(Command $command)
  35      {
  36          $this->command = $command;
  37      }
  38  
  39      /**
  40       * Executes the command.
  41       *
  42       * Available execution options:
  43       *
  44       *  * interactive: Sets the input interactive flag
  45       *  * decorated:   Sets the output decorated flag
  46       *  * verbosity:   Sets the output verbosity flag
  47       *
  48       * @param array $input   An array of command arguments and options
  49       * @param array $options An array of execution options
  50       *
  51       * @return int The command exit code
  52       */
  53      public function execute(array $input, array $options = [])
  54      {
  55          // set the command name automatically if the application requires
  56          // this argument and no command name was passed
  57          if (!isset($input['command'])
  58              && (null !== $application = $this->command->getApplication())
  59              && $application->getDefinition()->hasArgument('command')
  60          ) {
  61              $input = array_merge(['command' => $this->command->getName()], $input);
  62          }
  63  
  64          $this->input = new ArrayInput($input);
  65          // Use an in-memory input stream even if no inputs are set so that QuestionHelper::ask() does not rely on the blocking STDIN.
  66          $this->input->setStream(self::createStream($this->inputs));
  67  
  68          if (isset($options['interactive'])) {
  69              $this->input->setInteractive($options['interactive']);
  70          }
  71  
  72          $this->output = new StreamOutput(fopen('php://memory', 'w', false));
  73          $this->output->setDecorated(isset($options['decorated']) ? $options['decorated'] : false);
  74          if (isset($options['verbosity'])) {
  75              $this->output->setVerbosity($options['verbosity']);
  76          }
  77  
  78          return $this->statusCode = $this->command->run($this->input, $this->output);
  79      }
  80  
  81      /**
  82       * Gets the display returned by the last execution of the command.
  83       *
  84       * @param bool $normalize Whether to normalize end of lines to \n or not
  85       *
  86       * @return string The display
  87       */
  88      public function getDisplay($normalize = false)
  89      {
  90          if (null === $this->output) {
  91              throw new \RuntimeException('Output not initialized, did you execute the command before requesting the display?');
  92          }
  93  
  94          rewind($this->output->getStream());
  95  
  96          $display = stream_get_contents($this->output->getStream());
  97  
  98          if ($normalize) {
  99              $display = str_replace(\PHP_EOL, "\n", $display);
 100          }
 101  
 102          return $display;
 103      }
 104  
 105      /**
 106       * Gets the input instance used by the last execution of the command.
 107       *
 108       * @return InputInterface The current input instance
 109       */
 110      public function getInput()
 111      {
 112          return $this->input;
 113      }
 114  
 115      /**
 116       * Gets the output instance used by the last execution of the command.
 117       *
 118       * @return OutputInterface The current output instance
 119       */
 120      public function getOutput()
 121      {
 122          return $this->output;
 123      }
 124  
 125      /**
 126       * Gets the status code returned by the last execution of the application.
 127       *
 128       * @return int The status code
 129       */
 130      public function getStatusCode()
 131      {
 132          return $this->statusCode;
 133      }
 134  
 135      /**
 136       * Sets the user inputs.
 137       *
 138       * @param array $inputs An array of strings representing each input
 139       *                      passed to the command input stream
 140       *
 141       * @return CommandTester
 142       */
 143      public function setInputs(array $inputs)
 144      {
 145          $this->inputs = $inputs;
 146  
 147          return $this;
 148      }
 149  
 150      private static function createStream(array $inputs)
 151      {
 152          $stream = fopen('php://memory', 'r+', false);
 153  
 154          foreach ($inputs as $input) {
 155              fwrite($stream, $input.\PHP_EOL);
 156          }
 157  
 158          rewind($stream);
 159  
 160          return $stream;
 161      }
 162  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1