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.
The package requires PHP 8.2+.
composer require cycle/schema-renderer$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,
...
]);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));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.
use Cycle\Schema\Renderer\MermaidRenderer\MermaidRenderer;
$diagram = (new MermaidRenderer())->render($schemaArray);The result is a Mermaid class diagram of the entities and their relations.