Search by

Render D2 diagrams (d2lang.com) in Symfony & Twig: local binary or Kroki, cached, profiler-ready.

Package info

github.com/aymericcucherousset/d2-symfony

Type:symfony-bundle

pkg:composer/aymericcucherousset/d2-symfony

Statistics

Installs: 8

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 6

v0.7.0 2026-11-29 11:30 UTC

This package is auto-updated.

Last update: 2026-11-29 11:32:45 UTC


README

CI Coverage PHP Version PHPStan License

Write D2 diagrams as text in your Twig templates and get inline SVG, rendered by Symfony.

Note

Early development (0.7): the API may change between minor versions. See the roadmap. 0.7 is the last minor release before 1.0 freezes the API: feedback is welcome.

How a diagram is rendered: a Twig template uses the |d2 filter, which sends a Diagram to the BinaryRenderer; the d2 binary reads it on stdin and writes the SVG on stdout.

This diagram was rendered by the bundle itself from docs/example.d2.

Features

  • Twig: {{ source|d2 }} filter, {% d2 %}…{% endd2 %} tag and {{ d2_file('diagrams/architecture.d2') }} function, all outputting inline SVG
  • Markdown: ```d2 code blocks rendered by |markdown_to_html (league/commonmark)
  • PNG and PDF output, and |d2_data_uri for HTML-to-PDF generators and emails
  • Local d2 binary, downloaded and verified by bin/console d2:download, or a Kroki server, or both with a fallback
  • Cached rendering, static diagrams pre-rendered at cache:warmup
  • untrusted option for diagrams written by users: blocks the d2 features that read server files or inject markup (security)
  • Typed PHP API (Renderer, Diagram, RenderOptions, Theme and Layout enums), d2:render command and a Web Profiler panel

Requirements

  • PHP 8.2 or later
  • Symfony 6.4, 7.4 or 8.x
  • Twig 3.21 or later (symfony/twig-bundle), for the Twig filters, function and tag
  • The d2 binary, downloaded for you by bin/console d2:download (Linux, macOS and Windows on amd64 or arm64), or a Kroki server

Installation

composer require aymericcucherousset/d2-symfony symfony/http-client
bin/console d2:download

With Symfony Flex, the bundle is registered automatically; otherwise add Aymericcucherousset\D2Symfony\D2SymfonyBundle::class => ['all' => true] to config/bundles.php. Add /var/d2/ to your .gitignore, and run d2:download when you deploy too. No configuration is required.

symfony/http-client is only needed by d2:download and the Kroki renderer, and symfony/console by the commands: with d2 installed yourself, the bundle works with the local binary and Twig alone (see Requirements).

Usage

{{ 'shop -> payment: checkout'|d2 }}

{% d2 with { theme: 200, layout: 'elk' } %}
user -> app: {{ protocol }}
app -> db
{% endd2 %}

{{ d2_file('diagrams/architecture.d2', { sketch: true }) }}

<img src="{{ architecture|d2_data_uri }}" alt="Architecture"> {# PNG, e.g. for PDF generators #}
use Aymericcucherousset\D2Symfony\Core\Diagram;
use Aymericcucherousset\D2Symfony\Core\Renderer;
use Aymericcucherousset\D2Symfony\Core\RenderOptions;
use Aymericcucherousset\D2Symfony\Core\Theme;

final class ArchitectureDiagram
{
    public function __construct(private Renderer $renderer) // autowired
    {
    }

    public function svg(): string
    {
        return $this->renderer->render(new Diagram('a -> b', new RenderOptions(theme: Theme::DarkMauve)));
    }
}
bin/console d2:render diagram.d2 -o public/diagram.svg --layout=elk

Documentation

The full documentation is in docs/:

Backward compatibility

From 1.0, the bundle follows Semantic Versioning (MAJOR.MINOR.PATCH):

  • patch releases only fix bugs and security issues;
  • minor releases add features and may deprecate some, but never break the public API;
  • major releases may break it, and remove what was deprecated; the CHANGELOG lists each break with the change to make.

Public API

The promise covers:

  • the Core namespace: Renderer, VersionAwareRenderer and the renderers, Diagram, RenderOptions, the Theme, Layout and Format enums and the exceptions;
  • D2SymfonyBundle and its configuration, the d2.renderer service (autowired as Renderer) and the cache.d2 pool;
  • the Twig |d2 and |d2_data_uri filters, the d2_file() function and the {% d2 %} tag;
  • D2CommonMarkExtension (service d2.commonmark.extension);
  • the d2:download and d2:render commands and their options.

For these, a minor release never removes or renames a class, method, constant, enum case, configuration option, service, Twig name or command option, never changes their meaning, and never changes a signature in a way that breaks callers. Parameter names are covered too, since RenderOptions is meant to be built with named arguments. Adding optional parameters, options, enum cases or exception classes is not a break.

Classes are final: use them, don't extend them. The Renderer and VersionAwareRenderer interfaces can be implemented, and get no new method before the next major version. The exception interfaces (D2Exception, RendererUnavailableException) are for catch blocks.

Not covered

These may change in any release:

  • classes and methods marked @internal, and the d2.* service IDs not listed above;
  • the exact SVG, PNG and PDF output, which depends on d2: a minor release may update the d2 version installed by d2:download (d2.binary.version) to a newer one tested with the bundle;
  • cache keys: after an upgrade, diagrams may be rendered again once;
  • exception and log messages, the command output and the Web Profiler panel.

Support for a PHP or Symfony version that reached its end of life may be dropped in a minor release: Composer then keeps the previous version of the bundle on applications that still use it.

Deprecations

A feature to be removed is first deprecated in a minor release: it keeps working, is marked @deprecated, triggers a PHP deprecation (shown by the Symfony profiler and PHPUnit) and is listed under "Deprecated" in the CHANGELOG with its replacement. It is only removed in the next major release.

Before 1.0, minor releases (0.x) could break the API; these breaks are flagged BC break in the CHANGELOG.

Roadmap

Milestone Goal
v0.1.0 — Working core Render a D2 diagram to SVG from Twig using a manually installed binary.
v0.2.0 — Caching & Twig ergonomics Cached rendering, {% d2 %} tag, readable errors.
v0.3.0 — Frictionless installation The d2 binary is downloaded automatically.
v0.4.0 — Kroki renderer HTTP renderer for environments where binaries cannot run.
v0.5.0 — Developer experience Profiler panel, rendering CLI, fully typed options.
v0.6.0 — Formats & integrations PNG/PDF output, .d2 files, Markdown code blocks.
v0.7.0 — Hardening & feedback Untrusted sources, production load, feedback before freezing the API.
v1.0.0 — Stabilization Frozen API, complete documentation, release.

Development

composer install
composer ci        # PHP-CS-Fixer + PHPStan + Deptrac + PHPUnit
composer cs-fix    # apply coding standards
composer coverage  # HTML coverage report in var/coverage/ (needs pcov or Xdebug)

Deptrac keeps src/Core isolated so it can become a standalone library: Core may only depend on symfony/process, symfony/http-client-contracts, psr/cache and psr/log, and never on the bundle code (composer deptrac).

Integration tests need the d2 binary on your PATH; they are skipped otherwise (set D2_REQUIRED=1 to make them fail instead).

CI runs PHPUnit against Symfony 6.4, 7.4 and 8.x on every PHP version each one supports (8.2 to 8.6) with the highest dependencies, plus a --prefer-lowest job for each Symfony version on its lowest PHP version. To test one Symfony version locally, restrict it with Symfony Flex:

composer global config allow-plugins.symfony/flex true && composer global require symfony/flex
SYMFONY_REQUIRE='7.4.*' composer update           # add --prefer-lowest for the lowest versions

A separate CI job measures the code coverage with the d2 binary installed, so integration tests count. It fails when less than 95 % of the lines of src/ are covered, writes a summary on the workflow run page and uploads the report to Codecov, which comments the coverage and its diff against main on pull requests.

To regenerate the README diagram after editing docs/example.d2:

d2 --pad=20 docs/example.d2 docs/images/example.svg

Changelog

The CHANGELOG follows Keep a Changelog. Every pull request with a user-facing change adds an entry under [Unreleased] (Added, Changed, Deprecated, Removed, Fixed or Security), with a link to its issue; BC breaks are flagged BC break and say what to change.

Releasing

  1. Open a chore: release X.Y.Z pull request that:

    • moves the [Unreleased] entries to a new ## [X.Y.Z] - YYYY-MM-DD section, and updates the [Unreleased] and [X.Y.Z] compare links at the bottom of the file;
    • points the dev-main branch alias of composer.json to the next minor version (X.(Y+1).x-dev) when it changes.
  2. Once it is merged, tag main and push the tag:

    git switch main && git pull
    git tag -a vX.Y.Z -m "vX.Y.Z — <milestone name>"
    git push origin vX.Y.Z
  3. The Release workflow then checks that the tag is on main and that the CHANGELOG has its section, publishes the GitHub release, named after the vX.Y.Z — … milestone, with the notes of that section and a link to the full changelog, and closes the milestone once its issues are closed. A vX.Y.Z-rc.N tag, with its own [X.Y.Z-rc.N] section, publishes a pre-release and leaves the milestone open. Packagist picks the new tag up automatically.

Preview the notes before tagging with .github/scripts/release-notes.sh vX.Y.Z: it fails like the workflow when the section is missing. A failed run can be re-run from the Actions tab; it updates the release when it already exists. If the tag itself is wrong (e.g. pushed before the release pull request was merged), delete it right away (git push --delete origin vX.Y.Z, then git tag -d vX.Y.Z) and tag again: never move a tag once Packagist has published it.

License

MIT — see LICENSE.