A Package to Generate Custom Stubs in Laravel

Laravel Stub is a package that aims to enhance your development workflow in Laravel by providing a set of customizable stubs. Using the project's LaravelStub facade, you can manage stubs with the following API:

// Given the following stub file:
//
// namespace {{ NAMESPACE }};
//
// class {{ CLASS }}
// {
//     //
// }

use Binafy\LaravelStub\Facades\LaravelStub;

LaravelStub::from(__DIR__ . 'model.stub')
    ->to(__DIR__ . '/app')
    ->name('new-model')
    ->ext('php')
    ->replaces([
        'NAMESPACE' => 'App',
        'CLASS' => 'Example'
    ])
    ->generate();

Given the above code, the file will be created with the following contents using the model stub:

<?php

namespace App;

class Example
{
    //
}

Another interesting idea in this package is the download() method, which you can use to force a download in a controller if you want to provide stubs via your application:

LaravelStub::from(__DIR__ . 'model.stub')
    ->to(__DIR__ . '/App')
    ->name('new-model')
    ->ext('php')
    ->replaces([
        'NAMESPACE' => 'App',
        'CLASS' => 'Example'
    ])
    ->download(); 

This package could be an efficient way to provide stub files in your package or application that you want to allow other developers to override with their own customizations. To get started with this package—including installation and usage instructions—check it out on GitHub at binafy/laravel-stub.


The post A Package to Generate Custom Stubs in Laravel appeared first on Laravel News.

Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.