Chris@4: formatterManager = $formatterManager; Chris@4: return $this; Chris@4: } Chris@4: Chris@4: /** Chris@4: * Return the formatter manager Chris@4: * @return FormatterManager Chris@4: */ Chris@4: public function formatterManager() Chris@4: { Chris@4: return $this->formatterManager; Chris@4: } Chris@4: Chris@4: public function setDisplayErrorFunction(callable $fn) Chris@4: { Chris@4: $this->displayErrorFunction = $fn; Chris@4: return $this; Chris@4: } Chris@4: Chris@4: /** Chris@4: * Handle the result output and status code calculation. Chris@4: */ Chris@4: public function handle(OutputInterface $output, $result, CommandData $commandData, $statusCodeDispatcher = null, $extractDispatcher = null) Chris@4: { Chris@4: // A little messy, for backwards compatibility: if the result implements Chris@4: // ExitCodeInterface, then use that as the exit code. If a status code Chris@4: // dispatcher returns a non-zero result, then we will never print a Chris@4: // result. Chris@4: $status = null; Chris@4: if ($result instanceof ExitCodeInterface) { Chris@4: $status = $result->getExitCode(); Chris@4: } elseif (isset($statusCodeDispatcher)) { Chris@4: $status = $statusCodeDispatcher->determineStatusCode($result); Chris@4: if (isset($status) && ($status != 0)) { Chris@4: return $status; Chris@4: } Chris@4: } Chris@4: // If the result is an integer and no separate status code was provided, then use the result as the status and do no output. Chris@4: if (is_integer($result) && !isset($status)) { Chris@4: return $result; Chris@4: } Chris@4: $status = $this->interpretStatusCode($status); Chris@4: Chris@4: // Get the structured output, the output stream and the formatter Chris@4: $structuredOutput = $result; Chris@4: if (isset($extractDispatcher)) { Chris@4: $structuredOutput = $extractDispatcher->extractOutput($result); Chris@4: } Chris@4: if (($status != 0) && is_string($structuredOutput)) { Chris@4: $output = $this->chooseOutputStream($output, $status); Chris@4: return $this->writeErrorMessage($output, $status, $structuredOutput, $result); Chris@4: } Chris@4: if ($this->dataCanBeFormatted($structuredOutput) && isset($this->formatterManager)) { Chris@4: return $this->writeUsingFormatter($output, $structuredOutput, $commandData, $status); Chris@4: } Chris@4: return $this->writeCommandOutput($output, $structuredOutput, $status); Chris@4: } Chris@4: Chris@4: protected function dataCanBeFormatted($structuredOutput) Chris@4: { Chris@4: if (!isset($this->formatterManager)) { Chris@4: return false; Chris@4: } Chris@4: return Chris@4: is_object($structuredOutput) || Chris@4: is_array($structuredOutput); Chris@4: } Chris@4: Chris@4: /** Chris@4: * Determine the formatter that should be used to render Chris@4: * output. Chris@4: * Chris@4: * If the user specified a format via the --format option, Chris@4: * then always return that. Otherwise, return the default Chris@4: * format, unless --pipe was specified, in which case Chris@4: * return the default pipe format, format-pipe. Chris@4: * Chris@4: * n.b. --pipe is a handy option introduced in Drush 2 Chris@4: * (or perhaps even Drush 1) that indicates that the command Chris@4: * should select the output format that is most appropriate Chris@4: * for use in scripts (e.g. to pipe to another command). Chris@4: * Chris@4: * @return string Chris@4: */ Chris@4: protected function getFormat(FormatterOptions $options) Chris@4: { Chris@4: // In Symfony Console, there is no way for us to differentiate Chris@4: // between the user specifying '--format=table', and the user Chris@4: // not specifying --format when the default value is 'table'. Chris@4: // Therefore, we must make --field always override --format; it Chris@4: // cannot become the default value for --format. Chris@4: if ($options->get('field')) { Chris@4: return 'string'; Chris@4: } Chris@4: $defaults = []; Chris@4: if ($options->get('pipe')) { Chris@4: return $options->get('pipe-format', [], 'tsv'); Chris@4: } Chris@4: return $options->getFormat($defaults); Chris@4: } Chris@4: Chris@4: /** Chris@4: * Determine whether we should use stdout or stderr. Chris@4: */ Chris@4: protected function chooseOutputStream(OutputInterface $output, $status) Chris@4: { Chris@4: // If the status code indicates an error, then print the Chris@4: // result to stderr rather than stdout Chris@4: if ($status && ($output instanceof ConsoleOutputInterface)) { Chris@4: return $output->getErrorOutput(); Chris@4: } Chris@4: return $output; Chris@4: } Chris@4: Chris@4: /** Chris@4: * Call the formatter to output the provided data. Chris@4: */ Chris@4: protected function writeUsingFormatter(OutputInterface $output, $structuredOutput, CommandData $commandData, $status = 0) Chris@4: { Chris@4: $formatterOptions = $commandData->formatterOptions(); Chris@4: $format = $this->getFormat($formatterOptions); Chris@4: $this->formatterManager->write( Chris@4: $output, Chris@4: $format, Chris@4: $structuredOutput, Chris@4: $formatterOptions Chris@4: ); Chris@4: return $status; Chris@4: } Chris@4: Chris@4: /** Chris@4: * Description Chris@4: * @param OutputInterface $output Chris@4: * @param int $status Chris@4: * @param string $structuredOutput Chris@4: * @param mixed $originalResult Chris@4: * @return type Chris@4: */ Chris@4: protected function writeErrorMessage($output, $status, $structuredOutput, $originalResult) Chris@4: { Chris@4: if (isset($this->displayErrorFunction)) { Chris@4: call_user_func($this->displayErrorFunction, $output, $structuredOutput, $status, $originalResult); Chris@4: } else { Chris@4: $this->writeCommandOutput($output, $structuredOutput); Chris@4: } Chris@4: return $status; Chris@4: } Chris@4: Chris@4: /** Chris@4: * If the result object is a string, then print it. Chris@4: */ Chris@4: protected function writeCommandOutput( Chris@4: OutputInterface $output, Chris@4: $structuredOutput, Chris@4: $status = 0 Chris@4: ) { Chris@4: // If there is no formatter, we will print strings, Chris@4: // but can do no more than that. Chris@4: if (is_string($structuredOutput)) { Chris@4: $output->writeln($structuredOutput); Chris@4: } Chris@4: return $status; Chris@4: } Chris@4: Chris@4: /** Chris@4: * If a status code was set, then return it; otherwise, Chris@4: * presume success. Chris@4: */ Chris@4: protected function interpretStatusCode($status) Chris@4: { Chris@4: if (isset($status)) { Chris@4: return $status; Chris@4: } Chris@4: return 0; Chris@4: } Chris@4: }