Skip to content

Commit 8ddff7c

Browse files
author
RIonut
committed
Make Repo and Service from a single command make:logic
1 parent ad1cfbd commit 8ddff7c

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/Commands/BothCommand.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace TheCodeRepublic\Repository\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class BothCommand extends Command
8+
{
9+
10+
protected $signature = 'make:logic {name : ServiceName (ex: ProductSearchService)}';
11+
12+
protected $description = 'Create a repository and a service';
13+
14+
public function __construct()
15+
{
16+
parent::__construct();
17+
}
18+
19+
20+
public function handle()
21+
{
22+
23+
//SERVICE
24+
$name = $this->argument('name');
25+
26+
if ( ! file_exists ( $path = base_path ( 'app/Services' ) ) )
27+
mkdir($path, 0777, true);
28+
29+
if ( file_exists ( base_path ( "app/Services/{$name}Service.php" ) ) ) {
30+
$this->error("Service with that name ({$name}) already exists");
31+
exit(0);
32+
}
33+
34+
self::createService($name);
35+
36+
$this->info("Service pattern implemented for model ". $name);
37+
38+
//REPO
39+
if ( ! file_exists ( $path = base_path ( 'app/Repositories' ) ) )
40+
mkdir($path, 0777, true);
41+
42+
if ( file_exists ( base_path ( "app/Repositories/{$name}Repository.php" ) ) ) {
43+
$this->error("Repository with that name ({$name}) already exists");
44+
exit(0);
45+
}
46+
47+
self::createRepository($name);
48+
49+
$this->info("Repository pattern implemented for model ". $name);
50+
51+
52+
}
53+
54+
55+
protected static function getStubs($type)
56+
{
57+
return file_get_contents("vendor/thecoderepublic/repository/src/resources/$type.stub");
58+
}
59+
60+
61+
protected static function createService($name)
62+
{
63+
$template = str_replace( ['{{serviceName}}'], [$name], self::GetStubs('Service') );
64+
file_put_contents(base_path("app/Services/{$name}Service.php"), $template);
65+
}
66+
67+
protected static function createRepository($name)
68+
{
69+
$template = str_replace( ['{{modelName}}'], [$name], self::GetStubs('Repository') );
70+
file_put_contents(base_path("app/Repositories/{$name}Repository.php"), $template);
71+
}
72+
73+
74+
}

src/RepositoryServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace TheCodeRepublic\Repository;
44

55
use Illuminate\Support\ServiceProvider;
6+
use TheCodeRepublic\Repository\Commands\BothCommand;
67
use TheCodeRepublic\Repository\Commands\RepositoryCommand ;
78
use TheCodeRepublic\Repository\Commands\ServiceCommand ;
89

@@ -28,6 +29,7 @@ public function boot()
2829
$this->commands([
2930
RepositoryCommand::class,
3031
ServiceCommand::class,
32+
BothCommand::class,
3133
]);
3234
}
3335
}

0 commit comments

Comments
 (0)