Skip to content

Commit 858c32e

Browse files
committed
basic generator for repository boilerplate
1 parent 83c072b commit 858c32e

File tree

9 files changed

+470
-2
lines changed

9 files changed

+470
-2
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace AwesIO\Repository\Commands;
4+
5+
use AwesIO\Repository\Services\Replacer;
6+
use Illuminate\Console\GeneratorCommand;
7+
8+
class RepositoryMakeCommand extends GeneratorCommand
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'make:repository {name} {--model=} {--scopes=}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new Awes.IO repository';
23+
24+
/**
25+
* The type of class being generated.
26+
*
27+
* @var string
28+
*/
29+
protected $type = 'Awes.IO platform repository';
30+
31+
/**
32+
* Execute the console command.
33+
*
34+
* @return void
35+
*/
36+
public function handle()
37+
{
38+
if (parent::handle() === false && ! $this->option('force')) {
39+
return false;
40+
}
41+
}
42+
43+
/**
44+
* Build the class with the given name.
45+
*
46+
* @param string $name
47+
* @return string
48+
*/
49+
protected function buildClass($name)
50+
{
51+
$model = $this->option('model');
52+
53+
$scopes = $this->option('scopes');
54+
55+
return (new Replacer(parent::buildClass($name)))
56+
->replace($model, $scopes);
57+
}
58+
59+
/**
60+
* Get the stub file for the generator.
61+
*
62+
* @return string
63+
*/
64+
protected function getStub()
65+
{
66+
return __DIR__.'/stubs/repository.stub';
67+
}
68+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace AwesIO\Repository\Commands;
4+
5+
use Illuminate\Support\Str;
6+
use Illuminate\Console\Command;
7+
8+
class RepositoryMakeMainCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'repository:generate {modelName} {--scope=}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new Awes.IO repository';
23+
24+
/**
25+
* The type of class being generated.
26+
*
27+
* @var string
28+
*/
29+
protected $type = 'Model';
30+
31+
protected $baseNamespace;
32+
33+
/**
34+
* Execute the console command.
35+
*
36+
* @return void
37+
*/
38+
public function handle()
39+
{
40+
$this->baseNamespace = 'Repositories\\'.Str::plural($this->getNameInput());
41+
42+
$this->createRepository();
43+
44+
$this->createScopes();
45+
46+
if ($scope = $this->option('scope')) {
47+
$this->createScope($scope);
48+
}
49+
}
50+
51+
/**
52+
* Create a repository.
53+
*
54+
* @return void
55+
*/
56+
protected function createRepository()
57+
{
58+
$this->call('make:repository', [
59+
'name' => $this->getNamespacedRepository(),
60+
'--model' => $this->getNamespacedModel(),
61+
'--scopes' => $this->getNamespacedScopes(),
62+
]);
63+
}
64+
65+
/**
66+
* Create a repository.
67+
*
68+
* @return void
69+
*/
70+
protected function createScopes()
71+
{
72+
$this->call('make:repository:scopes', [
73+
'name' => $this->getNamespacedScopes(),
74+
]);
75+
}
76+
77+
/**
78+
* Create a repository.
79+
*
80+
* @return void
81+
*/
82+
protected function createScope($scope)
83+
{
84+
$this->call('make:repository:scope', [
85+
'name' => $this->getNamespacedScope($scope),
86+
]);
87+
}
88+
89+
/**
90+
* Get the desired class name from the input.
91+
*
92+
* @return string
93+
*/
94+
protected function getNameInput()
95+
{
96+
return last(explode('/', trim($this->argument('modelName'))));
97+
}
98+
99+
/**
100+
* Get the root namespace for the class.
101+
*
102+
* @return string
103+
*/
104+
protected function rootNamespace()
105+
{
106+
return $this->laravel->getNamespace();
107+
}
108+
109+
private function getNamespacedModel()
110+
{
111+
return $this->baseNamespace .'\Models\\'
112+
. Str::singular($this->getNameInput());
113+
}
114+
115+
private function getNamespacedScopes()
116+
{
117+
return $this->baseNamespace .'\Scopes\\'
118+
. Str::plural($this->getNameInput()) . 'Scopes';
119+
}
120+
121+
private function getNamespacedScope($scope)
122+
{
123+
return $this->baseNamespace .'\Scopes\\'
124+
. Str::singular($scope) . Str::plural($this->getNameInput()) . 'Scope';
125+
}
126+
127+
private function getNamespacedRepository()
128+
{
129+
$repository = Str::studly(class_basename($this->getNameInput()));
130+
131+
return $this->baseNamespace .'\\'
132+
. Str::plural($repository) . 'Repository';
133+
}
134+
135+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace AwesIO\Repository\Commands;
4+
5+
use AwesIO\Repository\Services\Replacer;
6+
use Illuminate\Console\GeneratorCommand;
7+
8+
class RepositoryScopeMakeCommand extends GeneratorCommand
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'make:repository:scope {name}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new Awes.IO repository scope';
23+
24+
/**
25+
* The type of class being generated.
26+
*
27+
* @var string
28+
*/
29+
protected $type = 'Awes.IO platform repository scope';
30+
31+
/**
32+
* Execute the console command.
33+
*
34+
* @return void
35+
*/
36+
public function handle()
37+
{
38+
if (parent::handle() === false && ! $this->option('force')) {
39+
return false;
40+
}
41+
}
42+
43+
/**
44+
* Build the class with the given name.
45+
*
46+
* @param string $name
47+
* @return string
48+
*/
49+
protected function buildClass($name)
50+
{
51+
return (new Replacer(parent::buildClass($name)))
52+
->replace($name);
53+
}
54+
55+
/**
56+
* Get the stub file for the generator.
57+
*
58+
* @return string
59+
*/
60+
protected function getStub()
61+
{
62+
return __DIR__.'/stubs/scope.stub';
63+
}
64+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace AwesIO\Repository\Commands;
4+
5+
use AwesIO\Repository\Services\Replacer;
6+
use Illuminate\Console\GeneratorCommand;
7+
8+
class RepositoryScopesMakeCommand extends GeneratorCommand
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'make:repository:scopes {name}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new Awes.IO repository scopes';
23+
24+
/**
25+
* The type of class being generated.
26+
*
27+
* @var string
28+
*/
29+
protected $type = 'Awes.IO platform repository scopes';
30+
31+
/**
32+
* Execute the console command.
33+
*
34+
* @return void
35+
*/
36+
public function handle()
37+
{
38+
if (parent::handle() === false && ! $this->option('force')) {
39+
return false;
40+
}
41+
}
42+
43+
/**
44+
* Build the class with the given name.
45+
*
46+
* @param string $name
47+
* @return string
48+
*/
49+
protected function buildClass($name)
50+
{
51+
return (new Replacer(parent::buildClass($name)))
52+
->replace($name);
53+
}
54+
55+
/**
56+
* Get the stub file for the generator.
57+
*
58+
* @return string
59+
*/
60+
protected function getStub()
61+
{
62+
return __DIR__.'/stubs/scopes.stub';
63+
}
64+
}

