amin3520/anar

v1.1.9 2023-05-31 12:00 UTC

README

Latest Version on Packagist Total Downloads Build Status StyleCI

Anar 是一个用于创建 Laravel 应用程序仓库的 artisan 命令,简单快捷。查看 contributing.md 以了解待办事项列表。

如果你不了解仓库模式,请阅读 此链接

安装

通过 Composer

$ composer require amin3520/anar

变更日志

请参阅 变更日志 了解最近的变化。

命令

$ php artisan make:repository name  --m=model_name --imp 
  #sample php artisan make:repository UserRepository --m=User --imp 
  #sample2 php artisan make:repository UserRepository --m='\App\User' --imp 

name 是你的仓库名称,

--m选项是用于仓库中的模型名称,这是必须输入的,现在你也可以以字符串的形式传递你的地址模型,例如 '\App\User'

--imp 为你的仓库创建接口

第一次运行命令会创建基础文件和目录,你可以在下面看到它们

|--Providers
|       |--RepositoryServiceProvider.php
|
|--Repositories
|       |--BaseRepository.php
|       |--BaseRepositoryImp.php
|       |//and other your repoitorys

配置

如果你想在某些构造函数(如控制器)中注入你的仓库,请在 Providers/RepositoryServiceProvider.php 中的 $names 添加仓库名称,并在 config/app.php 中的 providers 添加 \App\Providers\RepositoryServiceProvider::class

  /**
     * Register RepositoryServiceProvider  .
     * provide your repository and inject it any where below your app directoy, like in to your controller's app if you want to use it
     * @return void
     */
    public function register()
    {
         $names = [
               //add Begin your repository name here   like -> 'UserRepository',
            ];

            foreach ($names as $name) {
                $this->app->bind(
                    "App\\Repositories\\{$name}",
                    "App\\Repositories\\{$name}");
            }


    }

用法

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    private $userRepo;

    /**
     * Controller constructor.
     *inject repo by service provider
     */
    public function __construct(UserRepositoryImp $repositoryImp)
    {
        $this->userRepo=$repositoryImp;
        //now u can use it
    }

    public function updateName(Request $request)
    {
        $this->userRepo->update(['name'=>'amin'],auth::id());
    }
}

基本方法

基本仓库有一些有用的方法你可以使用

interface BaseRepositoryImp
{
        public function create(array $attributes);
        public function update(array $attributes, int $id);
        public function all($columns = array('*'), string $orderBy = 'id', string $sortBy = 'desc');
        public function find(int $id);
        public function findOneOrFail(int $id);
        public function findBy(array $data);
        public function findOneBy(array $data);
        public function findOneByOrFail(array $data);
        public function paginateArrayResults(array $data, int $perPage = 50);
        public function delete(int $id);
        public function findWhere($where, $columns = ['*'], $or = false);
        public function with(array $relations);
}

methods

贡献

请参阅 contributing.md 了解详细信息及待办事项列表。

任务列表

  • 添加测试
  • 添加动态目录选项
  • 添加动态选择地址模型
  • 添加缓存选项

许可证

MIT 许可证。请参阅 许可证文件 了解更多信息。