packages/sql-formatter/src/Core/Layout/Renderer.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlFormatter\Core\Layout;
6
7use SqlFormatter\Core\FormatOptions;
8use SqlFormatter\Core\Syntax\Document;
9
10/**
11 * Applies one layout to grammar-derived clauses, lists, and nested blocks.
12 *
13 * @visibility SqlFormatter
14 */
15final class Renderer
16{
17    private readonly Writer $writer;
18
19    /**
20     * Starts a fresh output buffer for the supplied document.
21     */
22    public function __construct(private readonly Document $document, private readonly FormatOptions $options)
23    {
24        $this->writer = new Writer();
25    }
26
27    /**
28     * Writes a complete document and its final comments.
29     */
30    public function render(): string
31    {
32        (new Block($this->document, $this->writer, $this->options))->render(0, count($this->document->tokens) - 1, 0);
33        return $this->writer->finish($this->document->trailing);
34    }
35
36}
37