Skip to content

Commit 869792c

Browse files
committed
Initial version
1 parent bc55d60 commit 869792c

File tree

7 files changed

+142
-53
lines changed

7 files changed

+142
-53
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Librarian\Create;
4+
5+
use Minicli\Stencil;
6+
use Minicli\Command\CommandController;
7+
use Minicli\Input;
8+
9+
class ContentController extends CommandController
10+
{
11+
public function handle(): void
12+
{
13+
if (!$this->getApp()->config->has('stencil_dir')) {
14+
$this->getApp()->getPrinter()->error("You must define a stencil_dir config option.");
15+
return;
16+
}
17+
18+
if (!$this->getApp()->config->has('stencil_locations')) {
19+
$this->getApp()->getPrinter()->error("You must define a stencil_locations array config option.");
20+
return;
21+
}
22+
23+
$args = $this->getArgs();
24+
$template_name = $args[3] ?? null;
25+
if (!$template_name) {
26+
$template_name = 'post';
27+
}
28+
29+
$stencil = new Stencil($this->getApp()->config->stencil_dir);
30+
31+
$input = new Input(' ');
32+
33+
$this->getPrinter()->info("Content Title: ");
34+
$title = $input->read();
35+
36+
$this->getPrinter()->info("Content Description: ");
37+
$description = $input->read();
38+
39+
$content = $stencil->applyTemplate($template_name, [
40+
'title' => $title,
41+
'description' => $description
42+
]);
43+
44+
$save_locations = $this->getApp()->config->stencil_locations;
45+
46+
if (!array_key_exists($template_name, $save_locations)) {
47+
$this->getPrinter()->error("Save location not found for template $template_name");
48+
return;
49+
}
50+
51+
$path = $save_locations[$template_name];
52+
$save_name = date('Ymd') . '_' . $this->slugify($title) . '.md';
53+
$file = fopen($path . '/' . $save_name, 'a+');
54+
55+
fwrite($file, $content);
56+
$this->getPrinter()->info("Content generated at " . $path . '/' . $save_name);
57+
}
58+
59+
public function slugify($title)
60+
{
61+
$slug = strtolower($title);
62+
$slug = str_replace(' ', '-', $slug);
63+
64+
return preg_replace('/[^A-Za-z0-9\-]/', '', $slug);
65+
}
66+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Librarian\Create;
4+
5+
use Minicli\App;
6+
use Minicli\Command\CommandController;
7+
8+
class DefaultController extends CommandController
9+
{
10+
public function handle(): void
11+
{
12+
$this->getPrinter()->info("./librarian create [subcommand]", true);
13+
$this->getPrinter()->info("Run \"./librarian create content\" to create a content file based on a template.");
14+
}
15+
}

Command/Help/DefaultController.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
# command-demo
2-
demo command repository
1+
# command-create
2+
3+
Librarian's built-in create command.
4+
5+
```shell
6+
./librarian create content [template]
7+
```

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "librarianphp/command-help",
2+
"name": "librarianphp/command-create",
33
"type": "library",
4-
"description": "Librarian's built-in help command",
4+
"description": "Librarian's built-in command to create new content",
55
"license": "MIT",
66
"homepage": "https://github.com/librarianphp/command-demo",
77
"keywords": ["cli","command-line", "markdown"],
@@ -11,6 +11,7 @@
1111
}
1212
},
1313
"require": {
14-
"minicli/minicli": "^3.0"
14+
"minicli/minicli": "^3.0",
15+
"minicli/stencil": "^0.1.1"
1516
}
1617
}

composer.lock

Lines changed: 45 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

minicli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ $app = new App([
1414
'app_path' => [
1515
__DIR__ . '/Command'
1616
],
17+
'stencil_dir' => __DIR__ . '/../../librarian/app/Resources/stencil',
18+
'stencil_locations' => [
19+
'post' => __DIR__ . '/../../librarian/app/Resources/data/_p'
20+
],
1721
'debug' => true
1822
]);
1923

20-
$app->setSignature('./minicli help');
24+
$app->setSignature('./minicli create');
2125

2226
try {
2327
$app->runCommand($argv);

0 commit comments

Comments
 (0)