Skip to content

Commit 34e638e

Browse files
committed
Console: HMS Entity boilerplate creation helpers
Usage: `php artisan hms:make GateKeeper/RfidTag` `hms:make` will create boilerplate files for Entity, Mapping, Repository Interface, Repository Implementation (docrtine), Factory each file can also be generated individually using the appropriate command from below ``` hms:make Create a new Entity/Mapping/Interface/Implementation/Factory all in one go. hms:make:entity Create a new Entity hms:make:factory Create a new Factory hms:make:mapping Create a new Mapping hms:make:repository:implementation Create a new Repository Implementation hms:make:repository:interface Create a new Repository Interface ```
1 parent da93c5d commit 34e638e

13 files changed

+600
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Console\Commands\Make;
4+
5+
class EntityMakeCommand extends GeneratorCommand
6+
{
7+
/**
8+
* The name and signature of the console command.
9+
*
10+
* @var string
11+
*/
12+
protected $signature = 'hms:make:entity
13+
{name : Name of the entity to create}';
14+
15+
/**
16+
* The console command description.
17+
*
18+
* @var string
19+
*/
20+
protected $description = 'Create a new Entity';
21+
22+
/**
23+
* Get the stub file for the generator.
24+
*
25+
* @return string
26+
*/
27+
protected function getStub()
28+
{
29+
return __DIR__.'/stubs/entity.stub';
30+
}
31+
32+
/**
33+
* Execute the console command.
34+
*
35+
* @return mixed
36+
*/
37+
public function handle()
38+
{
39+
parent::fire();
40+
}
41+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace App\Console\Commands\Make;
4+
5+
class FactoryMakeCommand extends GeneratorCommand
6+
{
7+
/**
8+
* The name and signature of the console command.
9+
*
10+
* @var string
11+
*/
12+
protected $signature = 'hms:make:factory
13+
{name : Name of the entity this factory is for}';
14+
15+
/**
16+
* The console command description.
17+
*
18+
* @var string
19+
*/
20+
protected $description = 'Create a new Factory';
21+
22+
/**
23+
* Get the stub file for the generator.
24+
*
25+
* @return string
26+
*/
27+
protected function getStub()
28+
{
29+
return __DIR__.'/stubs/factory.stub';
30+
}
31+
32+
/**
33+
* Execute the console command.
34+
*
35+
* @return mixed
36+
*/
37+
public function handle()
38+
{
39+
parent::fire();
40+
}
41+
42+
/**
43+
* Build the class with the given name.
44+
*
45+
* @param string $name
46+
* @return string
47+
*/
48+
protected function buildClass($name)
49+
{
50+
$stub = parent::buildClass($name);
51+
$name = str_replace($this->getDefaultNamespace($name).'\\', '', $name);
52+
$dummyFactoryNamspace = $this->rootNamespace().'\\Factories\\'.$name;
53+
$dummyFactoryNamspace = trim(implode('\\', array_slice(explode('\\', $dummyFactoryNamspace), 0, -1)), '\\');
54+
55+
$stub = str_replace('DummyFactoryNamspace',
56+
$dummyFactoryNamspace,
57+
$stub
58+
);
59+
60+
return $stub;
61+
}
62+
63+
/**
64+
* Get the destination class path.
65+
*
66+
* @param string $name
67+
* @return string
68+
*/
69+
protected function getPath($name)
70+
{
71+
$name = str_replace($this->getDefaultNamespace($name).'\\', '', $name);
72+
73+
return $this->laravel['path'].'/HMS/Factories/'.str_replace('\\', '/', $name).'Factory.php';
74+
}
75+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
namespace App\Console\Commands\Make;
4+
5+
use Illuminate\Support\Str;
6+
use Illuminate\Console\GeneratorCommand as LaravelGeneratorCommand;
7+
8+
abstract class GeneratorCommand extends LaravelGeneratorCommand
9+
{
10+
/**
11+
* Build the class with the given name.
12+
*
13+
* @param string $name
14+
* @return string
15+
*/
16+
protected function buildClass($name)
17+
{
18+
$stub = parent::buildClass($name);
19+
20+
$stub = str_replace(
21+
['DummyRepositoryInterfaceNamespace',
22+
'DummyRepositoryImplementationNamespace',
23+
'NamespacedDummyRepositoryInterface',
24+
'dummyClass',
25+
],
26+
[$this->getRepositoryInterfaceNamespace($name),
27+
$this->getRepositoryImplementationNamespace($name),
28+
$this->getNamespacedRepositoryInterface($name),
29+
$this->getClassInstance($name),
30+
],
31+
$stub
32+
);
33+
34+
return $stub;
35+
}
36+
37+
/**
38+
* Get the root namespace for the class.
39+
*
40+
* @return string
41+
*/
42+
protected function rootNamespace()
43+
{
44+
return 'HMS';
45+
}
46+
47+
/**
48+
* Get the destination class path.
49+
*
50+
* @param string $name
51+
* @return string
52+
*/
53+
protected function getPath($name)
54+
{
55+
return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php';
56+
}
57+
58+
/**
59+
* Get the default namespace for the class.
60+
*
61+
* @param string $rootNamespace
62+
* @return string
63+
*/
64+
protected function getDefaultNamespace($rootNamespace)
65+
{
66+
return config('repositories.entity_namespace');
67+
}
68+
69+
/**
70+
* Get the namespaced repository interface for the class.
71+
*
72+
* @param string $name
73+
* @return string
74+
*/
75+
protected function getNamespacedRepositoryInterface($name)
76+
{
77+
$name = str_replace($this->getDefaultNamespace($name).'\\', '', $name);
78+
$interface = config('repositories.repositoriy_namespace') . '\\' . $name . 'Repository';
79+
80+
return $interface;
81+
}
82+
83+
/**
84+
* Get the repository interface namespace for the class.
85+
*
86+
* @param string $name
87+
* @return string
88+
*/
89+
protected function getRepositoryInterfaceNamespace($name)
90+
{
91+
$interface = $this->getNamespacedRepositoryInterface($name);
92+
93+
return trim(implode('\\', array_slice(explode('\\', $interface), 0, -1)), '\\');
94+
}
95+
96+
/**
97+
* Get the namespaced repository implementation for the class.
98+
*
99+
* @param string $name
100+
* @return string
101+
*/
102+
protected function getNamespacedRepositoryImplementation($name)
103+
{
104+
$name = str_replace($this->getDefaultNamespace($name).'\\', '', $name);
105+
$implementation = config('repositories.repositoriy_namespace') . '\\' .
106+
(strpos($name, '\\') ? explode('\\', $name)[0] . '\\' : '') .
107+
'Doctrine\\Doctrine' .
108+
(strpos($name, '\\') ? explode('\\', $name)[1] : $name) .
109+
'Repository';
110+
111+
return $implementation;
112+
}
113+
114+
/**
115+
* Get the repository implementation namespace for the class.
116+
*
117+
* @param string $name
118+
* @return string
119+
*/
120+
protected function getRepositoryImplementationNamespace($name)
121+
{
122+
$implementation = $this->getNamespacedRepositoryImplementation($name);
123+
124+
return trim(implode('\\', array_slice(explode('\\', $implementation), 0, -1)), '\\');
125+
}
126+
127+
/**
128+
* Get the camelcase name for the class.
129+
*
130+
* @param string $name
131+
* @return string
132+
*/
133+
protected function getClassInstance($name)
134+
{
135+
$class = str_replace($this->getNamespace($name).'\\', '', $name);
136+
137+
return Str::camel($class);
138+
}
139+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace App\Console\Commands\Make;
4+
5+
use Illuminate\Console\Command;
6+
7+
class MakeCommand extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'hms:make
15+
{name : Name of the entity to create}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new Entity/Mapping/Interface/Implementation/Factory all in one go.';
23+
24+
/**
25+
* Execute the console command.
26+
*
27+
* @return mixed
28+
*/
29+
public function handle()
30+
{
31+
$name = trim($this->argument('name'));
32+
33+
$this->call('hms:make:entity', [
34+
'name' => "$name",
35+
]);
36+
37+
$this->call('hms:make:mapping', [
38+
'name' => "$name",
39+
]);
40+
41+
$this->call('hms:make:repository:interface', [
42+
'name' => "$name",
43+
]);
44+
45+
$this->call('hms:make:repository:implementation', [
46+
'name' => "$name",
47+
]);
48+
49+
$this->call('hms:make:factory', [
50+
'name' => "$name",
51+
]);
52+
}
53+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace App\Console\Commands\Make;
4+
5+
use Illuminate\Support\Str;
6+
7+
class MappingMakeCommand extends GeneratorCommand
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'hms:make:mapping
15+
{name : Name of the entity this mapping is for}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new Mapping';
23+
24+
/**
25+
* Get the stub file for the generator.
26+
*
27+
* @return string
28+
*/
29+
protected function getStub()
30+
{
31+
return __DIR__.'/stubs/mapping.stub';
32+
}
33+
34+
/**
35+
* Execute the console command.
36+
*
37+
* @return mixed
38+
*/
39+
public function handle()
40+
{
41+
parent::fire();
42+
}
43+
44+
/**
45+
* Build the class with the given name.
46+
*
47+
* @param string $name
48+
* @return string
49+
*/
50+
protected function buildClass($name)
51+
{
52+
$stub = parent::buildClass($name);
53+
54+
$namspacedDummmyMapping = str_replace('\\', '.', $name);
55+
56+
$dummyTable = str_replace('\\', '', Str::snake(Str::plural(str_replace($this->getNamespace($name).'\\', '', $name))));
57+
58+
$stub = str_replace(
59+
['NamspacedDummmyMapping', 'dummyTable'],
60+
[$namspacedDummmyMapping, $dummyTable],
61+
$stub
62+
);
63+
64+
return $stub;
65+
}
66+
67+
/**
68+
* Get the destination class path.
69+
*
70+
* @param string $name
71+
* @return string
72+
*/
73+
protected function getPath($name)
74+
{
75+
$name = str_replace('\\', '.', $name);
76+
77+
return $this->laravel['path'].'/HMS/Mappings/'.str_replace('\\', '/', $name).'.dcm.yml';
78+
}
79+
}

0 commit comments

Comments
 (0)