src/Commands/stubs/repository.stub

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use App\DummyModel;
6+
use AwesIO\Repository\Eloquent\BaseRepository;
7+
use NamespacedDummyScope;
8+
9+
class DummyClass extends BaseRepository
10+
{
11+
/**
12+
* The attributes that can be searched by.
13+
*
14+
* @var array
15+
*/
16+
protected $searchable = [];
17+
18+
/**
19+
* Returns DocDummyModel model's full class name.
20+
*
21+
* @return string
22+
*/
23+
public function entity()
24+
{
25+
return DummyModel::class;
26+
}
27+
28+
public function scope($request)
29+
{
30+
parent::scope($request);
31+
$this->entity = (new DummyScope($request))->scope($this->entity);
32+
return $this;
33+
}
34+
}

src/Commands/stubs/scope.stub

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use AwesIO\Repository\Scopes\ScopeAbstract;
6+
7+
class DummyScope extends ScopeAbstract
8+
{
9+
public function scope($builder, $value, $scope)
10+
{
11+
return $builder->where($scope, $value);
12+
}
13+
}

src/Commands/stubs/scopes.stub

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use AwesIO\Repository\Scopes\ScopesAbstract;
6+
7+
class DummyScope extends ScopesAbstract
8+
{
9+
protected $scopes = [
10+
//'search' => SearchScope::class,
11+
];
12+
}

0 commit comments

Comments
 (0)