Skip to content

Commit f2b77a3

Browse files
committed
Add generic make commands
1 parent 3e41847 commit f2b77a3

File tree

7 files changed

+104
-23
lines changed

7 files changed

+104
-23
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "laravel-ready/packager",
33
"description": "Minicli Application Template",
4-
"version": "1.1.0",
4+
"version": "1.2.0",
55
"license": "MIT",
66
"homepage": "https://github.com/minicli/application",
77
"keywords": ["cli","command-line", "template"],

resources/stubs/command/Command.php.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class {{ MAKE_CLASSNAME }} extends Command
1111
*
1212
* @var string
1313
*/
14-
protected $signature = 'command:name';
14+
protected $signature = '{{ PACKAGE_SLUG }}:{{ COMMAND_SLUG }}';
1515

1616
/**
1717
* The console command description.
@@ -39,7 +39,7 @@ class {{ MAKE_CLASSNAME }} extends Command
3939
*/
4040
private function askQuestion()
4141
{
42-
$answer = $this->ask('Do you use Laravel?');
42+
$answer = $this->ask('Do you love Laravel?');
4343

4444
// this answer required
4545
if (!$answer) {

resources/stubs/command/Controller.php.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ class {{ MAKE_CLASSNAME }} extends BaseController
1010
{
1111
public function index(Request $request)
1212
{
13-
// add your controller code here
13+
// add your controller methods here
1414
}
1515
}

resources/stubs/command/Service.php.stub

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ class {{ MAKE_CLASSNAME }}
77
public function __construct()
88
{
99
}
10+
11+
public function sayHello()
12+
{
13+
return 'Hello World!';
14+
}
1015
}

src/Command/Make/DefaultController.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,24 @@ public function handle(): void
5959
return;
6060
}
6161

62+
$result = false;
63+
6264
if ($command['name'] === 'migration') {
6365
$type = $this->hasParam('--type') && in_array($this->getParam('--type'), $this->makeMigrationList)
6466
? $this->getParam('--type')
6567
: 'create';
6668

6769
$result = $this->packagerService->makeMigration($command['value'], $type);
70+
} else {
71+
$result = $this->packagerService->make($command['name'], $command['value']);
72+
}
73+
74+
if ($result === true) {
75+
$this->getPrinter()->success("Make {$command['name']}: \"{$command['value']}\" created successfully.");
76+
} else if ($result === false) {
77+
$this->getPrinter()->error("Make {$command['name']}: \"{$command['value']}\" failed. Please retry.");
78+
} else if ($result === null) {
79+
$this->getPrinter()->display("Make {$command['name']}: \"{$command['value']}\" already exists.");
6880
}
6981
}
7082

src/Services/PackagerService.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,70 @@ class PackagerService
2222

2323
private StubSupport $stubSupport;
2424

25+
/**
26+
* Relative paths for selected commands
27+
*/
28+
private $relativePaths = [
29+
'controller' => 'Http\Controllers',
30+
'command' => 'Console\Commands',
31+
'model' => 'Models',
32+
'request' => 'Http\Requests',
33+
'service' => 'Services',
34+
'middleware' => 'Http\Middleware',
35+
];
36+
2537
public function __construct()
2638
{
2739
$this->file = new Filesystem();
2840
$this->stubSupport = new StubSupport();
2941

42+
// TODO: add support for custom base path
3043
$this->basePath = realpath('./');
3144
}
3245

