承接 binafy/laravel-stub 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

binafy/laravel-stub

Composer 安装命令:

composer require binafy/laravel-stub

包简介

Generate stub files easy

README 文档

README

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.

conditions

The conditions method allows you to define conditional blocks within your stub files. You can specify conditions that determine whether certain parts of the stub should be included or excluded based on provided values or closures.

LaravelStub::from(__DIR__ . 'model.stub')
    ->to(__DIR__ . '/App')
    ->name('new-model')
    ->ext('php')
    ->replaces([
        'NAMESPACE' => 'App',
        'CLASS' => 'Milwad'
    ])
    ->conditions([
        'hasController' => true,
        'hasController' => fn() => false, // or with closure
    ])
    ->generate();

NOTE: Ensure that your stub files contain the appropriate conditional placeholders (e.g., {{ if isEnabled }}) to utilize this method effectively.

Your stub code should looks like this:

<?php

namespace {{ NAMESPACE }};

class {{ CLASS }}
{
    {{ if hasController }}
    public function controller()
    {
        // Controller logic here
    }
    {{ endif }}
}

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
{
    
}

generateForce

If you want to generate a stub file and overwrite it if it exists, you can use the generateForce method:

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

generateIf

If you want to generate a stub file if given boolean expression evaluates to true, you can use the generateIf method:

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

generateUnless

If you want to generate a stub file if given boolean expression evaluates to false, you can use the generateIf method:

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

Contributors

Thanks to all the people who contributed. Contributors.

Security

If you discover any security-related issues, please email binafy23@gmail.com 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.

Donate

If this package is helpful for you, you can buy a coffee for me :) ❤️

binafy/laravel-stub 适用场景与选型建议

binafy/laravel-stub 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 177.98k 次下载、GitHub Stars 达 100, 最近一次更新时间为 2024 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「filesystem」 「stub」 「laravel」 「laravel package」 「laravel stub generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 binafy/laravel-stub 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 binafy/laravel-stub 我们能提供哪些服务?
定制开发 / 二次开发

基于 binafy/laravel-stub 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 177.98k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 100
  • 点击次数: 29
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 100
  • Watchers: 3
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-09