tgalfa/reposervice

为Repository-Service模式生成仓库、服务和接口文件。

v1.2.5 2024-01-18 12:13 UTC

This package is auto-updated.

Last update: 2024-09-18 13:52:30 UTC


README

RepoService 生成器是一个Laravel包,用于生成Repository-Service模式的服务和接口文件。

  • 仓库位于您的应用程序的领域和数据层之间,以执行CRUD操作。
  • 服务应用您的应用程序的业务逻辑。它简单地使用提供的信息执行一组任务,使用任何服务之外创建的仓库或其他类。

安装

您可以通过composer安装此包

composer require tgalfa/reposervice --dev

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="reposervice-config"

您可以在配置文件中设置仓库和服务文件夹的路径和命名空间

return [
    ...
    'namespaces' => [
        ....
        'repositories' => 'App\Repositories',
        'repositoryContracts' => 'App\Repositories\Contracts',

        'services' => 'App\Services',
        'serviceContracts' => 'App\Services\Contracts',
        ....
    ],
    ....
    'paths' => [
        'repositories' => 'app/Repositories/',
        'repositoryContracts' => 'app/Repositories/Contracts/',

        'services' => 'app/Services/',
        'serviceContracts' => 'app/Services/Contracts/',
    ],
    ....
];

所有生成的服务和仓库都扩展了一个抽象类。如果您不想使用默认的AbstractMainService和AbstractMainRepository类,您可以创建自己的类并替换它们。更新您的reposervice配置

return [
    ...
    'namespaces' => [
        ....
        'main' => [
            'repository' => 'App\Repositories',
            'repositoryContract' => 'App\Repositories\Contracts',

            'service' => 'App\Services',
            'serviceContract' => 'App\Services\Contracts',
        ],
    ],
    ....
    'main' => [
        'repository' => 'MyAbstractMainRepository',
        'repositoryContract' => 'MyMainRepositoryInterface',

        'service' => 'MyAbstractMainService',
        'serviceContract' => 'MyMainServiceInterface',
    ],
];

使用

在运行生成命令之前,您应该为您的使用定制config/reposervice.php。您可以使用reposervice:generate并传递您的模型类作为参数

php artisan reposervice:generate Post

可用方法

以下方法可用

tgalfa\RepoService\Services\Contracts\MainServiceInterface
    public function getModel();

    public function get(array $columns = ['*'], array $scopes = []]);

    public function paginate(
        int $perPage,
        array $columns = ['*'],
        array $scopes = []
    );

    public function getById(int $id, array $columns = ['*']);

    public function store(array $data, array $scopes = []);

    public function update(Model $model, array $data, array $scopes = []);

    public function updateOrCreate(array $attributes, array $data, array $scopes = []);

    public function delete(Model $model);

示例用法

获取category_id = 5的活跃帖子

$post = $this->postService->get(
    ['id', 'title', 'description'],
    ['isActive', 'byCategory' => 5]
);

您需要在模型类中创建相关的作用域才能使用它们。示例

public function scopeIsActive(Builder $query)
{
    return $query->where('active', 1);
}
public function scopeByCategory(Builder $query, int $categoryId)
{
    return $query->where('category_id', $categoryId);
}

获取分页帖子

$post = $this->postService->paginate(
    8,
    ['id', 'title', 'description'],
);

在仓库中创建新的帖子

$post = $this->postService->store($request->all());

更新现有的帖子

$post = $this->postService->update($post, $request->all());

如果所有给定的属性匹配,则更新现有的帖子或创建一个新的帖子

$post = $this->postService->updateOrCreate(
    ['slug' => $request->slug],
    $request->all()
);

删除帖子

$post = $this->postService->delete($post);
tgalfa\RepoService\Services\Contracts\MainRepositoryInterface
    public function getModel();

    public function get(
        array $columns = ['*'],
        array $scopes = [],
        int $perPage = null
    );

    public function paginate(
        int $perPage,
        array $columns = ['*'],
        array $scopes = []
    );

    public function getById(int $id, array $columns = ['*']);

    public function store(array $data);

    public function update(Model $model, array $data);

    public function updateOrCreate(array $attributes, array $data);

    public function delete(Model $model);

测试

composer test

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

有关详细信息,请参阅CONTRIBUTING

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件