GithubHelp home page GithubHelp logo

binafy / laravel-stub Goto Github PK

View Code? Open in Web Editor NEW
65.0 4.0 2.0 56 KB

Generate stub files very easy in Laravel framework

Home Page: https://packagist.org/packages/binafy/laravel-stub

License: MIT License

PHP 100.00%
binafy filesystem laravel php stub laravel-stub milwad framework laravel-framework laravel-package

laravel-stub's Introduction

Laravel Stub

laravel-stub-banner

PHP Version Require Latest Stable Version Total Downloads License Passed Tests

Introduction

The Laravel-Stub package enhances the development workflow in Laravel by providing a set of customizable stubs. Stubs are templates used to scaffold code snippets for various components like models, controllers, and migrations. With Laravel-Stub, developers can easily tailor these stubs to match their project's coding standards and conventions. This package aims to streamline the code generation process, fostering consistency and efficiency in Laravel projects. Explore the customization options and boost your development speed with Laravel-Stub.

Requirements


  • PHP >= 8.0
  • Laravel >= 9.0
Version/Laravel L9 L10 L11
1.0

Installation

You can install the package with Composer:

composer require binafy/laravel-stub

You don't need to publish anything.

Usage

Create a stub file

First of all, create a stub file called model.stub:

touch model.stub

Add some code to that, like this:

<?php

namespace {{ NAMESPACE }};

class {{ CLASS }}
{
    
}

How to use Laravel Stub

You may use Laravel Stub, you need to use the LaravelStub facade:

use Binafy\LaravelStub\Facades\LaravelStub;

LaravelStub::class;

from

First thing, you need to use the from method to give the stub path:

LaravelStub::from(__DIR__ . 'model.stub');

to

So, you need to determine the destination path of the stub file:

LaravelStub::from(__DIR__ . 'model.stub')
    ->to(__DIR__ . '/App');

name

You can determine the stub file but also attention don't write the stub extension:

LaravelStub::from(__DIR__ . 'model.stub')
    ->to(__DIR__ . '/App')
    ->name('new-model');

ext

You can determine the stub extension:

LaravelStub::from(__DIR__ . 'model.stub')
    ->to(__DIR__ . '/App')
    ->name('new-model')
    ->ext('php');

replace

The replace method takes two parameters, the first one is the key (variable) and the second one is the value. The value will be replaced with the variable:

LaravelStub::from(__DIR__ . 'model.stub')
    ->to(__DIR__ . '/App')
    ->name('new-model')
    ->ext('php')
    ->replace('NAMESPACE', 'App');

replaces

The replaces method takes an array. If you want to replace multiple variables you can use this method:

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

moveStub

By default, Laravel Stub creates a copy from your stub file and moves it to the destination path. If you want to move the current stub file, you can use the moveStub method:

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

After running this code, the model.stub didn't exist.

download

If you want to download the stub file, you can use the download method:

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

generate

The important method is generate. To generate the stub file at the end you need to use the generate method to generate stub file:

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

NOTE: Don't use the download and generate methods in one chain.

The final file will be like this (new-model.php):

<?php

namespace App;

class Milwad
{
    
}

Contributors

Thanks to all the people who contributed. Contributors.

Security

If you discover any security-related issues, please email [email protected] instead of using the issue tracker.

Changelog

The changelog can be found in the CHANGELOG.md file of the GitHub repository. It lists the changes, bug fixes, and improvements made to each version of the Laravel User Monitoring package.

License

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

laravel-stub's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar milwad-dev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

laravel-stub's Issues

i need to keep my stub file

File::move($this->from, $path);

from this line of code im using local stub file for example my-projects/stubs/SampleController.stubs, when generate the original stubs file is moved , consider you to changes this line of code to be

        // Copy file
        File::copy($this->from, $path);

so it can keep my original stub to more reuseable

Tab support

Good afternoon,

Thank you for your efforts in creating this package.

I have question for you. Do you have any plans for new line and tab prefix support (\n\t)?

I have personal stub service and it's adds new lines and tabs:

implode("\n    " . str_repeat('    ', 4), $field);

I wondering if you will have some support for this? eg:

LaravelStub::from(__DIR__ . 'model.stub')
    ->to(__DIR__ . '/App')
    ->name('new-model')
    ->newLinePrefix()
    ->replace('NAMESPACE', [
        'use Blueprint\Models\Column;',
        'use Blueprint\Models\Random;',
    ]);
LaravelStub::from(__DIR__ . 'model.stub')
    ->to(__DIR__ . '/App')
    ->name('new-model')
    ->tabPrefix()
    ->replace('FIELDS', [
        "TextColumn::make('title'),",
        "TextColumn::make('content'),",
    ]);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.