Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ProcessOutputCallback.php

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

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
     */
    public function reset() : self {
        $this->clearErrorOutput();
        $this->clearOutput();
        return $this;
    }

}

Members

Title Sort descending Modifiers Object type Summary
LoggerAwareTrait::$logger protected property The logger instance.
LoggerAwareTrait::setLogger public function Sets a logger.
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
RSS feed
Powered by Drupal