Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/binafy/laravel-stub
Generate stub files very easily in the Laravel framework
https://github.com/binafy/laravel-stub
binafy filesystem framework generate-stub laravel laravel-framework laravel-package laravel-stub laravel-stub-binafy laravel-stub-package laravel-stubs laravelstub milwad php stub stub-files
Last synced: 2 days ago
JSON representation
Generate stub files very easily in the Laravel framework
- Host: GitHub
- URL: https://github.com/binafy/laravel-stub
- Owner: binafy
- License: mit
- Created: 2024-03-02T17:18:14.000Z (11 months ago)
- Default Branch: 1.x
- Last Pushed: 2024-12-06T14:50:51.000Z (about 2 months ago)
- Last Synced: 2025-01-17T18:11:10.095Z (9 days ago)
- Topics: binafy, filesystem, framework, generate-stub, laravel, laravel-framework, laravel-package, laravel-stub, laravel-stub-binafy, laravel-stub-package, laravel-stubs, laravelstub, milwad, php, stub, stub-files
- Language: PHP
- Homepage: https://packagist.org/packages/binafy/laravel-stub
- Size: 89.8 KB
- Stars: 86
- Watchers: 4
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
## Laravel Stub
[![PHP Version Require](https://img.shields.io/packagist/dependency-v/binafy/laravel-stub/php.svg)](https://packagist.org/packages/binafy/laravel-stub)
[![Latest Stable Version](https://img.shields.io/packagist/v/binafy/laravel-stub.svg)](https://packagist.org/packages/binafy/laravel-stub)
[![Total Downloads](https://img.shields.io/packagist/dt/binafy/laravel-stub.svg)](https://packagist.org/packages/binafy/laravel-stub)
[![License](https://img.shields.io/packagist/l/binafy/laravel-stub.svg)](https://packagist.org/packages/binafy/laravel-stub)
[![Passed Tests](https://github.com/binafy/laravel-stub/actions/workflows/tests.yml/badge.svg)](https://github.com/binafy/laravel-stub/actions/workflows/tests.yml)- [Introduction](#introduction)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
- [Create a stub file](#create-a-stub-file)
- [How to use Laravel Stub](#how-using-laravel-stub)
- [`from`](#from)
- [`to`](#to)
- [`name`](#name)
- [`ext`](#ext)
- [`replace`](#replace)
- [`replaces`](#replaces)
- [`moveStub`](#move-stub)
- [`conditions`](#conditions)
- [`download`](#download)
- [`generate`](#generate)
- [`generateForce`](#generate-force)
- [`generateIf`](#generate-if)
- [`generateUnless`](#generate-unless)
- [Contributors](#contributors)
- [Security](#security)
- [Changelog](#changelog)
- [License](#license)
- [Donate](#donate)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.
***
- ```PHP >= 8.0```
- ```Laravel >= 9.0```| Version/Laravel | L9 | L10 | L11 |
|-----------------|--------------------|--------------------|--------------------|
| 1.0 | :white_check_mark: | :white_check_mark: | :white_check_mark: |You can install the package with Composer:
```bash
composer require binafy/laravel-stub
```You don't need to publish anything.
### Create a stub file
First of all, create a stub file called `model.stub`:```bash
touch model.stub
```Add some code to that, like this:
```php
### How to use Laravel Stub
You may use Laravel Stub, you need to use the `LaravelStub` facade:
```php
use Binafy\LaravelStub\Facades\LaravelStub;LaravelStub::class;
```First thing, you need to use the `from` method to give the stub path:
```php
LaravelStub::from(__DIR__ . 'model.stub');
```So, you need to determine the destination path of the stub file:
```php
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App');
```You can determine the stub file but also attention don't write the stub extension:
```php
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model');
```You can determine the stub extension:
```php
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php');
```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:
```php
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replace('NAMESPACE', 'App');
```The `replaces` method takes an array. If you want to replace multiple variables you can use this method:
```php
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
]);
```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:
```php
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.
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.```php
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
### `download`
If you want to download the stub file, you can use the `download` method:
```php
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
])
->download(); // Return download response
```The important method is `generate`. To generate the stub file at the end you need to use the `generate` method to generate stub file:
```php
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
### `generateForce`
If you want to generate a stub file and overwrite it if it exists, you can use the `generateForce` method:
```php
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
])
->generateForce();
```If you want to generate a stub file if given boolean expression evaluates to `true`, you can use the `generateIf` method:
```php
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
])
->generateIf(true);
```If you want to generate a stub file if given boolean expression evaluates to `false`, you can use the `generateIf` method:
```php
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
])
->generateUnless(true);
```Thanks to all the people who contributed. [Contributors](https://github.com/binafy/laravel-stub/graphs/contributors).
If you discover any security-related issues, please email `[email protected]` instead of using the issue tracker.
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.
The MIT License (MIT). Please see [License File](https://github.com/binafy/laravel-stub/blob/1.x/LICENSE) for more information.
## Donate
If this package is helpful for you, you can buy a coffee for me :) ❤️
- Iraninan Gateway: https://daramet.com/milwad_khosravi
- Paypal Gateway: SOON
- MetaMask Address: `0xf208a562c5a93DEf8450b656c3dbc1d0a53BDE58`