Installation
composer require akimmaksimov85/creator "dev-master"
Note: The package is in dev-master; ensure your project can handle unstable dependencies.
Publish Configuration
php artisan vendor:publish --provider="Akimmaksimov85\Creator\CreatorServiceProvider"
This generates a creator.php config file in config/.
First Use Case: Generate a Basic Entity
Register a command in routes/console.php:
use Akimmaksimov85\Creator\Commands\GenerateEntityCommand;
Artisan::command('creator:entity', GenerateEntityCommand::class);
Run:
php artisan creator:entity --name="User" --namespace="App\\Entities"
Expected Output: A new User entity class with annotations, UUID, and basic CRUD methods in app/Entities/User.php.
Generate Domain Layer
php artisan creator:entity --name="Post" --namespace="App\\Domain\\Posts" --with-repository
Output: Post.php (entity), PostRepositoryInterface.php, and PostRepository.php.
Generate Application Layer
php artisan creator:service --name="PostService" --entity="Post" --namespace="App\\Services"
Output: PostService.php with CRUD methods and dependency injection for the repository.
Generate API Endpoints
php artisan creator:controller --name="PostController" --entity="Post" --namespace="App\\Http\\Controllers"
Output: PostController.php with annotated routes (e.g., #[Route('/posts')]).
Generate Migrations
php artisan creator:migration --entity="Post" --fields="title:string,content:text,createdAt:datetime"
Output: A migration file with UUID and timestamp fields.
Custom Templates: Override default templates by publishing assets:
php artisan vendor:publish --tag="creator-templates" --provider="Akimmaksimov85\Creator\CreatorServiceProvider"
Modify files in resources/views/creator/.
Doctrine Annotations: The package auto-generates @ORM\Entity, @ORM\Table, and @ORM\Id annotations. Extend with custom annotations in the config:
'annotations' => [
'use' => ['Doctrine\ORM\Mapping as ORM'],
'custom' => ['@ORM\GeneratedValue(strategy="UUID")'],
],
Messenger Integration: Generated services include Messenger annotations (e.g., #[AsMessageHandler]). Configure framework.yaml for async handling:
messenger:
transports:
async: '%env(MESSENGER_TRANSPORT_DSN)%'
Validation: Use --with-validation flag to auto-generate Symfony Validator constraints:
php artisan creator:entity --name="User" --with-validation
Output: Includes @Assert\NotBlank and @Assert\Email annotations.
Namespace Conflicts
composer.json has custom paths, ensure namespace flags in commands match your project structure.--namespace (e.g., App\\Domain\\Posts instead of Domain/Posts).Doctrine ORM Dependency
doctrine/orm:^2.8). If you’re not using Doctrine, remove the doctrine/* dependencies from composer.json and override templates to strip ORM annotations.EntityGenerator class to skip ORM-specific logic.UUID Generation
symfony/uuid), update the config:
'uuid' => [
'library' => 'symfony/uuid',
'type' => 'Symfony\\Uid\\Uuid',
],
Command Registration
routes/console.php or use a package like laravel/artisan for dynamic registration.Symfony Console Dependencies
6.1.*). If your Laravel version is incompatible, alias the Symfony\Component\Console classes in composer.json:
"extra": {
"laravel": {
"dont-discover": ["akimmaksimov85/creator"]
}
}
Verbose Output
Add --verbose to commands to see generated code:
php artisan creator:entity --name="Test" --verbose
Template Debugging
To inspect the template being used, add this to your command’s handle() method:
\Log::info($this->getTemplateContents('entity.stub'));
Configuration Overrides
Temporarily override config in config/creator.php to test changes:
'entity' => [
'path' => 'custom/path',
'stub' => 'custom/Entity.stub',
],
Custom Generators
Extend the base generator classes (e.g., EntityGenerator, ServiceGenerator) in app/Generators:
namespace App\Generators;
use Akimmaksimov85\Creator\Generators\EntityGenerator;
class CustomEntityGenerator extends EntityGenerator {
protected function getStub(): string {
return __DIR__ . '/stubs/custom.entity.stub';
}
}
Bind it in AppServiceProvider:
$this->app->bind(
\Akimmaksimov85\Creator\Contracts\EntityGenerator::class,
\App\Generators\CustomEntityGenerator::class
);
Dynamic Field Generation
Hook into the fields event in your AppServiceProvider:
use Akimmaksimov85\Creator\Events\FieldsPrepared;
public function boot() {
FieldsPrepared::listen(function (FieldsPrepared $event) {
$event->fields[] = [
'name' => 'slug',
'type' => 'string',
'options' => ['unique' => true],
];
});
}
Post-Generation Hooks
Use the generation.finished event to run logic after generation:
use Akimmaksimov85\Creator\Events\GenerationFinished;
GenerationFinished::listen(function (GenerationFinished $event) {
if ($event->type === 'entity') {
// Run migrations, seeders, etc.
Artisan::call('migrate');
}
});
php artisan cache:clear
Artisan::queue([
'creator:entity' => ['--name' => 'User'],
'creator:entity' => ['--name' => 'Post'],
]);
How can I help you explore Laravel packages today?