Skip to content

Commit 0051503

Browse files
authored
Merge pull request #3 from WendellAdriel/feature/command
Command to Create Commands
2 parents cbdb617 + e4cce35 commit 0051503

File tree

13 files changed

+375
-101
lines changed

13 files changed

+375
-101
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.{yml,yaml,json}]
15+
indent_size = 2

.github/workflows/php.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ jobs:
1212
runs-on: ubuntu-latest
1313

1414
steps:
15-
- uses: actions/checkout@v2
15+
- uses: actions/checkout@v3
1616

1717
- name: Validate composer.json and composer.lock
1818
run: composer validate
1919

2020
- name: Install dependencies
2121
run: composer install --prefer-dist --no-progress
2222

23-
- name: Run test suite
24-
run: composer test
23+
- name: Check code style
24+
run: composer test:lint
2525

26-
- name: Run php-cs-fixer
27-
run: composer csfix .
26+
- name: Run test suite
27+
run: composer test:unit
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace librarianphp\Create;
6+
7+
use Minicli\Command\CommandController as MiniCliCommandController;
8+
use Minicli\Input;
9+
10+
class CommandController extends MiniCliCommandController
11+
{
12+
public function handle(): void
13+
{
14+
$input = new Input(' ');
15+
16+
$this->info('Command: ');
17+
$command = $input->read();
18+
19+
$this->createCommandFile($this->buildCommandPath($command), $command);
20+
}
21+
22+
private function buildCommandPath(string $command): array
23+
{
24+
$commandsPath = realpath($this->config->app_path[0]);
25+
$commandArray = explode(' ', $command);
26+
$commandPartsCount = count($commandArray);
27+
if ($commandPartsCount > 2) {
28+
$this->error('Command name must be one or two words.');
29+
30+
return [];
31+
}
32+
33+
$commandPath = [];
34+
35+
do {
36+
$commandPart = array_shift($commandArray);
37+
$commandPart = ucfirst(strtolower($commandPart));
38+
$commandPath[] = $commandPart;
39+
40+
if (count($commandArray) === 0 && $commandPartsCount > 1) {
41+
break;
42+
}
43+
44+
$dir = "{$commandsPath}/" . implode('/', $commandPath);
45+
if (! is_dir($dir)) {
46+
mkdir($dir);
47+
}
48+
} while (count($commandArray) > 0);
49+
50+
return array_map(fn ($item) => ucfirst(strtolower($item)), $commandPath);
51+
}
52+
53+
private function createCommandFile(array $commandPath, string $command): void
54+
{
55+
if ($commandPath === []) {
56+
return;
57+
}
58+
$commandsPath = realpath($this->config->app_path[0]);
59+
$commandName = count($commandPath) > 1 ? array_pop($commandPath) : 'Default';
60+
$commandClass = "{$commandName}Controller";
61+
$commandFilePath = realpath("{$commandsPath}/" . implode('/', $commandPath)) . "/{$commandClass}.php";
62+
63+
if (file_exists($commandFilePath)) {
64+
$this->error("Command file already exists at {$commandFilePath}");
65+
66+
return;
67+
}
68+
69+
$commandNamespace = 'namespace App\Command' . '\\' . implode('\\', $commandPath);
70+
$commandFileContent = file_get_contents(__DIR__ . '/../../stubs/command.stub');
71+
$commandFileContent = str_replace(
72+
['{{command_namespace}}', '{{command_class}}', '{{command_name}}'],
73+
[$commandNamespace, $commandClass, $command],
74+
$commandFileContent
75+
);
76+
77+
file_put_contents($commandFilePath, $commandFileContent);
78+
79+
$this->success("{$command} command created!");
80+
}
81+
}

Command/Create/ContentController.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace librarianphp\Create;
46

5-
use Minicli\FileNotFoundException;
6-
use Minicli\Stencil;
77
use Minicli\Command\CommandController;
8+
use Minicli\FileNotFoundException;
89
use Minicli\Input;
10+
use Minicli\Stencil;
911

