Skip to content

Commit c34b0ce

Browse files
Action, Controller and Service Commmands
1 parent 44db9c3 commit c34b0ce

File tree

4 files changed

+266
-2
lines changed

4 files changed

+266
-2
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
"type": "library",
55
"require": {
66
"php": "^8.3",
7-
"laravel/framework": "^11.0|^12.0",
8-
"illuminate/console": "*"
7+
"illuminate/console": "^11.0|^12.0",
8+
"illuminate/support": "^11.0|^12.0",
9+
"illuminate/filesystem": "^11.0|^12.0"
910
},
1011
"require-dev": {
1112
"laravel/pint": "^1.13",

src/Commands/MakeActionCommand.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace PlinCode\LaravelCleanArchitecture\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
use Illuminate\Support\Str;
8+
9+
class MakeActionCommand extends Command
10+
{
11+
protected $signature = 'clean-arch:make-action {name : The name of the action}
12+
{domain : The domain name}
13+
{--force : Overwrite existing files}';
14+
15+
protected $description = 'Create a new action in the specified domain';
16+
17+
protected Filesystem $files;
18+
19+
public function __construct(Filesystem $files)
20+
{
21+
parent::__construct();
22+
$this->files = $files;
23+
}
24+
25+
public function handle(): int
26+
{
27+
$name = $this->argument('name');
28+
$domain = $this->argument('domain');
29+
$force = $this->option('force');
30+
31+
$this->info("🚀 Creating action: {$name} for domain: {$domain}");
32+
33+
$this->createAction($name, $domain);
34+
35+
$this->info("✅ Action {$name} created successfully!");
36+
37+
return self::SUCCESS;
38+
}
39+
40+
protected function createAction(string $name, string $domain): void
41+
{
42+
$stub = $this->getStub('action');
43+
$content = $this->replacePlaceholders($stub, $name, $domain);
44+
45+
$pluralDomain = Str::plural($domain);
46+
$actionsPath = app_path("Application/Actions/{$pluralDomain}");
47+
48+
if (! $this->files->isDirectory($actionsPath)) {
49+
$this->files->makeDirectory($actionsPath, 0755, true);
50+
}
51+
52+
$this->files->put("{$actionsPath}/{$name}Action.php", $content);
53+
$this->info("Created: Application/Actions/{$pluralDomain}/{$name}Action.php");
54+
}
55+
56+
protected function replacePlaceholders(string $content, string $name, string $domain): string
57+
{
58+
$pluralDomain = Str::plural($domain);
59+
$domainVariable = Str::camel($domain);
60+
61+
$replacements = [
62+
'{{ActionName}}' => $name . 'Action',
63+
'{{DomainName}}' => $domain,
64+
'{{PluralDomainName}}' => $pluralDomain,
65+
'{{domainVariable}}' => $domainVariable,
66+
'{{RequestName}}' => 'Request',
67+
];
68+
69+
return str_replace(array_keys($replacements), array_values($replacements), $content);
70+
}
71+
72+
protected function getStub(string $stub): string
73+
{
74+
$stubPath = __DIR__ . "/../../stubs/{$stub}.stub";
75+
76+
if (! $this->files->exists($stubPath)) {
77+
throw new \Exception("Stub file not found: {$stubPath}");
78+
}
79+
80+
return $this->files->get($stubPath);
81+
}
82+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace PlinCode\LaravelCleanArchitecture\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
use Illuminate\Support\Str;
8+
9+
class MakeControllerCommand extends Command
10+
{
11+
protected $signature = 'clean-arch:make-controller {name : The name of the controller}
12+
{--api : Generate API controller}
13+
{--web : Generate Web controller}
14+
{--force : Overwrite existing files}';
15+
16+
protected $description = 'Create a new controller in the Infrastructure layer';
17+
18+
protected Filesystem $files;
19+
20+
public function __construct(Filesystem $files)
21+
{
22+
parent::__construct();
23+
$this->files = $files;
24+
}
25+
26+
public function handle(): int
27+
{
28+
$name = $this->argument('name');
29+
$isApi = $this->option('api');
30+
$isWeb = $this->option('web');
31+
$force = $this->option('force');
32+
33+
if (! $isApi && ! $isWeb) {
34+
$isApi = true; // Default to API
35+
}
36+
37+
$this->info("🚀 Creating controller: {$name}");
38+
39+
if ($isApi) {
40+
$this->createApiController($name);
41+
}
42+
43+
if ($isWeb) {
44+
$this->createWebController($name);
45+
}
46+
47+
$this->info("✅ Controller {$name} created successfully!");
48+
49+
return self::SUCCESS;
50+
}
51+
52+
protected function createApiController(string $name): void
53+
{
54+
$stub = $this->getStub('controller');
55+
$content = $this->replacePlaceholders($stub, $name);
56+
57+
$controllersPath = app_path('Infrastructure/API/Controllers');
58+
if (! $this->files->isDirectory($controllersPath)) {
59+
$this->files->makeDirectory($controllersPath, 0755, true);
60+
}
61+
62+
$this->files->put("{$controllersPath}/{$name}Controller.php", $content);
63+
$this->info("Created: Infrastructure/API/Controllers/{$name}Controller.php");
64+
}
65+
66+
protected function createWebController(string $name): void
67+
{
68+
$stub = $this->getStub('web-controller');
69+
$content = $this->replacePlaceholders($stub, $name);
70+
71+
$controllersPath = app_path('Infrastructure/UI/Web/Controllers');
72+
if (! $this->files->isDirectory($controllersPath)) {
73+
$this->files->makeDirectory($controllersPath, 0755, true);
74+
}
75+
76+
$this->files->put("{$controllersPath}/{$name}Controller.php", $content);
77+
$this->info("Created: Infrastructure/UI/Web/Controllers/{$name}Controller.php");
78+
}
79+
80+
protected function replacePlaceholders(string $content, string $name): string
81+
{
82+
$pluralName = Str::plural($name);
83+
$domainVariable = Str::camel($name);
84+
85+
$replacements = [
86+
'{{ControllerName}}' => $name . 'Controller',
87+
'{{DomainName}}' => $name,
88+
'{{PluralDomainName}}' => $pluralName,
89+
'{{domainVariable}}' => $domainVariable,
90+
];
91+
92+
return str_replace(array_keys($replacements), array_values($replacements), $content);
93+
}
94+
95+
protected function getStub(string $stub): string
96+
{
97+
$stubPath = __DIR__ . "/../../stubs/{$stub}.stub";
98+
99+
if (! $this->files->exists($stubPath)) {
100+
throw new \Exception("Stub file not found: {$stubPath}");
101+
}
102+
103+
return $this->files->get($stubPath);
104+
}
105+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace PlinCode\LaravelCleanArchitecture\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
use Illuminate\Support\Str;
8+
9+
class MakeServiceCommand extends Command
10+
{
11+
protected $signature = 'clean-arch:make-service {name : The name of the service}
12+
{--force : Overwrite existing files}';
13+
14+
protected $description = 'Create a new service in the Application layer';
15+
16+
protected Filesystem $files;
17+
18+
public function __construct(Filesystem $files)
19+
{
20+
parent::__construct();
21+
$this->files = $files;
22+
}
23+
24+
public function handle(): int
25+
{
26+
$name = $this->argument('name');
27+
$force = $this->option('force');
28+
29+
$this->info("🚀 Creating service: {$name}");
30+
31+
$this->createService($name);
32+
33+
$this->info("✅ Service {$name} created successfully!");
34+
35+
return self::SUCCESS;
36+
}
37+
38+
protected function createService(string $name): void
39+
{
40+
$stub = $this->getStub('service');
41+
$content = $this->replacePlaceholders($stub, $name);
42+
43+
$servicesPath = app_path('Application/Services');
44+
if (! $this->files->isDirectory($servicesPath)) {
45+
$this->files->makeDirectory($servicesPath, 0755, true);
46+
}
47+
48+
$this->files->put("{$servicesPath}/{$name}Service.php", $content);
49+
$this->info("Created: Application/Services/{$name}Service.php");
50+
}
51+
52+
protected function replacePlaceholders(string $content, string $name): string
53+
{
54+
$pluralName = Str::plural($name);
55+
$domainVariable = Str::camel($name);
56+
57+
$replacements = [
58+
'{{DomainName}}' => $name,
59+
'{{PluralDomainName}}' => $pluralName,
60+
'{{domainVariable}}' => $domainVariable,
61+
];
62+
63+
return str_replace(array_keys($replacements), array_values($replacements), $content);
64+
}
65+
66+
protected function getStub(string $stub): string
67+
{
68+
$stubPath = __DIR__ . "/../../stubs/{$stub}.stub";
69+
70+
if (! $this->files->exists($stubPath)) {
71+
throw new \Exception("Stub file not found: {$stubPath}");
72+
}
73+
74+
return $this->files->get($stubPath);
75+
}
76+
}

0 commit comments

Comments
 (0)