cycle/schema-renderer
Render Cycle ORM schemas as terminal-friendly output or generate PHP/array representations. Convert Schema/SchemaInterface to arrays (including optional custom properties), then render with colorized Symfony Console output; extend templates via custom console renderers.
Installation Add the package via Composer:
composer require cycle/schema-renderer
Publish the config (if needed):
php artisan vendor:publish --provider="Cycle\SchemaRenderer\ServiceProvider"
Basic Setup
Register the SchemaRenderer service in your Laravel container (if not auto-discovered):
$this->app->bind('schema-renderer', function ($app) {
return new \Cycle\SchemaRenderer\Renderer($app['cycle.orm']);
});
First Use Case: Rendering a Schema Inject the renderer into a controller or service:
use Cycle\SchemaRenderer\Renderer;
public function showSchema(Renderer $renderer)
{
$schema = $renderer->render();
return response()->json($schema);
}
This generates a JSON schema of your Cycle ORM entities, now including improved relation block handling (e.g., COLLECTION_TYPE support) introduced in v1.4.0.
Schema for API Documentation Use the renderer to auto-generate OpenAPI/Swagger schemas from your Cycle entities:
$schema = $renderer->render(['include' => ['User', 'Post']]);
// Integrate with `darkaonline/l5-swagger` or `zircote/swagger-php`
Validation Layer Dynamically validate incoming requests against your database schema:
$validator = Validator::make($request->all(), $renderer->getValidationRules());
Database Migration Generation Extract schema definitions to generate migrations:
$tables = $renderer->renderTables();
// Use `spatie/laravel-migration-sniffer` to parse and convert
Relation-Aware Rendering (New in 1.4.0)
Leverage improved relation handling (e.g., COLLECTION_TYPE) for accurate schema representation:
$schema = $renderer->render(['include' => ['User'], 'withRelations' => true]);
// Now properly resolves collection-type relations (e.g., hasMany, belongsToMany)
webonyx/graphql-php), now with refined relation types.Circular References
If entities reference each other (e.g., User hasMany Post, Post belongsTo User), the renderer may hang. Use:
$renderer->render(['maxDepth' => 2]); // Limit recursion depth
Ignored Fields
Fields marked @Column(ignore: true) or excluded via $renderer->ignoreFields() won’t appear in the output.
Performance Rendering large schemas can be slow. Cache results:
$schema = Cache::remember('cycle_schema', now()->addHours(1), function () use ($renderer) {
return $renderer->render();
});
Relation Block Quirks (New in 1.4.0)
Ensure relation blocks (e.g., hasMany, belongsToMany) are correctly mapped. Test with:
$renderer->render(['debugRelations' => true]); // Logs relation parsing issues
php artisan schema:diff
string, int) match the renderer’s expectations. Use:
$renderer->setTypeMapper(new CustomTypeMapper());
COLLECTION_TYPE or other relation types, inspect the raw schema:
$rawSchema = $renderer->render(['raw' => true]);
Custom Formatters Override the default JSON output:
$renderer->setFormatter(new \Cycle\SchemaRenderer\Formatters\YamlFormatter());
Schema Hooks Modify the schema before rendering:
$renderer->beforeRender(function ($schema) {
$schema['metadata']['version'] = '1.0.0';
return $schema;
});
Entity Exclusions Exclude entire entities or namespaces:
$renderer->excludeEntities(['App\Models\Draft']);
Relation-Specific Customization (New in 1.4.0) Extend relation handling for custom types:
$renderer->extendRelationType('COLLECTION_TYPE', function ($relation) {
return ['type' => 'array', 'items' => $relation->getTargetSchema()];
});
How can I help you explore Laravel packages today?