├── .gitignore ├── .styleci.yml ├── config └── anar.php ├── src ├── Anar.php ├── Commands │ ├── stubs │ │ ├── repositoryImp.stub │ │ ├── repository.stub │ │ ├── BaseRepositoryImp.stub │ │ ├── repositoryProvider.stub │ │ └── BaseRepository.stub │ ├── MakeBaseRepository.php │ ├── MakeRepositoryImpCommand.php │ ├── MakeBaseRepositoryImp.php │ ├── MakeRepositoryProvider.php │ └── MakeRepositoryCommand.php ├── Facades │ └── Anar.php └── AnarServiceProvider.php ├── .travis.yml ├── changelog.md ├── tests └── Tests.php ├── phpunit.xml ├── license.md ├── composer.json ├── contributing.md └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /.idea -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel -------------------------------------------------------------------------------- /config/anar.php: -------------------------------------------------------------------------------- 1 | assertTrue(true); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Commands/stubs/repository.stub: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | 18 | 19 | src/ 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/Commands/stubs/BaseRepositoryImp.stub: -------------------------------------------------------------------------------- 1 | 'UserRepository', 29 | ]; 30 | 31 | foreach ($names as $name) { 32 | $this->app->bind( 33 | "App\\Repositories\\{$name}Imp", 34 | "App\\Repositories\\{$name}"); 35 | } 36 | 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /license.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 amin3536 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Commands/MakeBaseRepository.php: -------------------------------------------------------------------------------- 1 | loadTranslationsFrom(__DIR__.'/../resources/lang', 'amin3520'); 25 | // $this->loadViewsFrom(__DIR__.'/../resources/views', 'amin3520'); 26 | // $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); 27 | // $this->loadRoutesFrom(__DIR__.'/routes.php'); 28 | 29 | // Publishing is only necessary when using the CLI. 30 | if ($this->app->runningInConsole()) { 31 | $this->bootForConsole(); 32 | } 33 | } 34 | 35 | /** 36 | * Register any package services. 37 | * 38 | * @return void 39 | */ 40 | public function register() 41 | { 42 | $this->commands($this->commands); 43 | $this->mergeConfigFrom(__DIR__.'/../config/anar.php', 'anar'); 44 | 45 | // Register the service the package provides. 46 | $this->app->singleton('anar', function ($app) { 47 | return new Anar; 48 | }); 49 | } 50 | 51 | /** 52 | * Get the services provided by the provider. 53 | * 54 | * @return array 55 | */ 56 | public function provides() 57 | { 58 | return ['anar']; 59 | } 60 | 61 | /** 62 | * Console-specific booting. 63 | * 64 | * @return void 65 | */ 66 | protected function bootForConsole() 67 | { 68 | // Publishing the configuration file. 69 | $this->publishes([ 70 | __DIR__.'/../config/anar.php' => config_path('anar.php'), 71 | ], 'anar.config'); 72 | 73 | // Publishing the views. 74 | /*$this->publishes([ 75 | __DIR__.'/../resources/views' => base_path('resources/views/vendor/amin3520'), 76 | ], 'anar.views');*/ 77 | 78 | // Publishing assets. 79 | /*$this->publishes([ 80 | __DIR__.'/../resources/assets' => public_path('vendor/amin3520'), 81 | ], 'anar.views');*/ 82 | 83 | // Publishing the translation files. 84 | /*$this->publishes([ 85 | __DIR__.'/../resources/lang' => resource_path('lang/vendor/amin3520'), 86 | ], 'anar.views');*/ 87 | 88 | // Registering package commands. 89 | // $this->commands([]); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/Commands/MakeRepositoryCommand.php: -------------------------------------------------------------------------------- 1 | option('m')) { 66 | throw new InvalidArgumentException("Model name is required.\n make:repository RepositoryClassName --m=ModelClass --imp"); 67 | } 68 | 69 | if (strstr($this->option('m'), '\\')) { 70 | $stub = str_replace( 71 | ['DummyModel'], 72 | [$this->option('m')], 73 | $stub 74 | ); 75 | } else { 76 | $stub = str_replace( 77 | ['DummyModel'], 78 | ['\\App\\'.'Models\\'.$this->option('m')], 79 | $stub 80 | ); 81 | } 82 | 83 | return parent::replaceNamespace($stub, $name); // 84 | } 85 | 86 | protected function getNamespace($name) 87 | { 88 | return parent::getNamespace($name); 89 | } 90 | 91 | protected function buildClass($name) 92 | { 93 | return parent::buildClass($name); 94 | } 95 | 96 | public function handle() 97 | { 98 | $this->modelName = $this->option('m'); 99 | 100 | if ($this->option('imp')) { 101 | $this->createImp(); 102 | } 103 | if ($this->modelName && ! class_exists($this->modelName)) { 104 | $this->createModel(); 105 | } 106 | 107 | if (! file_exists(app_path().DIRECTORY_SEPARATOR.'Repositories'.DIRECTORY_SEPARATOR.'BaseRepository.php')) { 108 | $this->createBaseRepo(); 109 | } 110 | if (! file_exists(app_path().DIRECTORY_SEPARATOR.'Repositories'.DIRECTORY_SEPARATOR.'BaseRepositoryImp.php')) { 111 | $this->createBaseRepoImp(); 112 | } 113 | if (! file_exists(app_path().DIRECTORY_SEPARATOR.'Providers'.DIRECTORY_SEPARATOR.'RepositoryServiceProvider.php')) { 114 | $this->createRepoServiceProvider(); 115 | } 116 | 117 | return parent::handle(); 118 | } 119 | 120 | /** 121 | * Create a RepositoryImp for the model. 122 | * 123 | * @return void 124 | */ 125 | protected function createImp() 126 | { 127 | $this->call('make:RepositoryImp', [ 128 | 'name' => $this->argument('name').'Imp', 129 | ]); 130 | } 131 | 132 | /** 133 | * Create BaseRepository. 134 | * 135 | * @return void 136 | */ 137 | protected function createBaseRepo() 138 | { 139 | $this->call('make:baseRepository', [ 140 | 'name' => 'BaseRepository', 141 | ]); 142 | } 143 | 144 | protected function createModel() 145 | { 146 | if ($this->confirm("A {$this->modelName} model does not exist. Do you want to generate it?", true)) { 147 | $this->call('make:model', ['name' => $this->modelName]); 148 | } 149 | } 150 | 151 | /** 152 | * Create BaseRepositoryImp. 153 | * 154 | * @return void 155 | */ 156 | public function createBaseRepoImp() 157 | { 158 | $this->call('make:baseRepositoryImp', [ 159 | 'name' => 'BaseRepositoryImp', 160 | ]); 161 | } 162 | 163 | /** 164 | * Create Service provider. 165 | * 166 | * @return void 167 | */ 168 | public function createRepoServiceProvider() 169 | { 170 | $this->call('make:repositoryServiceProvider', [ 171 | 'name' => 'RepositoryServiceProvider', 172 | ]); 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /src/Commands/stubs/BaseRepository.stub: -------------------------------------------------------------------------------- 1 | model = $model; 20 | } 21 | /** 22 | * @param array $attributes 23 | * @return mixed 24 | */ 25 | public function create(array $attributes) 26 | { 27 | return $this->model->create($attributes); 28 | } 29 | /** 30 | * @param array $attributes 31 | * @param int $id 32 | * @return bool 33 | */ 34 | public function update(array $attributes, int $id) : bool 35 | { 36 | return $this->find($id)->update($attributes); 37 | } 38 | /** 39 | * @param array $columns 40 | * @param string $orderBy 41 | * @param string $sortBy 42 | * @return mixed 43 | */ 44 | public function all($columns = array('*'), string $orderBy = 'id', string $sortBy = 'asc') 45 | { 46 | return $this->model->orderBy($orderBy, $sortBy)->get($columns); 47 | } 48 | /** 49 | * @param int $id 50 | * @return mixed 51 | */ 52 | public function find(int $id) 53 | { 54 | return $this->model->find($id); 55 | } 56 | /** 57 | * @param int $id 58 | * @return mixed 59 | * @throws ModelNotFoundException 60 | */ 61 | public function findOneOrFail(int $id) 62 | { 63 | return $this->model->findOrFail($id); 64 | } 65 | /** 66 | * @param array $data 67 | * @return mixed 68 | */ 69 | public function findBy(array $data) 70 | { 71 | return $this->model->where($data)->get(); 72 | } 73 | /** 74 | * @param array $data 75 | * @return mixed 76 | */ 77 | public function findOneBy(array $data) 78 | { 79 | return $this->model->where($data)->first(); 80 | } 81 | /** 82 | * @param array $data 83 | * @return mixed 84 | * @throws ModelNotFoundException 85 | */ 86 | public function findOneByOrFail(array $data) 87 | { 88 | return $this->model->where($data)->firstOrFail(); 89 | } 90 | /** 91 | * @param array $data 92 | * @param int $perPage 93 | * @return LengthAwarePaginator 94 | */ 95 | public function paginateArrayResults(array $data, int $perPage = 20) 96 | { 97 | $page = request()->get('page', 1); 98 | $offset = ($page * $perPage) - $perPage; 99 | return new LengthAwarePaginator( 100 | array_slice($data, $offset, $perPage, false), 101 | count($data), 102 | $perPage, 103 | $page, 104 | [ 105 | 'path' => request()->url(), 106 | 'query' => request()->query() 107 | ] 108 | ); 109 | } 110 | /** 111 | * @param int $id 112 | * @return bool 113 | */ 114 | public function delete(int $id) : bool 115 | { 116 | return $this->model->find($id)->delete(); 117 | } 118 | 119 | 120 | /** 121 | * Find a collection of models by the given query conditions. 122 | * 123 | * @param array $where 124 | * @param array $columns 125 | * @param bool $or 126 | * 127 | * @return \Illuminate\Database\Eloquent\Collection|null 128 | */ 129 | public function findWhere($where, $columns = ['*'], $or = false) 130 | { 131 | $model = $this->model; 132 | foreach ($where as $field => $value) { 133 | if ($value instanceof \Closure) { 134 | $model = (!$or) 135 | ? $model->where($value) 136 | : $model->orWhere($value); 137 | } elseif (is_array($value)) { 138 | if (count($value) === 3) { 139 | list($field, $operator, $search) = $value; 140 | $model = (!$or) 141 | ? $model->where($field, $operator, $search) 142 | : $model->orWhere($field, $operator, $search); 143 | } elseif (count($value) === 2) { 144 | list($field, $search) = $value; 145 | $model = (!$or) 146 | ? $model->where($field, '=', $search) 147 | : $model->orWhere($field, '=', $search); 148 | } 149 | } else { 150 | $model = (!$or) 151 | ? $model->where($field, '=', $value) 152 | : $model->orWhere($field, '=', $value); 153 | } 154 | } 155 | return $model->get($columns); 156 | } 157 | 158 | /** 159 | * @param array $relations 160 | * @return $this 161 | */ 162 | public function with(array $relations) 163 | { 164 | $this->model = $this->model->with($relations); 165 | return $this; 166 | } 167 | 168 | } 169 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Anar 2 | 3 | [![Latest Version on Packagist][ico-version]][link-packagist] 4 | [![Total Downloads][ico-downloads]][link-downloads] 5 | [![Build Status](https://travis-ci.org/amin3536/Anar.svg?branch=master)](https://travis-ci.org/amin3536/Anar) 6 | [![StyleCI](https://github.styleci.io/repos/163210230/shield?branch=master)](https://github.styleci.io/repos/163210230) 7 | 8 | 9 | Anar is artisan command to create repository for your amazing laravel app easy peasy . Take look at [contributing.md](contributing.md) to see a to do list. 10 | 11 | if you don't know about Repository pattern read [this link](https://medium.com/employbl/use-the-repository-design-pattern-in-a-laravel-application-13f0b46a3dce) 12 | 13 | ## Installation 14 | 15 | Via Composer 16 | 17 | ``` bash 18 | $ composer require amin3520/anar 19 | ``` 20 | 21 | 22 | ## Change log 23 | 24 | Please see the [changelog](changelog.md) for more information on what has changed recently. 25 | 26 | ## command 27 | 28 | ``` bash 29 | $ php artisan make:repository name --m=model_name --imp 30 | #sample php artisan make:repository UserRepository --m=User --imp 31 | #sample2 php artisan make:repository UserRepository --m='\App\User' --imp 32 | 33 | ``` 34 | ``` name ``` is your name Repository , 35 | 36 | ``` --m ```option 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' 37 | 38 | ``` --imp ``` create interface for your repo 39 | 40 | 41 | first run of command create base files and directory ,you can see them below 42 | 43 | ``` bash 44 | 45 | |--Providers 46 | | |--RepositoryServiceProvider.php 47 | | 48 | |--Repositories 49 | | |--BaseRepository.php 50 | | |--BaseRepositoryImp.php 51 | | |//and other your repoitorys 52 | ``` 53 | 54 | 55 | ### _Configuration_ 56 | 57 | if you want inject your repositories in some constructor like controllers ,add repo name 58 | in ```$names``` in ```Providers/RepositoryServiceProvider.php``` 59 | and add ``\App\Providers\RepositoryServiceProvider::class`` in ``providers`` in ``config\app.php`` 60 | 61 | ```php 62 | /** 63 | * Register RepositoryServiceProvider . 64 | * 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 65 | * @return void 66 | */ 67 | public function register() 68 | { 69 | $names = [ 70 | //add Begin your repository name here like -> 'UserRepository', 71 | ]; 72 | 73 | foreach ($names as $name) { 74 | $this->app->bind( 75 | "App\\Repositories\\{$name}", 76 | "App\\Repositories\\{$name}"); 77 | } 78 | 79 | 80 | } 81 | ``` 82 | 83 | ### _Usage_ 84 | 85 | ```php 86 | class Controller extends BaseController 87 | { 88 | use AuthorizesRequests, DispatchesJobs, ValidatesRequests; 89 | private $userRepo; 90 | 91 | /** 92 | * Controller constructor. 93 | *inject repo by service provider 94 | */ 95 | public function __construct(UserRepositoryImp $repositoryImp) 96 | { 97 | $this->userRepo=$repositoryImp; 98 | //now u can use it 99 | } 100 | 101 | public function updateName(Request $request) 102 | { 103 | $this->userRepo->update(['name'=>'amin'],auth::id()); 104 | } 105 | } 106 | ``` 107 | 108 | 109 | ## _BaseMethods_ 110 | 111 | Base repository has some useful method you can use theme 112 | ```php 113 | interface BaseRepositoryImp 114 | { 115 | public function create(array $attributes); 116 | public function update(array $attributes, int $id); 117 | public function all($columns = array('*'), string $orderBy = 'id', string $sortBy = 'desc'); 118 | public function find(int $id); 119 | public function findOneOrFail(int $id); 120 | public function findBy(array $data); 121 | public function findOneBy(array $data); 122 | public function findOneByOrFail(array $data); 123 | public function paginateArrayResults(array $data, int $perPage = 50); 124 | public function delete(int $id); 125 | public function findWhere($where, $columns = ['*'], $or = false); 126 | public function with(array $relations); 127 | } 128 | ``` 129 | ![methods][methods] 130 | 131 | 132 | ## Contributing 133 | 134 | Please see [contributing.md](contributing.md) for details and a todolist. 135 | 136 | 137 | ## Task list: 138 | 139 | - [ ] add Test 140 | - [ ] add dynamic directory option 141 | - [x] add dynamically pickUp address's model 142 | - [ ] add cache option 143 | 144 | 145 | 146 | 147 | ## License 148 | 149 | MIT License. Please see the [license file](license.md) for more information. 150 | 151 | [ico-version]: https://img.shields.io/packagist/v/amin3520/anar.svg?style=flat-square 152 | [ico-downloads]: https://img.shields.io/packagist/dt/amin3520/anar.svg?style=flat-square 153 | [ico-travis]: https://img.shields.io/travis/amin3520/anar/master.svg?style=flat-square 154 | [ico-styleci]: https://styleci.io/repos/12345678/shield 155 | 156 | [link-packagist]: https://packagist.org/packages/amin3520/anar 157 | [link-downloads]: https://packagist.org/packages/amin3520/anar 158 | [link-travis]: https://travis-ci.org/amin3520/anar 159 | [link-styleci]: https://styleci.io/repos/12345678 160 | [link-author]: https://github.com/amin3520 161 | [link-contributors]: ../../contributors] 162 | [methods]:http://s9.picofile.com/file/8347045218/explain_1.jpg 163 | 164 | --------------------------------------------------------------------------------