/Anar

laravel package : artisan commands for create Repository and provider

Primary LanguagePHPMIT LicenseMIT

Anar

Latest Version on Packagist Total Downloads Build Status StyleCI

Anar is artisan command to create repository for your amazing laravel app easy peasy . Take look at contributing.md to see a to do list.

if you don't know about Repository pattern read this link

Installation

Via Composer

$ composer require amin3520/anar

Change log

Please see the changelog for more information on what has changed recently.

command

$ 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 is your name Repository ,

--moption is model name that use in repo and it is necessary input , now u can also pass your address model in string like '\App\User'

--imp create interface for your repo

first run of command create base files and directory ,you can see them below

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

Configuration

if you want inject your repositories in some constructor like controllers ,add repo name in $names in Providers/RepositoryServiceProvider.php and add \App\Providers\RepositoryServiceProvider::class in providers in config\app.php

  /**
     * 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}");
            }


    }

Usage

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());
    }
}

BaseMethods

Base repository has some useful method you can use theme

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

Please see contributing.md for details and a todolist.

Task list:

  • add Test
  • add dynamic directory option
  • add dynamically pickUp address's model
  • add cache option

License

MIT License. Please see the license file for more information.