rafi021/repository-pattern

用于在应用程序中应用仓库模式的laravel包

v1.0.0 2023-07-19 08:39 UTC

This package is auto-updated.

Last update: 2024-09-19 12:07:22 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

这里应该放置您的描述。请限制为一两段话。考虑添加一个小示例。

安装

您可以通过composer安装此包

composer require rafi021/repository-pattern

您可以使用以下命令发布和运行迁移

php artisan vendor:publish --tag="repository-pattern-migrations"
php artisan migrate

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

php artisan vendor:publish --tag="repository-pattern-config"

这是已发布配置文件的内容

return [
];

可选地,您可以使用以下命令发布视图

php artisan vendor:publish --tag="repository-pattern-views"

快速使用

此包覆盖了默认的laravel php artisan make:model User 命令,并添加了一些可以帮助您快速设置仓库和服务的标志。

// will genearate controller, factory, service, seeder, repository, resource and migration
php artisan make:model User --all

// use the service and repository flag to generate the class
php artisan make:model User --service --repository

// use the short form to generate model with service and repository
php artisan make:model User -sr -rt

您也可以仅创建仓库、服务或两者都创建

php artisan make:repository User
// or
php artisan make:repository UserRepository

// or create together with a service
php artisan make:repository User --service
// or
php artisan make:repository UserRepository --service

// or create a service separately
php artisan make:service User
// or
php artisan make:service UserService

php artisan make:repository User 将生成两个文件。一个是接口,另一个是仓库类。接口会根据当前使用的实现自动绑定到相应的类。如果没有提供接口的实现,您可以手动提供,否则尝试使用服务将引发错误。

Eloquent是默认实现。将来将添加其他实现。这是因为此包主要是为了简化laravel中仓库模式的使用。创建的类包括

// app/Repositories/Interfaces/UserRepository.php

<?php

namespace App\Repositories\Interfaces;

use Mwakalingajohn\LaravelEasyRepository\Repository;

class UserRepositoryInterface extends Repository{

    // Write something awesome :)
}

和,

// app/Repositories/Eloquent/UserRepository.php

<?php

namespace App\Repositories\Eloquent;

use Mwakalingajohn\LaravelEasyRepository\Repository;
use Mwakalingajohn\LaravelEasyRepository\Implementations\Eloquent;
use App\Repositories\Interfaces\UserRepositoryInterface;

class UserRepository extends Eloquent implements UserRepositoryInterface{

    /**
    * Model class to be used in this repository for the common methods inside Eloquent
    * @property Model|mixed $model;
    */
    protected $model = Model::class;

    // Write something awesome :)
}

如果您包含了服务标志或通过运行命令创建了服务,生成的服务文件是

// app/Services/UserService

<?php

namespace App\Services;

use Mwakalingajohn\LaravelEasyRepository\Service;

class UserService extends Service{

    /**
    * The repository interface to use in this service. Will allow to use within
    * methods $this->repository. It will be resolved from the container
    * @property string $repositoryInterface;
    */
    protected string $repositoryInterface = "App\Repositories\Interfaces\UserRepositoryInterface";

    // Define your custom methods :)
}

仓库接口属性告诉服务从容器中获取哪个仓库。获取到的仓库将可通过$this->repository变量使用。

用法

在您的控制器中,您可以使用服务或仓库。

<?php

namespace App\Http\Controllers;

use App\Services\UserService;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
        $userService = new UserService;
        return $userService->all();
    }
}

// or using the repository, the service container will automatically resolve the repository for you

<?php

namespace App\Http\Controllers;

use App\Repositories\Interfaces\UserRepositoryInterface;
use App\Services\UserService;
use Illuminate\Http\Request;

class UserController extends Controller
{

    public $repository;

    public function __construct(UserRepositoryInterface $userRepositoryInterface)
    {
        $this->repository = $userRepositoryInterface;
    }

    public function index()
    {
        return $this->repository->all();
    }
}

仓库和服务还内置了5个常见的CRUD方法

interface Repository
{
    /**
     * Fin an item by id
     * @param int $id
     * @return Model|null
     */
    public function find(int $id);

    /**
     * Return all items
     * @return Collection|null
     */
    public function all();

    /**
     * Return query builder instance to perform more manouvers
     * @return Builder|null
     */
    public function query();

    /**
     * Create an item
     * @param array|mixed $data
     * @return Model|null
     */
    public function create($data);

    /**
     * Update a model
     * @param int|mixed $id
     * @param array|mixed $data
     * @return bool|mixed
     */
    public function update($id, array $data);

    /**
     * Delete a model
     * @param int|Model $id
     */
    public function delete($id);
}

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

php artisan vendor:publish --provider="Mwakalingajohn\LaravelEasyRepository\LaravelEasyRepositoryServiceProvider" --tag="easy-repository-config"

配置文件中的配置是标准的,可以根据进一步的要求进行扩展。除非您非常清楚自己在做什么,否则无需更改任何内容 :) 这是已发布配置文件的内容

return [
    /**
     * The directory for all the repositories
     */
    "repository_directory" => "app/Repositories",

    /**
     * Default repository namespace
     */
    "repository_namespace" => "App\Repositories",

    /**
     * The directory for all the services
     */
    "service_directory" => "app/Services",

    /**
     * Default service namespace
     */
    "service_namespace" => "App\Services",

    /**
     * Default repository implementation
     */
    "default_repository_implementation" => "Eloquent",

    /**
     * Current repository implementation
     */
    "current_repository_implementation" => "Eloquent",
];

## Testing

```bash
composer test

变更日志

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

贡献

有关详细信息,请参阅CONTRIBUTING

安全漏洞

有关如何报告安全漏洞,请参阅我们的安全策略

鸣谢

许可证

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