Skip to content

Commit 27f9669

Browse files
author
Jonathan
committed
make service command
1 parent 5494039 commit 27f9669

File tree

11 files changed

+288
-0
lines changed

11 files changed

+288
-0
lines changed

config/crudgen.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,13 @@
2626
'section'=>'content'
2727
],
2828
],
29+
'paths' =>
30+
[
31+
'service' =>
32+
[
33+
'path' => app_path('Services'),
34+
'namespace' => 'App\Services'
35+
]
36+
]
2937

3038
];

src/Console/MakeService.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Mrdebug\Crudgen\Console;
4+
5+
use Illuminate\Console\Command;
6+
7+
use Mrdebug\Crudgen\Services\MakeGlobalService;
8+
use Mrdebug\Crudgen\Services\PathsAndNamespacesService;
9+
use Mrdebug\Crudgen\Services\Service\MakeServiceService;
10+
11+
class MakeService extends Command
12+
{
13+
/**
14+
* The name and signature of the console command.
15+
*
16+
* @var string
17+
*/
18+
protected $signature = 'make:service {service_name}';
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Create service file';
26+
27+
/**
28+
* Create a new command instance.
29+
*
30+
* @return void
31+
*/
32+
public MakeGlobalService $makeGlobalService;
33+
public PathsAndNamespacesService $pathsAndNamespacesService;
34+
public MakeServiceService $makeServiceService;
35+
36+
public function __construct(
37+
MakeGlobalService $makeGlobalService,
38+
PathsAndNamespacesService $pathsAndNamespacesService,
39+
MakeServiceService $makeServiceService
40+
)
41+
{
42+
parent::__construct();
43+
$this->makeGlobalService = $makeGlobalService;
44+
$this->pathsAndNamespacesService = $pathsAndNamespacesService;
45+
$this->makeServiceService = $makeServiceService;
46+
}
47+
48+
/**
49+
* Execute the console command.
50+
*
51+
* @return mixed
52+
*/
53+
public function handle()
54+
{
55+
// we create our variables to respect the naming conventions
56+
$serviceName = ucfirst($this->argument('service_name'));
57+
$namingConvention = $this->makeGlobalService->getCommentableNamingConvention($serviceName);
58+
$laravelNamespace = $this->laravel->getNamespace();
59+
60+
$this->makeServiceService->makeCompleteServiceFile($namingConvention, $laravelNamespace);
61+
}
62+
}

src/Console/RemoveService.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Mrdebug\Crudgen\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\File;
7+
use Mrdebug\Crudgen\Services\MakeGlobalService;
8+
use Mrdebug\Crudgen\Services\PathsAndNamespacesService;
9+
use Mrdebug\Crudgen\Services\RemoveCommentableService;
10+
11+
class RemoveService extends Command
12+
{
13+
/**
14+
* The name and signature of the console command.
15+
*
16+
* @var string
17+
*/
18+
protected $signature = 'rm:service {service_name} {--force}';
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Remove a service';
26+
27+
public RemoveCommentableService $removeCommentableService;
28+
public MakeGlobalService $makeGlobalService;
29+
public PathsAndNamespacesService $pathsAndNamespacesService;
30+
public function __construct(RemoveCommentableService $removeCommentableService,MakeGlobalService $makeGlobalService, PathsAndNamespacesService $pathsAndNamespacesService)
31+
{
32+
parent::__construct();
33+
$this->removeCommentableService = $removeCommentableService;
34+
$this->makeGlobalService = $makeGlobalService;
35+
$this->pathsAndNamespacesService = $pathsAndNamespacesService;
36+
}
37+
38+
/**
39+
* Execute the console command.
40+
*
41+
* @return mixed
42+
*/
43+
public function handle()
44+
{
45+
// we create our variables to respect the naming conventions
46+
$serviceName = ucfirst($this->argument('service_name'));
47+
$namingConvention = $this->makeGlobalService->getCommentableNamingConvention($serviceName );
48+
$force = $this->option('force');
49+
50+
$completePath = $this->pathsAndNamespacesService->getRealpathBaseCustomService($namingConvention);
51+
52+
if(File::exists($completePath))
53+
{
54+
if ($force || $this->confirm('Do you want to delete '.$completePath.'?'))
55+
{
56+
if(File::delete($completePath))
57+
$this->line("<info>".$completePath." deleted</info>");
58+
}
59+
}
60+
}
61+
}

