Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / consolidation / robo / src / Log / ResultPrinter.php
diff --git a/vendor/consolidation/robo/src/Log/ResultPrinter.php b/vendor/consolidation/robo/src/Log/ResultPrinter.php
new file mode 100644 (file)
index 0000000..a6f166a
--- /dev/null
@@ -0,0 +1,112 @@
+<?php
+namespace Robo\Log;
+
+use Robo\Result;
+use Robo\Contract\PrintedInterface;
+use Robo\Contract\ProgressIndicatorAwareInterface;
+use Robo\Contract\VerbosityThresholdInterface;
+use Robo\Common\ProgressIndicatorAwareTrait;
+
+use Psr\Log\LogLevel;
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerAwareTrait;
+use Consolidation\Log\ConsoleLogLevel;
+
+/**
+ * Log the creation of Result objects.
+ */
+class ResultPrinter implements LoggerAwareInterface, ProgressIndicatorAwareInterface
+{
+    use LoggerAwareTrait;
+    use ProgressIndicatorAwareTrait;
+
+    /**
+     * Log the result of a Robo task.
+     *
+     * Returns 'true' if the message is printed, or false if it isn't.
+     *
+     * @param \Robo\Result $result
+     *
+     * @return bool
+     */
+    public function printResult(Result $result)
+    {
+        $task = $result->getTask();
+        if ($task instanceof VerbosityThresholdInterface && !$task->verbosityMeetsThreshold()) {
+            return;
+        }
+        if (!$result->wasSuccessful()) {
+            return $this->printError($result);
+        } else {
+            return $this->printSuccess($result);
+        }
+    }
+
+    /**
+     * Log that we are about to abort due to an error being encountered
+     * in 'stop on fail' mode.
+     *
+     * @param \Robo\Result $result
+     */
+    public function printStopOnFail($result)
+    {
+        $this->printMessage(LogLevel::NOTICE, 'Stopping on fail. Exiting....');
+        $this->printMessage(LogLevel::ERROR, 'Exit Code: {code}', ['code' => $result->getExitCode()]);
+    }
+
+    /**
+     * Log the result of a Robo task that returned an error.
+     *
+     * @param \Robo\Result $result
+     *
+     * @return bool
+     */
+    protected function printError(Result $result)
+    {
+        $task = $result->getTask();
+        $context = $result->getContext() + ['timer-label' => 'Time', '_style' => []];
+        $context['_style']['message'] = '';
+
+        $printOutput = true;
+        if ($task instanceof PrintedInterface) {
+            $printOutput = !$task->getPrinted();
+        }
+        if ($printOutput) {
+            $this->printMessage(LogLevel::ERROR, "{message}", $context);
+        }
+        $this->printMessage(LogLevel::ERROR, 'Exit code {code}', $context);
+        return true;
+    }
+
+    /**
+     * Log the result of a Robo task that was successful.
+     *
+     * @param \Robo\Result $result
+     *
+     * @return bool
+     */
+    protected function printSuccess(Result $result)
+    {
+        $task = $result->getTask();
+        $context = $result->getContext() + ['timer-label' => 'in'];
+        $time = $result->getExecutionTime();
+        if ($time) {
+            $this->printMessage(ConsoleLogLevel::SUCCESS, 'Done', $context);
+        }
+        return false;
+    }
+
+    /**
+     * @param string $level
+     * @param string $message
+     * @param array $context
+     */
+    protected function printMessage($level, $message, $context = [])
+    {
+        $inProgress = $this->hideProgressIndicator();
+        $this->logger->log($level, $message, $context);
+        if ($inProgress) {
+            $this->restoreProgressIndicator($inProgress);
+        }
+    }
+}