1012
class ContentController extends CommandController
1113
{
@@ -14,41 +16,44 @@ class ContentController extends CommandController
1416
*/
1517
public function handle(): void
1618
{
17-
if (!$this->getApp()->config->has('stencil_dir')) {
18-
$this->error("You must define a stencil_dir config option.");
19+
if (! $this->getApp()->config->has('stencil_dir')) {
20+
$this->error('You must define a stencil_dir config option.');
21+
1922
return;
2023
}
2124

22-
if (!$this->getApp()->config->has('stencil_locations')) {
23-
$this->error("You must define a stencil_locations array config option.");
25+
if (! $this->getApp()->config->has('stencil_locations')) {
26+
$this->error('You must define a stencil_locations array config option.');
27+
2428
return;
2529
}
2630

2731
$args = $this->getArgs();
2832
$template_name = $args[3] ?? null;
29-
if (!$template_name) {
33+
if (! $template_name) {
3034
$template_name = 'post';
3135
}
3236

3337
$stencil = new Stencil($this->getApp()->config->stencil_dir);
3438

3539
$input = new Input(' ');
3640

37-
$this->info("Content Title: ");
41+
$this->info('Content Title: ');
3842
$title = $input->read();
3943

40-
$this->info("Content Description: ");
44+
$this->info('Content Description: ');
4145
$description = $input->read();
4246

4347
$content = $stencil->applyTemplate($template_name, [
4448
'title' => $title,
45-
'description' => $description
49+
'description' => $description,
4650
]);
4751

4852
$save_locations = $this->getApp()->config->stencil_locations;
4953

50-
if (!array_key_exists($template_name, $save_locations)) {
51-
$this->error("Save location not found for template $template_name");
54+
if (! array_key_exists($template_name, $save_locations)) {
55+
$this->error("Save location not found for template {$template_name}");
56+
5257
return;
5358
}
5459

@@ -57,7 +62,7 @@ public function handle(): void
5762
$file = fopen($path . '/' . $save_name, 'a+');
5863

5964
fwrite($file, $content);
60-
$this->info("Content generated at " . $path . '/' . $save_name);
65+
$this->info('Content generated at ' . $path . '/' . $save_name);
6166
}
6267

6368
public function slugify($title)
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace librarianphp\Create;
46

5-
use Minicli\App;
67
use Minicli\Command\CommandController;
78

89
class DefaultController extends CommandController
910
{
1011
public function handle(): void
1112
{
12-
$this->info("./librarian create [subcommand]", true);
13-
$this->info("Run \"./librarian create content\" to create a content file based on a template.");
13+
$this->info('./librarian create [subcommand]', true);
14+
$this->info('Run "./librarian create content" to create a content file based on a template.');
1415
}
1516
}

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
# command-create
1+
<div align="center">
2+
<h1>Command Create</h1>
3+
<h4>Librarian's built-in create command.</h4>
4+
</div>
25

3-
Librarian's built-in create command.
6+
## Commands Available
7+
8+
### Create Content
49

510
```shell
611
./librarian create content [template]
712
```
13+
14+
### Create Command
15+
16+
```shell
17+
./librarian create command
18+
```

composer.json

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
{
2-
"name": "librarianphp/command-create",
3-
"type": "library",
4-
"description": "Librarian's built-in command to create new content",
5-
"license": "MIT",
6-
"homepage": "https://github.com/librarianphp/command-demo",
7-
"keywords": ["cli","command-line", "markdown"],
8-
"autoload": {
9-
"psr-4": {
10-
"librarianphp\\": "Command/"
11-
}
12-
},
13-
"require": {
14-
"minicli/minicli": "^4.0",
15-
"minicli/stencil": "^0.1.1"
16-
},
17-
"require-dev": {
18-
"pestphp/pest": "^2.4",
19-
"friendsofphp/php-cs-fixer": "^3.16"
20-
},
21-
"scripts": {
22-
"test" : ["pest"],
23-
"csfix": ["php-cs-fixer fix"]
24-
},
25-
"config": {
26-
"allow-plugins": {
27-
"pestphp/pest-plugin": true
28-
}
2+
"name": "librarianphp/command-create",
3+
"type": "library",
4+
"description": "Librarian's built-in command to create new content",
5+
"license": "MIT",
6+
"homepage": "https://github.com/librarianphp/command-demo",
7+
"keywords": [
8+
"cli",
9+
"command-line",
10+
"markdown"
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"librarianphp\\": "Command/"
2915
}
16+
},
17+
"require": {
18+
"php": ">=8.1",
19+
"minicli/minicli": "^4.0",
20+
"minicli/stencil": "^0.1.1"
21+
},
22+
"require-dev": {
23+
"pestphp/pest": "^2.6",
24+
"friendsofphp/php-cs-fixer": "^3.17",
25+
"laravel/pint": "^1.10"
26+
},
27+
"scripts": {
28+
"lint": ["pint"],
29+
"test:lint": ["pint --test"],
30+
"test:unit": ["pest"],
31+
"test": [
32+
"@test:lint",
33+
"@test:unit"
34+
]
35+
},
36+
"config": {
37+
"allow-plugins": {
38+
"pestphp/pest-plugin": true
39+
}
40+
}
3041
}

0 commit comments

Comments
 (0)