承接 thalles/repositories-commands 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thalles/repositories-commands

Composer 安装命令:

composer require thalles/repositories-commands

包简介

Artisan commands to create repositories

README 文档

README

Artisan commands to create repositories

This package provides an easy way to quickly setup and create a Repository using Unit Of Work Pattern.

  1. Requirements
  2. Installation
  3. Configuration
  4. Usage
  5. Default Repository Methods
  6. Unit Of Work methods

1. Requirements

  • PHP >= 7.1.3
  • laravel/framework ^5.8

2. Installation

Require the package using composer:

composer require thalles/repositories-commands

3. Configuration

Run the following command to create the main Repository and Interface for this repository.

php artisan repository:setup

Pay attention to the terminal output. You will need to copy the output and paste on register method of AppServiceProvider.

4. Usage

To create a new repository, use the following command.

php artisan repository:new RepositoryName ModelName

The fisrt argument is the repository name. The second is the model that you want to use. If your model was not on default path, you can inform the path to file.

Exemple:

php artisan repository:new UserRepository Models/User

Add the repository attribute to the UnitOfWork class.

<?php

namespace App\Repositories;

use App\Interfaces\UnitOfWorkInterface;
use Illuminate\Support\Facades\DB;

class UnitOfWork implements UnitOfWorkInterface
{
    /**
     * UserRepository instance
     *
     * @return void
     */
    private $UserRepository;

    /**
     * ProductRepository instance
     *
     * @return void
     */
    private $ProductRepository;

    ...
}

Use the UnitOfWorkInterface on your code implementation.

<?php

namespace App\Http\Controllers;

use App\Interfaces\UnitOfWorkInterface;

class UserController extends Controller
{
    /**
     * UnitOfWork instance
     *
     * @var App\Interfaces\UnitOfWorkInterface
     */
    private $uow;

    /**
     * Constructor method
     *
     * @param App\Interfaces\UnitOfWorkInterface $uow
     */
    public function __construct(UnitOfWorkInterface $uow)
    {
        $this->uow = $uow;
    }

    public function index()
    {
        try {
            $this->uow->beginTransaction();

            $user = $this->uow->UserRepository->add([
                'name' => 'User Name',
                'email' => 'email@email.com',
                'password' => bcrypt('secret')
            ]);

            if ($user) {
                $this->uow->commit();
                return $this->uow->UserRepository->all();
            }

            throw new Exception();
        } catch (Exception $e) {
            $this->uow->rollback();
            return back()->with('error', 'Internal Server Error.');
        }
    }
}

5. Default Repository Methods

This package makes available some methods to be used on CRUD.

  1. Return an item with defined id.
/**
 * @param integer $id
 * @return stdClass|null
 */
function getById(int $id) : ?stdClass
  1. Return all rows from database.
/**
 * @return Collection
 */
function all() : Collection;
  1. Add a new item to database.
/**
 * @param array $data
 * @return stdClass|null
 */
function add(array $data) : ?stdClass;
  1. Update an item in database.
/**
 * @param integer $id
 * @param array $data
 * @return stdClass|null
 */
function update(int $id, array $data) : ?stdClass;
  1. Delete an item from database.
/**
 * @param integer $id
 * @return boolean
 */
function delete(int $id) : bool;
  1. Return total of items in database.
/**
 * @return integer
 */
function count() : int;
  1. Format model data.
/**
 * @param Model $model
 * @return array
 */
function dataFormat(Model $model) : array;

If you need to create another methods for your repository, fist register the method on the Interface, after implementing the method on Repository.

  • Interface.
<?php

namespace App\Interfaces;

use Illuminate\Support\Collection;

interface UserRepositoryInterface extends RepositoryInterface
{
    /**
     * Make the search query
     *
     * @param string $search
     * @param int $pagination
     *
     * @return Collection
     */
    function search(string $search, int $id_user_auth) : Collection;
}
  • Repository.
<?php

namespace App\Repositories;

use App\Models\User;
use Illuminate\Support\Collection;
use App\Interfaces\UserRepositoryInterface;

class UserRepository extends Repository implements UserRepositoryInterface
{
    /**
     * Constructor method
     */
    public function __construct()
    {
        parent::__construct(new User());
    }

    /**
     * Make the search query
     *
     * @param string $search
     * @param int $pagination
     *
     * @return Collection
     */
    public function search(string $search, int $id_user_auth) : Collection
    {
        $users = $this->model
            ->where('id', '<>', $id_user_auth)
            ->where('name', 'LIKE', "%{$search}%")
            ->orWhere('email', 'LIKE', "%{$search}%")
            ->orderBy('name', 'ASC')
            ->get()
            ->toArray();

        $users = $this->arrayToStdClass($users);
        $users = new Collection($users);

        return $users;
    }
}

6. Unit Of Work methods

  1. Begin the database transaction.
/**
 * @return void
 */
function beginTransaction();
  1. Commit the transaction changes.
/**
 * @return void
 */
function commit();
  1. Rollback the transaction changes.
/**
 * @return void
 */
function rollback();

统计信息

  • 总下载量: 37
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固