PHP code example of touhidurabir / laravel-stub-generator

1. Go to this page and download the library: Download touhidurabir/laravel-stub-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

touhidurabir / laravel-stub-generator example snippets


use Touhidurabir\StubGenerator\Facades\StubGenerator

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

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

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

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

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();

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();

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

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

StubGeneratorFacade::from('/app/stubs/repository.stub')
    ->to('/app/Repositories', true)
    ->as('UserRepository')
    ->withReplacers([
        'class'             => 'UserRepository',
        'model'             => 'User',
        'modelInstance'     => 'user',
        'modelNamespace'    => 'App\\Models',
        'baseClass'         => 'Touhidurabir\\ModelRepository\\BaseRepository',
        'baseClassName'     => 'BaseRepository',
        'classNamespace'    => 'App\\Repositories',
    ])
    ->save();



namespace App\Repositories;

use Touhidurabir\ModelRepository\BaseRepository;
use App\Models\User;

class UserRepository extends BaseRepository {

	/**
     * Constructor to bind model to repo
     *
     * @param  object<App\Models\User> $user
     * @return void
     */
    public function __construct(User $user) {

        $this->model = $user;

        $this->modelClass = get_class($user);
    }

}



use Touhidurabir\StubGenerator\Concerns\NamespaceResolver;

class Someclass {

    use NamespaceResolver;
}

/**
 * Resolve the class name and class store path from give class namespace
 * In case a full class namespace provides, need to extract class name
 *
 * @param  string $name
 * @return string
 */
public function resolveClassName(string $name)

/**
 * Resolve the class namespace from given class name
 *
 * @param  string $name
 * @return mixed<string|null>
 */
public function resolveClassNamespace(string $name)

/**
 * Generate class store path from give namespace
 *
 * @param  mixed<string|null> $namespace
 * @return mixed<string|null>
 */
public function generateFilePathFromNamespace(string $namespace = null)
stub


namespace {{classNamespace}};

use {{baseClass}};
use {{modelNamespace}}\{{model}};

class {{class}} extends {{baseClassName}} {

	/**
     * Constructor to bind model to repo
     *
     * @param  object<{{modelNamespace}}\{{model}}> ${{modelInstance}}
     * @return void
     */
    public function __construct({{model}} ${{modelInstance}}) {

        $this->model = ${{modelInstance}};

        $this->modelClass = get_class(${{modelInstance}});
    }

}