Skip to content

feat(console): render Cycle schema to PHP file (+roles filter, overwrite)#101

Merged
roxblnfk merged 4 commits intospiral:2.xfrom
petrdobr:feat/render-schema-to-php-file
Oct 22, 2025
Merged

feat(console): render Cycle schema to PHP file (+roles filter, overwrite)#101
roxblnfk merged 4 commits intospiral:2.xfrom
petrdobr:feat/render-schema-to-php-file

Conversation

@petrdobr
Copy link
Copy Markdown
Contributor

@petrdobr petrdobr commented Oct 8, 2025

  • New option to write Cycle ORM schema to a PHP file for cycle:render
  • Filter by roles to limit the exported subset
  • Overwrite flag to safely replace existing file

What was changed

  • Extended the existing cycle:render console command:
    • -o, --output=/path/to/schema.php — writes rendered schema to a file (supported only for format=php).
    • --overwrite — allows replacing an existing output file.
    • -r, --role=<role> [--role=<role> ...] — filters the rendered schema by roles; accepts multiple flags and comma-separated values (e.g. --role=auth,rbac --role=default).
  • STDOUT behavior for all formats is preserved. If --output is not provided, output goes to STDOUT as before.
  • For non-php formats with --output, the command fails with an explicit error.
  • Safe write: content is written to a temporary file in the target directory and then atomically moved to the destination; parent directories are created as needed.

Examples

# to stdout (unchanged)
php app.php cycle:render php

# to file
php app.php cycle:render php --output=var/schema.php

# filter by roles (comma-separated, or multiple --role flags)
php app.php cycle:render php --output=var/schema.auth.php --role=auth,rbac
php app.php cycle:render php --output=var/schema.default.php --role=default --overwrite

Edge cases & messages

  • Unknown roles are reported as a warning and ignored.
  • If the role filter yields no entries:
    • with --output → prints “Nothing to write.” and exits successfully;
    • without --output → prints an empty line to STDOUT.
  • If the destination file already exists without --overwrite, the command fails with an explicit message.
  • Directory creation and file write failures are reported with descriptive errors.

Why?

  • A deterministic PHP artifact of the schema is useful for audits, code reviews, caching in CI and reproducible builds.
  • Role-based filtering keeps artifacts minimal and context-specific (e.g., exporting only auth-related roles).
  • Preserving STDOUT behavior keeps the command backward compatible, while --output enables file-based workflows.

Checklist

Documentation

New options:

  • -o, --output=PATH — write rendered schema to a PHP file (supported only when format=php).
  • --overwrite — allow replacing an existing output file.
  • -r, --role=ROLE — filter exported schema by roles (for any format).
    Accepts multiple flags and comma-separated values inside a flag (e.g. --role=auth,rbac --role=default).
    Role matching is case-insensitive; unknown roles are ignored with a warning.

Behavior & edge cases:

  • Without --output the command writes to STDOUT (unchanged behavior).
  • If --output is used with a non-php format → the command fails with an explicit error.
  • If filtering results in an empty schema:
    • with --output → prints “Nothing to write.” and exits successfully;
    • without --output → prints an empty line to STDOUT.
  • Safe write: schema is written to a temp file in the target dir and then atomically moved; parent dirs are created if missing.

Examples

# stdout (unchanged)
php app.php cycle:render php

# write to file
php app.php cycle:render php --output=var/schema.php

# filter by roles (comma-separated or multiple flags)
php app.php cycle:render php --output=var/schema.auth.php --role=auth,rbac
php app.php cycle:render php --output=var/schema.default.php --role=default --overwrite

Comment thread src/Console/Command/CycleOrm/RenderCommand.php
…e filtering

- New console command to export Cycle ORM schema into a PHP file
- Filter by roles to limit the exported subset
- Overwrite flag to safely replace existing file
@petrdobr petrdobr force-pushed the feat/render-schema-to-php-file branch from 828eaf1 to 85a4498 Compare October 9, 2025 16:20
Comment thread src/Console/Command/CycleOrm/RenderCommand.php Outdated
Comment thread src/Console/Command/CycleOrm/RenderCommand.php Outdated
Comment thread src/Console/Command/CycleOrm/RenderCommand.php
Comment thread src/Console/Command/CycleOrm/RenderCommand.php Outdated
Comment thread src/Console/Command/CycleOrm/RenderCommand.php Outdated
@roxblnfk
Copy link
Copy Markdown
Member

And eah, please don't overwrite reviewed commits via force pushe

@petrdobr
Copy link
Copy Markdown
Contributor Author

And eah, please don't overwrite reviewed commits via force pushe

Understood, sorry for rewriting them earlier and thanks for the advice :)

petrdobr and others added 2 commits October 21, 2025 23:24
- Make roles an optional argument (comma-separated). If omitted export full schema.
- Add --format=php|mermaid|color|plain; default is
  color for ANSI-capable terminals, otherwise plain.
- Allow --output for all formats; automatically overwrite existing files.
- Update command description/help with usage examples.
simplify and apply code style
@roxblnfk roxblnfk force-pushed the feat/render-schema-to-php-file branch from 56483d3 to 43ab4fb Compare October 22, 2025 07:09
@roxblnfk roxblnfk merged commit 515c94f into spiral:2.x Oct 22, 2025
8 of 9 checks passed
@roxblnfk
Copy link
Copy Markdown
Member

Good feature, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add console command for Schema rendering into PHP file

2 participants