- Can jms/cg generate Laravel Eloquent models or API Resources automatically?
- No, jms/cg is a low-level code generation library and doesn’t natively support Laravel-specific constructs like Eloquent models or API Resources. You’d need to manually define the class structure and properties, then extend or wrap the generated code with Laravel traits or interfaces. For example, you could generate a base class and then apply `HasFactory` or `Resource` traits afterward.
- How do I install jms/cg in a Laravel project?
- Add it via Composer: `composer require jms/cg`. Since it has no Laravel dependencies, it works alongside any Laravel version (though PHP 7.4+ is recommended). For testing, ensure your `composer.json` includes PHPUnit 9.x constraints to avoid conflicts, as the package was built for older PHPUnit versions.
- Will jms/cg work with Laravel 10 and PHP 8.2+?
- No, jms/cg was last updated in 2016 and lacks support for PHP 8.x features like named arguments, union types, or attributes—common in Laravel 9+. You’d need to polyfill missing features (e.g., via `php-compat`) or fork the package. Test thoroughly, as DocBlock generation or AST-like elements may break with modern PHP syntax.
- Can I use jms/cg to generate migrations alongside models?
- No, jms/cg focuses on PHP source code generation, not database schema changes. For migrations, you’d need to integrate it with Laravel’s migration system manually, such as parsing generated model properties to create corresponding `Schema::create` calls. Tools like Laravel Shift or custom Artisan commands could bridge this gap.
- How do I generate a Laravel Service class with jms/cg?
- Define a class using jms/cg’s fluent API, then specify methods like `handle()`, properties (e.g., `$repository`), and DocBlocks with `@method` or `@property` annotations. For example, create a `Service` class with a `create()` method and inject dependencies via constructor. The output will be a clean PHP file ready to extend with Laravel’s `bind()` or `call()` methods.
- Are there alternatives to jms/cg for Laravel scaffolding?
- Yes. For Laravel-specific scaffolding, consider **Laravel Shift** (API/CRUD generators), **Filament/Spatie generators** (admin panels), or **Symfony’s MakerBundle** (if using Symfony components). For frontend codegen, Blade templates or Inertia/Vue scaffolding tools may fit better. jms/cg is best for custom, low-level codegen where you need fine-grained control over PHP syntax.
- How can I trigger jms/cg during Laravel’s build process?
- Use Composer scripts (e.g., `post-install-cmd`) or create a custom Artisan command to invoke jms/cg. For example, add a `generate:entity` command that reads metadata (e.g., from a config file) and outputs classes to `app/Generated`. Treat generated files as ephemeral or version-control them if they’re part of your build pipeline.
- Does jms/cg support generating Laravel’s Form Request validation rules?
- Not directly. jms/cg generates PHP classes, so you’d need to define validation rules as properties or methods (e.g., `protected $rules = [...]`) and manually implement `authorize()` or `rules()` methods. For dynamic validation, combine it with Laravel’s `Request` facade or a custom validator class generated by jms/cg.
- How do I handle DocBlock generation for Laravel-specific annotations?
- Use jms/cg’s DocBlock builder to add custom annotations like `@property-read` or `@method`. For Laravel-specific tags (e.g., `@mixin`, `@template`), extend the DocBlock class or pre-process annotations before generation. Example: `$docBlock->addTag('property', ['type' => 'Collection', 'var' => '$items'])` for a collection property.
- Is jms/cg safe to use in production if it’s unmaintained?
- Proceed with caution. The package’s Apache 2.0 license is permissive, but lack of updates means no PHP 8.x fixes or security patches. Audit dependencies (e.g., older PHPUnit) for vulnerabilities and consider forking it if critical. For production, pair it with a wrapper layer (e.g., Artisan command) to isolate changes and test thoroughly in a staging environment.