touhidurabir/laravel-stub-generator

一个用于从模板文件生成类文件的 Laravel 扩展包。

1.0.8 2024-03-12 06:48 UTC

This package is auto-updated.

Last update: 2024-09-12 07:52:44 UTC


README

一个 PHP Laravel 扩展包,可以从给定的模板文件生成可用的 PHP 文件。

安装

使用 composer 安装此包

composer require touhidurabir/laravel-stub-generator

用法

最佳做法是通过外观使用此功能

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

默认情况下,它会生成带有 php 扩展名的文件,但也可以覆盖它

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

或者设置为无扩展名以生成类似 .env 的文件

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

默认情况下,它假设所有给定的 fromto 方法的路径都是相对路径,但也可以通过指定绝对路径来工作。

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

如果已保存的生成文件已存在,也可以通过指定 replace 方法用新生成的文件替换它。

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

需要注意的一个重要事项是,这个包不仅可以处理 string 类型的值,还可以处理 arrayboolean 类型的值。所以基本上它可以这样做

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

示例

考虑以下模板文件

<?php

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

}

如果模板文件存储在 app/stubs/repository.stub 的位置,并且我们想在 app/Repositories/ 路径下创建一个名为 UserRepository.php 的新仓库文件,那么

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

将生成 UserRepository.php 的文件内容

<?php

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

}

额外功能

有时我们有一个遵循 psr-4 标准的命名空间,而这个命名空间路径就是我们打算使用的路径。此包可以直接使用命名空间路径,并且包含一个实用的特质,可以在一定程度上帮助我们。

使用 特质

use Touhidurabir\StubGenerator\Concerns\NamespaceResolver;

class Someclass {

    use NamespaceResolver;
}

NamespaceResolver 的可用方法

resolveClassName(string $name)

根据定义

/**
 * 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)

从给定的完整类命名空间中提取类名。

resolveClassNamespace(string $name)

根据定义

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

只从给定的完整类命名空间中提取类命名空间。

generateFilePathFromNamespace(string $namespace = null)

根据定义

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

从给定的类完整命名空间中生成类的相对路径。

贡献

欢迎拉取请求。对于重大更改,请首先打开一个问题来讨论您想更改的内容。

请确保适当地更新测试。

许可

MIT