Skip to content

Customizing Stubs ​

Every generated file comes from an editable template. If the default code style isn't yours, change the templates rather than the generated files.

Publish the stubs ​

bash
php artisan vendor:publish --tag=api-generator-stubs

This copies every .stub into stubs/vendor/laravel-api-generator/. The StubLoader always checks this folder first and falls back to the package defaults, so you can override only the stubs you need.

Validate your customizations ​

api-generator:validate-stubs checks that every required {{placeholder}} is still present in your modified templates, and flags the ones written for 3.x (see Upgrading to 4.0):

bash
php artisan api-generator:validate-stubs

The --json form fits into a CI pipeline, with machine-readable output and exit code 1 on error, enough to catch a broken stub before it reaches a teammate's machine. The VS Code extension runs this validation automatically before each generation.

Extending the generator ​

Need a whole new artifact type? Create a custom generator by extending AbstractGenerator:

php
use nameless\CodeGenerator\EntitiesGenerator\AbstractGenerator;
use nameless\CodeGenerator\ValueObjects\EntityDefinition;

class CustomGenerator extends AbstractGenerator
{
    public function getType(): string
    {
        return 'Custom';
    }

    public function getOutputPath(EntityDefinition $definition): string
    {
        return app_path("Custom/{$definition->name}Custom.php");
    }

    protected function getStubName(): string
    {
        return 'custom'; // loads stubs/custom.stub
    }

    protected function getReplacements(EntityDefinition $definition): array
    {
        return ['modelName' => $definition->name];
    }

    protected function generateContent(EntityDefinition $definition): string
    {
        return $this->processStub($definition);
    }
}

Register it in your service provider and it is called automatically during generation.