class ProcessOutputCallback

A process callback for capturing output.

@internal This is an internal part of Package Manager and may be changed or removed at any time without warning. External code should not interact with this class.

Hierarchy

  • class \Drupal\package_manager\ProcessOutputCallback implements \PhpTuf\ComposerStager\API\Process\Service\OutputCallbackInterface, \Psr\Log\LoggerAwareInterface uses \Psr\Log\LoggerAwareTrait

Expanded class hierarchy of ProcessOutputCallback

See also

\Symfony\Component\Process\Process

3 files declare their use of ProcessOutputCallback
LoggingBeginnerTest.php in core/modules/package_manager/tests/src/Unit/LoggingBeginnerTest.php
LoggingCommitterTest.php in core/modules/package_manager/tests/src/Unit/LoggingCommitterTest.php
ProcessOutputCallbackTest.php in core/modules/package_manager/tests/src/Unit/ProcessOutputCallbackTest.php

File

core/modules/package_manager/src/ProcessOutputCallback.php, line 23

Namespace

Drupal\package_manager
View source
final class ProcessOutputCallback implements OutputCallbackInterface, LoggerAwareInterface {
  use LoggerAwareTrait;
  
  /**
   * The output buffer.
   *
   * @var array
   */
  private array $outBuffer = [];
  
  /**
   * The error buffer.
   *
   * @var array
   */
  private array $errorBuffer = [];
  
  /**
   * Constructs a ProcessOutputCallback object.
   */
  public function __construct() {
    $this->setLogger(new NullLogger());
  }
  
  /**
   * {@inheritdoc}
   */
  public function __invoke(OutputTypeEnum $type, string $buffer) : void {
    if ($type === OutputTypeEnum::OUT) {
      $this->outBuffer[] = $buffer;
    }
    elseif ($type === OutputTypeEnum::ERR) {
      $this->errorBuffer[] = $buffer;
    }
  }
  
  /**
   * Gets the output.
   *
   * If there is anything in the error buffer, it will be logged as a warning.
   *
   * @return array
   *   The output buffer.
   */
  public function getOutput() : array {
    $error_output = $this->getErrorOutput();
    if ($error_output) {
      $this->logger
        ->warning(implode('', $error_output));
    }
    return $this->outBuffer;
  }
  
  /**
   * Gets the parsed JSON output.
   *
   * @return mixed
   *   The decoded JSON output or NULL if there isn't any.
   */
  public function parseJsonOutput() : mixed {
    $output = $this->getOutput();
    if ($output) {
      return json_decode(trim(implode('', $output)), TRUE, flags: JSON_THROW_ON_ERROR);
    }
    return NULL;
  }
  
  /**
   * Gets the error output.
   *
   * @return array
   *   The error output buffer.
   */
  public function getErrorOutput() : array {
    return $this->errorBuffer;
  }
  
  /**
   * {@inheritdoc}
   */
  public function clearErrorOutput() : void {
    $this->errorBuffer = [];
  }
  
  /**
   * {@inheritdoc}
   */
  public function clearOutput() : void {
    $this->outBuffer = [];
  }
  
  /**
   * Resets buffers.
   *
   * @return self
   *   The current instance for method chaining.
   */
  public function reset() : self {
    $this->clearErrorOutput();
    $this->clearOutput();
    return $this;
  }

}

Members

Title Sort descending Modifiers Object type Summary
ProcessOutputCallback::$errorBuffer private property The error buffer.
ProcessOutputCallback::$outBuffer private property The output buffer.
ProcessOutputCallback::clearErrorOutput public function
ProcessOutputCallback::clearOutput public function
ProcessOutputCallback::getErrorOutput public function Gets the error output.
ProcessOutputCallback::getOutput public function Gets the output.
ProcessOutputCallback::parseJsonOutput public function Gets the parsed JSON output.
ProcessOutputCallback::reset public function Resets buffers.
ProcessOutputCallback::__construct public function Constructs a ProcessOutputCallback object.
ProcessOutputCallback::__invoke public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.