定制 williamjss/layers 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

williamjss/layers

最新稳定版本:v1.3.3

Composer 安装命令:

composer require williamjss/layers

包简介

A laravel package to generate files for layered architecture

README 文档

README

A laravel package to generate files for layered architecture and automate interface bindings.

Recommended Laravel version: ^9.0

Go to Laravel Docs to see support policy.

Summary

Requirements

"php": "^8.0.2"
"symfony/finder": "^6.3"
"illuminate/support": "^9.0 || ^10.20"
"illuminate/console": "^9.0 || ^10.20"

Installation

composer require williamjss/layers --dev

Configuration

php artisan vendor:publish --tag=layers

This command will copy Layers config to your project config folder

<?php

return [

    'namespace' => [
        'repositories' => 'Repositories',
        'services' => 'Services',
    ],

    'path' => [
        'models' => app_path('Models'),
        'repositories' => app_path('Repositories'),
        'services' => app_path('Services'),
    ]
];

Into this file, you can switch the services/repositories default path. For this, keep the namespace and path keys both equals.

Usage

Using the layers artisan command, we can be generate files for repositories (interface and eloquent) and services.

php artisan layers + {option} + {model name}

Available options:

  • -e or --eloquent : Generate a repository eloquent for the model
  • -i or --interface : Generate a repository interface for the model
  • -s or --service : Generate a service for the model
  • -r or --repository : Generate a repository interface and eloquent for the model
  • -a or --all : Generate a service, repository interface and repository eloquent for the model
  • --wr : Specify the service's repositories

Subcommands

  • php artisan layers:repository --eloquent : the same as php artisan layers --eloquent
  • php artisan layers:repository --interface : the same as php artisan layers --interface
  • php artisan layers:service : the same as php artisan layers --service
  • php artisan layers:binds : List all binds from application

Generate Layers

php artisan layers --all User

This command will generate 3 files:

  • app/Repositories/UserRepositoryInterface.php
  • app/Repositories/UserRepositoryEloquent.php
  • app/Services/UserService.php

Structure Folder

UserRepositoryInterface.php

<?php

namespace App\Repositories\User;

use App\Models\User;

interface UserRepositoryInterface
{
    public function __construct(User $user);

    /**
     * Stores a new instance of User in the database
     * @param \Illuminate\Support\Collection|array|int|string $data
     * @return User
     */
    public function store($data);

    /**
     * Returns all instances of User from the database
     * @param array|string $columns
     * @param array<array> $filters
     * @return \Illuminate\Database\Eloquent\Collection<int, static>
     */
    public function getList($columns=['*'], $filters=null);

    /**
     * Returns an instance of User from the given id
     * @param int|string $id
     * @return User
     */
    public function get($id);

    /**
     * Updates the data of an instance of User
     * @param \Illuminate\Support\Collection|array|int|string $data
     * @param int|string $id
     * @return User
     */
    public function update($data, $id);

    /**
     * Removes an instance of User from the database
     * @param int|string $id
     * @return int
     */
    public function destroy($id);
}

UserRepositoryEloquent.php

<?php

namespace App\Repositories;

use App\Models\User;

class UserRepositoryEloquent implements UserRepositoryInterface
{
    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Stores a new instance of User in the database
     * @param \Illuminate\Support\Collection|array|int|string $data
     * @return User
     */
    public function store($data)
    {
        return $this->user->create($data);
    }

    /**
     * Returns all instances of User from the database
     * @param array|string $columns
     * @param array<array> $filters
     * @return \Illuminate\Database\Eloquent\Collection<int, static>
     */
    public function getList($columns=['*'], $filters=null)
    {
        return $this->user->where($filters)->get($columns);
    }

    /**
     * Returns an instance of User from the given id
     * @param int|string $id
     * @return User
     */
    public function get($id)
    {
        return $this->user->find($id);
    }

    /**
     * Updates the data of an instance of User
     * @param \Illuminate\Support\Collection|array|int|string $data
     * @param int|string $id
     * @return User
     */
    public function update($data, $id)
    {
        $user = $this->user->find($id);
        $user->update($data);
        return $user;
    }

    /**
     * Removes an instance of User from the database
     * @param int|string $id
     * @return int
     */
    public function destroy($id)
    {
        return $this->user->find($id)->delete();
    }
}

UserService.php

<?php

namespace App\Services;

use App\Repositories\UserRepositoryInterface;

class UserService
{
    private $repoUser;

    public function __construct(
        UserRepositoryInterface $repoUser,
    )
    {
        $this->repoUser = $repoUser;
    }

    // Add your functions here...
}

Generate Layers with Subfolders

php artisan layers --repository User.Address

This command will generate 2 files:

  • app/Repositories/User/AddressRepositoryInterface.php
  • app/Repositories/User/AddressRepositoryEloquent.php

Structure Folder with Subfolders

Generate Services with more than one repository

php artisan layers --service --wr=Address --wr=User Person

This command will generate the follow file:

<?php

namespace App\Services;

use App\Repositories\UserRepositoryInterface;
use App\Repositories\User\AddressRepositoryInterface;

class PersonService
{
    private $repoAddress;
    private $repoUser;

    public function __construct(
        AddressRepositoryInterface $repoAddress,
        UserRepositoryInterface $repoUser,
    )
    {
        $this->repoAddress = $repoAddress;
        $this->repoUser = $repoUser;
    }

    // Add your functions here...
}

统计信息

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

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固