PHPackages                             touhidurabir/laravel-stub-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. touhidurabir/laravel-stub-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

touhidurabir/laravel-stub-generator
===================================

A laravel package to generate class files from stub files.

1.0.9(1y ago)54167.1k↓29.4%107MITPHPPHP &gt;=7.2.0

Since Sep 7Pushed 1y ago3 watchersCompare

[ Source](https://github.com/touhidurabir/laravel-stub-generator)[ Packagist](https://packagist.org/packages/touhidurabir/laravel-stub-generator)[ RSS](/packages/touhidurabir-laravel-stub-generator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (11)Used By (7)

Laravel Stub Generator
======================

[](#laravel-stub-generator)

A php laravel package to generate useable php files from given stub files .

Installation
------------

[](#installation)

Require the package using composer:

```
composer require touhidurabir/laravel-stub-generator
```

Usage
-----

[](#usage)

The best approach to use this via the facade as

```
use Touhidurabir\StubGenerator\Facades\StubGenerator
```

and then implement as follow

```
StubGenerator::from('some/path/to/stub/file.stub') // the stub file path
    ->to('some/path/to/store/generated/file') // the store directory path
    ->as('TheGeneratingFileNameItself') // the generatable file name without extension
    ->ext('php') // the file extension(optional, by default to php)
    // ->noExt() // to remove the extension from the file name for the generated file like .env
    ->withReplacers([]) // the stub replacing params
    ->save(); // save the file
```

By default it generate file with **php** extension but possible to override it as

```
StubGenerator::from('some/path/to/stub/file.stub') // the stub file path
    ...
    ->ext('ANY_FILE_EXTENSION')
    ...
    ->save(); // save the file
```

Or set to no extension to generate **.env** like files as

```
StubGenerator::from('some/path/to/stub/file.stub') // the stub file path
    ...
    ->noExt() // to remove the extension from the file name for the generated file like .env
    ...
    ->save(); // save the file
```

Also possible to get the generated content as string or download the generated file

```
StubGenerator::from('path')->to('path')->as('name')->withReplacers([])->toString(); // get generated content
StubGenerator::from('path')->as('name')->withReplacers([])->download(); // download the file
```

By default it assume all the given path for **from** and **to** methods are relative path, but it can also work with absolute path by specifying that.

```
StubGenerator::from('some/path/to/stub/file.stub', true) // the second argument **true** specify absolute path
    ->to('some/path/to/store/generated/file', false, true) // the third argument **true** specify absolute path
    ->as('TheGeneratingFileNameItself')
    ->withReplacers([])
    ->save();
```

Also if the store directory path does not exists, it can create that target store path if that is specified in the method call.

```
StubGenerator::from('some/path/to/stub/file.stub', true)
    ->to('some/path/to/store/generated/file', true, true) // the second argument **true** specify to generated path if not exists
    ->as('TheGeneratingFileNameItself')
    ->withReplacers([])
    ->save();
```

If the saved generated file aleady exists, it is also possible to replace that with the newly generated one if that is specified via the **replace** method.

```
StubGenerator::from('some/path/to/stub/file.stub') // the stub file path
    ->to('some/path/to/store/generated/file') // the store directory path
    ->as('TheGeneratingFileNameItself') // the generatable file name without extension
    ->withReplacers([]) // the stub replacing params
    ->replace(true) // instruct to replace if already exists at the give path
    ->save(); // save the file
```

One important thing to note that this package can handle not just **string** type values to but also hanlde **array** and **boolean** type also. So basically it can do as :

```
->withReplacers([
    'replacer_1' = 'some value',
    'replacer_2' = ['some', 'more', 'values'],
    'replacer_3' = true,
])
```

Example
-------

[](#example)

Considering the following stub file

```