src/CrudgenServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use Mrdebug\Crudgen\Console\MakeApiCrud;
77
use Mrdebug\Crudgen\Console\MakeCommentable;
88
use Mrdebug\Crudgen\Console\MakeCrud;
9+
use Mrdebug\Crudgen\Console\MakeService;
910
use Mrdebug\Crudgen\Console\MakeViews;
1011
use Mrdebug\Crudgen\Console\RemoveApiCrud;
1112
use Mrdebug\Crudgen\Console\RemoveCommentable;
1213
use Mrdebug\Crudgen\Console\RemoveCrud;
14+
use Mrdebug\Crudgen\Console\RemoveService;
1315

1416
class CrudgenServiceProvider extends ServiceProvider
1517
{
@@ -47,6 +49,8 @@ public function register()
4749
RemoveApiCrud::class,
4850
MakeCommentable::class,
4951
RemoveCommentable::class,
52+
MakeService::class,
53+
RemoveService::class
5054
);
5155
}
5256
}

src/Services/MakeGlobalService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function getCommentableNamingConvention($commentableName): array
4545
'singular_low_variable_name' => Str::singular(Str::camel(Str::lower($commentableName))),
4646
'plural_low_variable_name' => Str::plural(Str::camel(Str::lower($commentableName))),
4747
'table_name' => Str::plural(Str::snake($commentableName)),
48+
'service_name' => Str::singular(Str::studly($commentableName)),
4849
];
4950
}
5051

src/Services/PathsAndNamespacesService.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,28 @@ public function getCommentableCommentBlockPath()
184184
return $this->getStubPath().DIRECTORY_SEPARATOR.'commentable'.DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.'comment-block.stub';
185185
}
186186

