Skip to content

Commit f3ab891

Browse files
create a repository pattern
1 parent 50a1076 commit f3ab891

12 files changed

+485
-8
lines changed

README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,122 @@ Add following code in root blade file in before close the body.
5959
}
6060
</script>
6161
```
62+
# Repository Generator
63+
64+
## Table of Contents
65+
<ol>
66+
<li><a href="#features">Features</a></li>
67+
<li>
68+
<a href="#getting-started">Getting started</a>
69+
<ul>
70+
<li><a href="#installation">Installation</a></li>
71+
<li><a href="#publish-config-(optional)">Publish config (optional)</a></li>
72+
</ul>
73+
</li>
74+
<li>
75+
<a href="#usage">Usage</a>
76+
<ul>
77+
<li><a href="#generating-repositories">Generating repositories</a></li>
78+
<li><a href="#dependency-injection">Dependency Injection</a></li>
79+
</ul>
80+
</li>
81+
<li><a href="#manual-binding">Manual binding</a></li>
82+
<li><a href="#more-generator-packages">More generator packages</a></li>
83+
<li><a href="#contributing">Contributing</a></li>
84+
<li><a href="#license">License</a></li>
85+
</ol>
86+
87+
## Features
88+
With this package you can generate repositories with the ```artisan make:repository``` command.
89+
The generator will generate the repository, repository interface and will bind them automatically (can be changed to
90+
manual binding) to the Service Container so you can inject the interface into your controllers.
91+
92+
### Publish config (optional)
93+
```
94+
php artisan vendor:publish --provider="RajTechnologies\Tools\ToolServiceProvider" --tag="config"
95+
```
96+
97+
## Usage
98+
For usage take the following steps. Generate the repository and then inject it into a controller or service.
99+
100+
### Generating repositories
101+
Run the following command.
102+
```
103+
php artisan make:repository UserRepository
104+
```
105+
This example will generate the following files:
106+
```
107+
app\Repositories\Eloquent\UserRepository
108+
app\Repositories\UserRepositoryInterface
109+
```
110+
111+
### Dependency Injection
112+
Next we have to inject the interface into the constructor our controller or service. For this example we will use the UserController.
113+
```php
114+
<?php
115+
116+
namespace App\Http\Controllers;
117+
118+
use App\Repositories\UserRepositoryInterface;
119+
120+
class UserController extends Controller
121+
{
122+
private $user;
123+
124+
public function __construct(UserRepositoryInterface $userRepository)
125+
{
126+
$this->user = $userRepository;
127+
}
128+
129+
// your controller functions
130+
}
131+
```
132+
133+
By default you will be able to use Eloquent methods like ```all()``` and ```find()```.
134+
You can extend this in your repository. Now you will be able to use your repository
135+
in your methods like this.
136+
```php
137+
public function index()
138+
{
139+
return $this->user->all();
140+
}
141+
```
142+
## Manual binding
143+
By default the package will automatically bind the repository interfaces for you with the repositories so you can
144+
inject the interface into your controllers. If you want to bind manually you can disable
145+
this behaviour by setting the ```auto_bind_interfaces``` option to ```false``` in ```config\repository-generator.php```.
146+
If the config is not there make sure to publish it first as described in the Installation chapter.
147+
148+
You can add your bindings to your AppServiceProvider or
149+
you can a create a new provider with ```php artisan make:provider RepositoryServiceProvider```
150+
(don't forget to add it in ```config\app.php```) and add the bindings in the ```register()``` method, see the example below.
151+
152+
```php
153+
<?php
154+
155+
namespace App\Providers;
156+
157+
use App\Repositories\Eloquent\UserRepository;
158+
use App\Repositories\UserRepositoryInterface;
159+
use Illuminate\Support\ServiceProvider;
160+
161+
/**
162+
* Class RepositoryServiceProvider
163+
* @package App\Providers
164+
*/
165+
class RepositoryServiceProvider extends ServiceProvider
166+
{
167+
/**
168+
* Register services.
169+
*
170+
* @return void
171+
*/
172+
public function register()
173+
{
174+
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
175+
}
176+
}
177+
```
62178
## Contributing
63179

64180
- [Bhargav Raviya](https://github.com/bhargavraviya)

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rajtechnologies/laravel-tools",
3-
"description": "All Type of Base Tools to Helping Development",
3+
"description": "All Type of Base Tools to Helping Development and Repository Generator",
44
"type": "library",
55
"keywords": [
66
"laravel-clear-cache",
@@ -9,7 +9,10 @@
99
"laravel-pwa",
1010
"pwa-laravel",
1111
"laravel",
12-
"php"
12+
"php",
13+
"generator",
14+
"cli",
15+
"artisan"
1316
],
1417
"license": "MIT",
1518
"authors": [

config/repository-generator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'auto_bind_interfaces' => true,
5+
];

src/Console/MakeRepository.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace RajTechnologies\Tools\Console;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Support\Str;
7+
8+
class MakeRepository extends GeneratorCommand
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'make:repository {name}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new repository class';
23+
24+
/**
25+
* The type of class being generated.
26+
*
27+
* @var string
28+
*/
29+
protected $type = 'Repository';
30+
31+
/**
32+
* Get the stub file for the generator.
33+
*
34+
* @return string
35+
*/
36+
protected function getStub()
37+
{
38+
return __DIR__.'/../../stubs/repository.stub';
39+
}
40+
41+
public function handle()
42+
{
43+
// Generate the repository interface
44+
$this->call('make:repository-interface', ['name' => $this->getNameInput() . 'Interface']);
45+
46+
return parent::handle();
47+
}
48+
49+
/**
50+
* Get the default namespace for the class.
51+
*
52+
* @param string $rootNamespace
53+
* @return string
54+
*/
55+
protected function getDefaultNamespace($rootNamespace)
56+
{
57+
return $rootNamespace.'\Repositories\Eloquent';
58+
}
59+
60+
/**
61+
* Replace the namespace for the given stub.
62+
*
63+
* @param string $stub
64+
* @param string $name
65+
* @return $this
66+
*/
67+
protected function replaceNamespace(&$stub, $name)
68+
{
69+
$classname = Str::replace('Repository', '', $this->getNameInput());
70+
$stub = Str::replace('{{ Model }}', $classname, $stub);
71+
$stub = Str::replace('{{ namespacedModel }}', 'App\Models\\' . $classname, $stub);
72+
73+
return parent::replaceNamespace($stub, $name);
74+
}
75+
76+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace RajTechnologies\Tools\Console;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Support\Facades\Artisan;
7+
8+
class MakeRepositoryInterface extends GeneratorCommand
9+
{
10+
protected $hidden = true;
11+
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'make:repository-interface {name}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Create a new repository interface class';
25+
26+
/**
27+
* The type of class being generated.
28+
*
29+
* @var string
30+
*/
31+
protected $type = 'Repository Interface';
32+
33+
/**
34+
* Get the stub file for the generator.
35+
*
36+
* @return string
37+
*/
38+
protected function getStub()
39+
{
40+
return __DIR__.'/../../stubs/repository-interface.stub';
41+
}
42+
43+
/**
44+
* Get the default namespace for the class.
45+
*
46+
* @param string $rootNamespace
47+
* @return string
48+
*/
49+
protected function getDefaultNamespace($rootNamespace)
50+
{
51+
return $rootNamespace.'\Repositories';
52+
}
53+
}

src/Http/Controllers/HttpListController.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
use App\Http\Controllers\Controller;
66
use Illuminate\Http\Request;
7-
use RajTechnologies\Reminder\Repositories\WalkReminderRepository;
8-
use Illuminate\Support\Facades\Validator;
9-
use Route;
10-
use Artisan;
117
class HttpListController extends Controller
128
{
139
public function index(Request $request)

src/Http/Controllers/RouteController.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use App\Http\Controllers\Controller;
66
use Illuminate\Http\Request;
7-
use RajTechnologies\Reminder\Repositories\WalkReminderRepository;
8-
use Illuminate\Support\Facades\Validator;
97
use Route;
108
use Artisan;
119
class RouteController extends Controller

0 commit comments

Comments
 (0)