cycle / schema-renderer
Utils for Cycle ORM Schema rendering
Requires
- php: >=8.2
- cycle/orm: ^2.19
- symfony/polyfill-php83: ^1.31.0
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.8
- buggregator/trap: ^1.20
- cycle/testing: ^0.1.2
- llm/skills: ^1.20
- roxblnfk/unpoly: ^1.8
- testo/testo: ^0.10.60
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Render a Cycle ORM schema for a terminal, a PHP file or a Mermaid diagram
This package renders a Cycle ORM schema in a human-readable form: as text for a terminal, as a PHP file that returns the schema array, or as a Mermaid class diagram.
Get Started
Installation
The package requires PHP 8.2+.
composer require cycle/schema-renderer
Convert schema to array
$schema = new Schema([...]); $converter = new \Cycle\Schema\Renderer\SchemaToArrayConverter(); $schemaArray = $converter->convert($schema);
If the passed SchemaInterface doesn't have a toArray() method, the SchemaToArrayConverter converts
only the common properties from Cycle\ORM\SchemaInterface and skips null values.
To keep custom properties, pass their keys as the second argument:
$converter = new \Cycle\Schema\Renderer\SchemaToArrayConverter(); $schemaArray = $converter->convert($schema, [ 42, CustomClass::CUSTOM_PROPERTY, ... ]);
Render schema to a terminal
use Cycle\Schema\Renderer\OutputSchemaRenderer; $output = new \Symfony\Component\Console\Output\ConsoleOutput(); $renderer = new OutputSchemaRenderer(format: OutputSchemaRenderer::FORMAT_CONSOLE_COLOR); $output->write($renderer->render($schemaArray));
By default, OutputSchemaRenderer renders only the common properties and relations with its template.
Custom properties are rendered as is in a separate block.
To extend the default template, create custom renderers and add them to the output renderer.
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter; use Cycle\Schema\Renderer\ConsoleRenderer\Renderer; use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\PropertyRenderer; use Cycle\Schema\Renderer\OutputSchemaRenderer; class CustomPropertyRenderer implements Renderer { public function render(Formatter $formatter, array $schema, string $role): string { $data = $schema[CustomClass::CUSTOM_PROPERTY] ?? null; return \sprintf( '%s: %s', $formatter->title('My custom property'), $data === null ? $formatter->error('not defined') : $formatter->typecast($data), ); } } $renderer = new OutputSchemaRenderer(); $renderer->addRenderer( new CustomPropertyRenderer(), new PropertyRenderer(CustomClass::OTHER_PROPERTY, 'My super property'), ); $output->write($renderer->render($schemaArray));
Store schema in a PHP file
use Cycle\Schema\Renderer\PhpSchemaRenderer; $path = __DIR__ . '/schema.php'; $renderer = new PhpSchemaRenderer(); \file_put_contents($path, $renderer->render($schemaArray));
The renderer generates valid PHP code in which constants from Cycle ORM classes replace raw keys for better readability.
Render schema as a Mermaid diagram
use Cycle\Schema\Renderer\MermaidRenderer\MermaidRenderer; $diagram = (new MermaidRenderer())->render($schemaArray);
The result is a Mermaid class diagram of the entities and their relations.