mwakalingajohn/laravel-easy-repository

为laravel设计的简单仓库模式,带有服务!

v1.0.2 2021-06-13 20:13 UTC

This package is auto-updated.

Last update: 2024-08-29 05:37:28 UTC


README

使用简单仓库,您可以拥有仓库模式的力量,而不必写太多代码。该包自动将接口绑定到实现,您只需更改配置即可知道当前使用的是哪个实现!

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

安装

您可以通过composer安装此包

composer require mwakalingajohn/laravel-easy-repository

快速使用

此包覆盖了默认的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",
];

测试

composer test

变更日志

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

贡献

有关详细信息,请参阅CONTRIBUTING

致谢

许可证

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