-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBothCommand.php
93 lines (62 loc) · 2.57 KB
/
BothCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace TheCodeRepublic\Repository\Commands;
use Illuminate\Console\Command;
class BothCommand extends Command
{
protected $signature = 'make:logic {name : ServiceName (ex: ProductSearchService)}';
protected $description = 'Create a repository and a service';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$name = $this->argument('name');
//SERVICE
if ( ! file_exists ( $path = base_path ( 'app/Services' ) ) )
mkdir($path, 0777, true);
if ( file_exists ( base_path ( "app/Services/{$name}Service.php" ) ) ) {
$this->error("Service with that name ({$name}) already exists");
exit(0);
}
self::createService($name);
$this->info("Service pattern implemented for model ". $name);
//REPO
if ( ! file_exists ( $path = base_path ( 'app/Repositories' ) ) )
mkdir($path, 0777, true);
if ( file_exists ( base_path ( "app/Repositories/{$name}Repository.php" ) ) ) {
$this->error("Repository with that name ({$name}) already exists");
exit(0);
}
self::createRepository($name);
$this->info("Repository pattern implemented for model ". $name);
//Interface
if ( ! file_exists ( $path = base_path ( 'app/Interfaces' ) ) )
mkdir($path, 0777, true);
if ( file_exists ( base_path ( "app/Interfaces/{$name}Interface.php" ) ) ) {
$this->error("Interface with that name ({$name}) already exists");
exit(0);
}
self::createInterface($name);
$this->info("Interface pattern implemented");
}
protected static function getStubs($type)
{
return file_get_contents("vendor/thecoderepublic/repository/src/resources/$type.stub");
}
protected static function createService($name)
{
$template = str_replace( ['{{serviceName}}'], [$name], self::GetStubs('Service') );
file_put_contents(base_path("app/Services/{$name}Service.php"), $template);
}
protected static function createRepository($name)
{
$template = str_replace( ['{{modelName}}'], [$name], self::GetStubs('Repository') );
file_put_contents(base_path("app/Repositories/{$name}Repository.php"), $template);
}
protected static function createInterface($name)
{
$template = str_replace( ['{{modelName}}'], [$name], self::GetStubs('Interface') );
file_put_contents(base_path("app/Interfaces/{$name}Interface.php"), $template);
}
}