46+
/**
47+
* Create a new file
48+
*
49+
* @param string $makeCommand
50+
* @param string $makeValue
51+
*
52+
* @return bool|null
53+
*/
54+
public function make(string $makeCommand, string $makeValue): bool|null
55+
{
56+
$replacements = [];
57+
$composerJsonContent = json_decode($this->file->get("{$this->basePath}/composer.json"), true);
58+
59+
$namespaces = explode('/', $composerJsonContent['name']);
60+
$replacements['FULL_NAMESPACE'] = StrSupport::convertToPascalCase($namespaces[0]) . '\\' . StrSupport::convertToPascalCase($namespaces[1]);
61+
$replacements['PACKAGE_SLUG'] = Str::slug($namespaces[1]);
62+
63+
$parsedNamespace = PackagerSupport::parseNamespaceFrom($makeValue, $makeCommand, $this->relativePaths[$makeCommand]);
64+
$packageFolderPath = "{$this->basePath}/{$parsedNamespace['commandFolderPath']}";
65+
66+
if (!$this->file->exists($packageFolderPath)) {
67+
$this->file->makeDirectory($packageFolderPath, 0775, true);
68+
}
69+
70+
$targetPath = "{$this->basePath}/{$parsedNamespace['commandFolderPath']}/{$parsedNamespace['className']}.php";
71+
72+
if ($this->file->exists($targetPath)) {
73+
return null;
74+
}
75+
76+
$replacements['APPEND_NAMESPACE'] = $parsedNamespace['commandFolder'] ? "\\{$parsedNamespace['commandFolder']}" : '';
77+
$replacements['MAKE_CLASSNAME'] = $parsedNamespace['className'];
78+
$replacements['COMMAND_SLUG'] = Str::slug($parsedNamespace['classNameRaw']);
79+
80+
$stubPath = __DIR__ . "/../../resources/stubs/command/{$makeCommand}.php.stub";
81+
82+
$this->stubSupport->applyStub($stubPath, $targetPath, $replacements);
83+
84+
// TODO: append to service provider
85+
86+
return $this->file->exists($targetPath);
87+
}
88+
3389
/**
3490
* Create a new migration
3591
*

src/Supports/PackagerSupport.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,53 @@
44

55
use Illuminate\Support\Str;
66

7-
use LaravelReady\Packager\Supports\StringSupport;
7+
use LaravelReady\Packager\Supports\StrSupport;
88

99
class PackagerSupport
1010
{
1111
/**
1212
* Parse namespace from given string
1313
*
1414
* @param string $makeValue
15-
* @param string $makeFileType
15+
* @param string $commandType
1616
* @param string $relativePath
1717
*
1818
* @return array
1919
*/
20-
public static function parseNamespaceFrom(string $makeValue, string $makeFileType, string $relativePath): array
20+
public static function parseNamespaceFrom(string $makeValue, string $commandType, string $relativePath): array
2121
{
22+
$commandType = ucfirst($commandType);
23+
$makeValue = Str::replace("{$commandType}.php", '', $makeValue);
24+
$makeValue = Str::replace('.php', '', $makeValue);
25+
2226
$commandFolder = '';
23-
$makeClassName = $makeValue;
27+
$className = $makeValue;
2428

25-
if (Str::contains($makeValue, '\\')) {
26-
$makeClassName = last(explode('\\', $makeValue));
27-
$commandFolder = Str::replace("\\{$makeClassName}", '', $makeValue);
28-
} else if (Str::contains($makeValue, '/')) {
29-
$makeClassName = last(explode('/', $makeValue));
30-
$commandFolder = Str::replace("/{$makeClassName}", '', $makeValue);
29+
if (Str::contains($makeValue, '.')) {
30+
$makeValue = implode('\\', array_map(function ($part) {
31+
return StrSupport::convertToPascalCase($part);
32+
}, explode('.', $makeValue)));
3133
}
3234

33-
if (!Str::endsWith($makeClassName, $makeFileType)) {
34-
$makeClassName .= $makeFileType;
35+
if (Str::contains($makeValue, '\\')) {
36+
$className = last(explode('\\', $makeValue));
37+
$commandFolder = Str::replace("\\{$className}", '', $makeValue);
3538
}
3639

37-
$makeClassName = Str::replace('.php', '', $makeClassName);
38-
$makeClassName = StringSupport::correctClassName($makeClassName);
40+
$className = StrSupport::convertToPascalCase($className);
41+
$classNameRaw = $className;
42+
43+
if ($commandType !== 'model' && !Str::endsWith($className, $commandType)) {
44+
$className .= $commandType;
45+
}
3946

40-
// get path in package' src folder
4147
$makeFolder = "/src/{$relativePath}/{$commandFolder}";
4248

4349
return [
44-
'makeClassName' => $makeClassName,
45-
'makeFolder' => $commandFolder,
46-
'makeFolderPath' => $makeFolder,
50+
'classNameRaw' => $classNameRaw,
51+
'className' => $className,
52+
'commandFolder' => $commandFolder,
53+
'commandFolderPath' => $makeFolder,
4754
];
4855
}
4956

@@ -67,7 +74,8 @@ public static function getMigrationFilesCount(string $path): int
6774
* @return void
6875
* @throws \Exception
6976
*/
70-
public static function validateMigrationFileName(string $name): bool{
77+
public static function validateMigrationFileName(string $name): bool
78+
{
7179
return preg_match('/^[0-9]{4}_[0-9]{2}_[0-9]{2}_[0-9]{6}_[a-z_]+_table$/', $name);
7280
}
7381
}

0 commit comments

Comments
 (0)