appique/laravel-class-generator

Class generator with customizable stubs and placeholders

v1.0.0 2025-08-17 20:12 UTC

This package is auto-updated.

Last update: 2025-08-18 07:50:01 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Introduction

A lightweight Laravel package that simplifies class generation from stub files with dynamic placeholder replacement.

Installation

You can install the package via composer:

composer require appique/laravel-class-generator

Usage

Stub Files

First, create stub files in your project's stubs/ directory. Stub files are templates that use double curly braces for placeholders:

// stubs/service.stub
<?php

namespace {{ namespace }};

use App\Models\{{ model }};
use App\Repositories\{{ repository }};

class {{ class }}
{
    public function __construct(
        private {{ repository }} $repository
    ) {}
    
    public function process({{ model }} $model): void
    {
        // Implementation
    }
}

The following placeholders are always available:

  • {{ namespace }} - The class namespace
  • {{ class }} - The class name
  • {{ fqcn }} - Fully qualified class name

You can add any custom placeholders you need, and pass their values when generating classes.

Basic Usage

Once you have your stub files, generate classes from them:

use Appique\ClassGenerator\Console\ClassGenerator;

// Create a service class in the App namespace
ClassGenerator::make(className: 'UserService', stub: 'service')
    ->inAppNamespace('Services')
    ->placeholders([
        'model' => 'User',
        'repository' => 'UserRepository'
    ])
    ->create();

This will:

  1. Load the stubs/service.stub file
  2. Replace all placeholders with provided values
  3. Create app/Services/UserService.php with the generated content

Working with Different Namespaces

// Create a test class
ClassGenerator::make(className: 'UserServiceTest', stub: 'test')
    ->inTestNamespace('Unit', 'Services')
    ->placeholders(['class_under_test' => 'UserService'])
    ->create();
// Creates: tests/Unit/Services/UserServiceTest.php

// Create a factory class
ClassGenerator::make(className: 'ProductFactory', stub: 'model-factory')
    ->inFactoriesNamespace()
    ->placeholders(['model' => 'Product'])
    ->create();
// Creates: database/factories/ProductFactory.php

// Create a class with custom namespace
ClassGenerator::make(className: 'Services\\OrderProcessor', stub: 'service')
    ->placeholders(['author' => 'John Doe'])
    ->create();
// Creates: src/Services/OrderProcessor.php

Additional Options

// Force overwrite existing files
ClassGenerator::make(className: 'ExistingClass', stub: 'class')
    ->inAppNamespace()
    ->create(force: true);

// Execute callback after class generation
ClassGenerator::make(className: 'MyClass', stub: 'class')
    ->inAppNamespace()
    ->create(callback: fn(string $path) => Process::run("vendor/bin/pint {$path}");

Stub Resolution

The package looks for stub files in the following order:

  1. Published stubs: base_path('stubs/your-stub.stub')
  2. Package stubs: Auto-detected from vendor packages
  3. Custom fallback location (if specified)

Using in Your Own Package

When using this generator in your own package, it automatically detects the package's stubs directory:

// In your package's command or service class
namespace YourVendor\YourPackage\Commands;

use Appique\ClassGenerator\Console\ClassGenerator;

class MakeModelCommand extends Command
{
    public function handle()
    {
        // Automatically looks in vendor/your-vendor/your-package/stubs/
        ClassGenerator::make($this->argument('name'), 'model')
            ->inAppNamespace('Models')
            ->placeholders([
                'table' => Str::snake(Str::plural($this->argument('name')))
            ])
            ->create();
    }
}

Testing

Run the test suite:

composer test

Run tests with coverage:

composer test-coverage

Run static analysis:

composer analyse

Fix code style:

composer format

Run all checks in one go:

composer check

Changelog

Please see CHANGELOG for more information on recent changes.

Contributing

Contributions are welcome! Please see CONTRIBUTING for details.

Development Setup

  1. Fork the repository
  2. Clone your fork
  3. Install dependencies: composer install
  4. Create a feature branch
  5. Make your changes
  6. Run checks: composer check (formats code, runs static analysis, and tests)
  7. Submit a pull request

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Acknowledgments

This package was inspired by the Laravel community, particularly: