private function formatTextBlock($text, $prefix = '', $prefixAllLines = false)
    {
        if (empty($text)) {
            return $text;
        }

        $textLines = explode(PHP_EOL, $text);

        // remove the trailing PHP_EOL (and add it back afterwards) to avoid formatting issues
        $addTrailingNewline = false;
        if ('' === $textLines[count($textLines) - 1]) {
            array_pop($textLines);
            $addTrailingNewline = true;
        }

        $newTextLines = array();
        foreach ($textLines as $line) {
            if ($prefixAllLines) {
                $newTextLines[] = $prefix.$line;
            } else {
                if (isset($line[0]) && ' ' !== $line[0]) {
                    $newTextLines[] = $prefix.$line;
                } else {
                    $newTextLines[] = str_repeat(' ', strlen($prefix)).$line;
                }
            }
        }

        return implode(PHP_EOL, $newTextLines).($addTrailingNewline ? PHP_EOL : '');
    }