You are here

public function Debugger::writeTable in Acquia Purge 8

Write tabular data rendered as table to Drupal's debug output.

Parameters

mixed[] $table: Associative array with each key being the row title, when the array key is an integer, the row will be fully used. Non-string data will be rendered using json_encode().

string $title: Optional title to render above the table content.

Throws

\LogicException Thrown when the debugger isn't enabled.

Overrides DebuggerInterface::writeTable

File

src/Plugin/Purge/Purger/Debugger.php, line 257

Class

Debugger
Provides a centralized debugger for Acquia purger plugins.

Namespace

Drupal\acquia_purge\Plugin\Purge\Purger

Code

public function writeTable(array $table, $title = NULL) {
  if (!$this->enabled) {
    throw new \LogicException("Cannot call ::writeTable().");
  }
  if ($title) {
    $this
      ->writeTitle($title, TRUE, FALSE);
  }
  $this
    ->writeSeparator('-');
  $longest_key = max(array_map('strlen', array_keys($table)));
  foreach ($table as $key => $value) {
    $spacing = '';

    // Determine how the left-side of the table looks like.
    $left = '| ';
    if (!is_int($key)) {
      $spacing = str_repeat(' ', $longest_key - strlen($key));
      $left = '| ' . $key . $spacing . ' | ';
    }

    // Treat all values as potential multiline and render accordingly.
    if (!is_string($value)) {
      $value = json_encode($value);
    }
    foreach (explode("\n", $value) as $line) {
      $this
        ->write($left . $line);

      // Render empty columns on the left after rendering the key once.
      if (!is_int($key)) {
        $left = '| ' . str_repeat(' ', strlen($key)) . $spacing . ' | ';
      }
    }
  }
  $this
    ->writeSeparator('-');
}