187+
/** paths service */
188+
189+
public function getServiceStubPath(): string
190+
{
191+
return $this->getStubPath().DIRECTORY_SEPARATOR.'service'.DIRECTORY_SEPARATOR.'Service.stub';
192+
}
193+
194+
public function getRealpathBaseService(): string
195+
{
196+
return config('crudgen.paths.service.path') ?? $this->getRealpathBase('app'.DIRECTORY_SEPARATOR).'Services';
197+
}
198+
199+
public function getRealpathBaseCustomService($namingConvention): string
200+
{
201+
return $this->getRealpathBaseService().DIRECTORY_SEPARATOR.$namingConvention['service_name'].'.php';
202+
}
203+
204+
public function getDefaultNamespaceService($rootNamespace): string
205+
{
206+
return config('crudgen.paths.service.namespace') ?? $rootNamespace.'Services';
207+
}
208+
187209
public function getCrudgenViewsStub()
188210
{
189211
return resource_path('crudgen'.DIRECTORY_SEPARATOR.'views');
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Mrdebug\Crudgen\Services\Service;
4+
5+
use Illuminate\Console\Concerns\InteractsWithIO;
6+
use Illuminate\Support\Facades\File;
7+
use Mrdebug\Crudgen\Services\MakeGlobalService;
8+
use Mrdebug\Crudgen\Services\PathsAndNamespacesService;
9+
use Symfony\Component\Console\Output\ConsoleOutput;
10+
11+
class MakeServiceService
12+
{
13+
use InteractsWithIO;
14+
15+
public PathsAndNamespacesService $pathsAndNamespacesService;
16+
public MakeGlobalService $makeGlobalService;
17+
public function __construct(
18+
PathsAndNamespacesService $pathsAndNamespacesService,
19+
ConsoleOutput $consoleOutput,
20+
MakeGlobalService $makeGlobalService
21+
)
22+
{
23+
$this->pathsAndNamespacesService = $pathsAndNamespacesService;
24+
$this->output = $consoleOutput;
25+
$this->makeGlobalService = $makeGlobalService;
26+
}
27+
28+
29+
public function replaceContentServiceStub($namingConvention, $laravelNamespace)
30+
{
31+
$serviceStub = File::get($this->pathsAndNamespacesService->getServiceStubPath());
32+
$serviceStub = str_replace('DummyNamespace', $this->pathsAndNamespacesService->getDefaultNamespaceService($laravelNamespace), $serviceStub);
33+
$serviceStub = str_replace('DummyClass', $namingConvention['service_name'], $serviceStub);
34+
return $serviceStub;
35+
}
36+
37+
public function createServiceFile($serviceStub, $namingConvention)
38+
{
39+
if(!File::exists($this->pathsAndNamespacesService->getRealpathBaseService()))
40+
File::makeDirectory($this->pathsAndNamespacesService->getRealpathBaseService());
41+
42+
$completePathServiceFile = $this->pathsAndNamespacesService->getRealpathBaseCustomService($namingConvention);
43+
44+
// if the Service file doesn't exist, we create it
45+
if(!File::exists($completePathServiceFile))
46+
{
47+
File::put($completePathServiceFile, $serviceStub);
48+
$this->line("<info>Created Service:</info> ".$completePathServiceFile);
49+
}
50+
else
51+
$this->error('Service ' .$this->pathsAndNamespacesService->getRealpathBaseCustomService($namingConvention). ' already exists');
52+
}
53+
54+
public function makeCompleteServiceFile($namingConvention, $laravelNamespace)
55+
{
56+
$serviceStub = $this->replaceContentServiceStub($namingConvention, $laravelNamespace);
57+
$this->createServiceFile($serviceStub, $namingConvention);
58+
}
59+
}

src/stubs/service/Service.stub

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
class DummyClass
6+
{
7+
8+
}

tests/Console/MakeServiceTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Tests\Console;
4+
5+
use App\Models\Category;
6+
use App\Models\User;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Illuminate\Support\Facades\Artisan;
9+
use Tests\TestCase;
10+
use Illuminate\Support\Facades\File;
11+
12+
class MakeServiceTest extends TestCase
13+
{
14+
15+
public function setUp():void
16+
{
17+
parent::setUp();
18+
$this->artisan('rm:service', ['service_name'=>"PostService", "--force"=>true]);
19+
}
20+
21+
public function test_create_service()
22+
{
23+
$serviceExisting = array_map(fn($path) => basename($path), glob(app_path('Services/').'*'));
24+
$this->assertCount(0, $serviceExisting);
25+
26+
$this->artisan('make:service', ['service_name'=>"PostService"]);
27+
28+
$this->assertSame(preg_replace('/\s+/', '', File::get(__DIR__.DIRECTORY_SEPARATOR.'resultsOk/service/PostService.php')), preg_replace('/\s+/', '', File::get(app_path('Services/PostService.php'))));
29+
}
30+
31+
public function test_create_service_custom_path()
32+
{
33+
config()->set('crudgen.paths.service.path', app_path('Services2'));
34+
config()->set('crudgen.paths.service.namespace', "App\Services2");
35+
$serviceExisting = array_map(fn($path) => basename($path), glob(app_path('Services2/').'*'));
36+
$this->assertCount(0, $serviceExisting);
37+
38+
$this->artisan('make:service', ['service_name'=>"PostService"]);
39+
40+
$this->assertSame(preg_replace('/\s+/', '', File::get(__DIR__.DIRECTORY_SEPARATOR.'resultsOk/service/OtherPostService.php')), preg_replace('/\s+/', '', File::get(app_path('Services2/PostService.php'))));
41+
}
42+
43+
public function tearDown():void
44+
{
45+
$this->artisan('rm:service', ['service_name'=>"PostService", "--force"=>true]);
46+
}
47+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App\Services2;
4+
5+
class PostService
6+
{
7+
8+
}

0 commit comments

Comments
 (0)