Skip to content

Commit a015cf8

Browse files
committed
update
1 parent 452ad92 commit a015cf8

28 files changed

+2203
-1647
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/art export-ignore

.github/workflows/php.yml .github/workflows/tests.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: PHP Composer
1+
name: Tests
22

33
on:
44
push:
@@ -29,5 +29,8 @@ jobs:
2929
- name: Install dependencies
3030
run: composer install --prefer-dist --no-progress
3131

32+
# - name: Run Pint
33+
# run: composer lint
34+
3235
- name: Run test suite
3336
run: composer run-script test

README.md

+62-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,64 @@
1-
# Minicli Application Template
1+
<div align="center">
2+
<p>
3+
<img src="https://github.com/WendellAdriel/miniterm/raw/main/art/minicli.png" alt="Minicli" width="48"/>
4+
<h1>MiniTerm</h1>
5+
Minicli Application Template powered with Termwind and Plates
6+
</p>
7+
</div>
28

3-
This repository is an application template for building command-line applications in PHP with [Minicli](https://github.com/minicli/minicli).
9+
This repository is an application template for building command-line applications in PHP with [Minicli](https://github.com/minicli/minicli), [Termwind](https://github.com/nunomaduro/termwind) and [Plates](https://github.com/thephpleague/plates).
410

5-
Please check [the official documentation](https://docs.minicli.dev) for more information on how to use this application template.
11+
Please check [the official documentation](https://docs.minicli.dev) for more information on how to use this application template.
12+
13+
14+
## Development
15+
16+
#### Requirements
17+
18+
- PHP 8.1+ (cli)
19+
- [Composer](https://getcomposer.org/)
20+
21+
## Installation
22+
23+
```bash
24+
# clone the repository
25+
26+
# install the dependencies
27+
composer install
28+
```
29+
30+
## Try it out
31+
32+
```bash
33+
# to run the app
34+
./minicli demo
35+
```
36+
37+
### Docker
38+
39+
#### Development
40+
41+
```bash
42+
# for creating the app container
43+
docker-compose up -d
44+
45+
# to install the dependencies
46+
docker-compose exec app composer install
47+
48+
# to run the app
49+
docker-compose exec app minicli your-custom-command
50+
```
51+
52+
#### Production
53+
```bash
54+
# Build
55+
docker build -t yourname/myapp-minicli .
56+
57+
# Run
58+
docker run --rm
59+
60+
# Tag
61+
docker tag yourname/myapp-minicli ghcr.io/yourname/myapp-minicli:latest
62+
63+
yourname/myapp-minicli your-custom-command
64+
```

app/.gitkeep

-1
This file was deleted.

app/Command/BaseController.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Command;
4+
5+
use Closure;
6+
use Minicli\Command\CommandController;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Termwind\Terminal;
9+
use Termwind\ValueObjects\Style;
10+
11+
abstract class BaseController extends CommandController
12+
{
13+
protected function render(string $content, int $options = OutputInterface::OUTPUT_NORMAL): void
14+
{
15+
$this->getApp()->termwind->render($content, $options);
16+
}
17+
18+
protected function style(string $name, Closure $callback = null): Style
19+
{
20+
return $this->getApp()->termwind->style($name, $callback);
21+
}
22+
23+
protected function terminal(): Terminal
24+
{
25+
return $this->getApp()->termwind->terminal();
26+
}
27+
28+
protected function ask(string $question, iterable $autocomplete = null): mixed
29+
{
30+
return $this->getApp()->termwind->ask($question, $autocomplete);
31+
}
32+
33+
protected function view(string $template, array $data = []): void
34+
{
35+
$app = $this->getApp();
36+
37+
$app->termwind->render($app->plates->view($template, $data));
38+
}
39+
}

app/Command/Demo/AskController.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Command\Demo;
4+
5+
use App\Command\BaseController;
6+
7+
class AskController extends BaseController
8+
{
9+
public function handle(): void
10+
{
11+
$name = $this->ask(<<<HTML
12+
<span class="mt-1 ml-2 mr-1 bg-green px-1 text-black">
13+
What is your name?
14+
</span>
15+
HTML);
16+
17+
$this->render(<<<HTML
18+
<div class="py-2">
19+
<div class="px-1 bg-green-600">MiniTerm</div>
20+
<em class="ml-1">
21+
Hello, {$name}
22+
</em>
23+
</div>
24+
HTML);
25+
}
26+
}

app/Command/Demo/ColorController.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Command\Demo;
4+
5+
use App\Command\BaseController;
6+
7+
class ColorController extends BaseController
8+
{
9+
public function handle(): void
10+
{
11+
$this->getPrinter()->display("Hello World");
12+
$this->getPrinter()->error("Hello World");
13+
$this->getPrinter()->info("Hello World");
14+
$this->getPrinter()->success("Hello World");
15+
$this->getPrinter()->warning("Hello World");
16+
$this->getPrinter()->display("Hello World", true);
17+
$this->getPrinter()->error("Hello World", true);
18+
$this->getPrinter()->info("Hello World", true);
19+
$this->getPrinter()->success("Hello World", true);
20+
$this->getPrinter()->warning("Hello World", true);
21+
$this->getPrinter()->out("Hello World!\r\n", 'underline');
22+
$this->getPrinter()->out("Hello World!\r\n", 'dim');
23+
$this->getPrinter()->out("Hello World!\r\n", 'bold');
24+
$this->getPrinter()->out("Hello World!\r\n", 'invert');
25+
$this->getPrinter()->out("Hello World!\r\n", 'italic');
26+
}
27+
}

app/Command/Demo/ConfigController.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Command\Demo;
4+
5+
use App\Command\BaseController;
6+
7+
class ConfigController extends BaseController
8+
{
9+
public function handle(): void
10+
{
11+
$configValue = $this->getApp()->config->miniCliEnv ;
12+
13+
$this->render(<<<HTML
14+
<div class="py-2">
15+
<div class="px-1 bg-green-600">MiniTerm</div>
16+
<em class="ml-1">
17+
Hello, {$configValue}
18+
</em>
19+
</div>
20+
HTML);
21+
}
22+
}

app/Command/Demo/DefaultController.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
namespace App\Command\Demo;
44

5-
use Minicli\App;
6-
use Minicli\Command\CommandController;
5+
use App\Command\BaseController;
76

8-
class DefaultController extends CommandController
7+
class DefaultController extends BaseController
98
{
109
public function handle(): void
1110
{
12-
$this->getPrinter()->info('Run ./minicli help for usage help.');
11+
$this->render(<<<HTML
12+
<div class="py-2">
13+
<div class="px-1 bg-cyan-600">INFO</div>
14+
<span class="ml-1">
15+
Run <span class="font-bold italic">./minicli help</span> for usage help.
16+
</span>
17+
</div>
18+
HTML);
1319
}
14-
}
20+
}

app/Command/Demo/HelloController.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Command\Demo;
4+
5+
use App\Command\BaseController;
6+
7+
class HelloController extends BaseController
8+
{
9+
public function handle(): void
10+
{
11+
$name = $this->hasParam('user') ? $this->getParam('user') : 'World';
12+
13+
$this->render(<<<HTML
14+
<div class="py-2">
15+
<div class="px-1 bg-green-600">MiniTerm</div>
16+
<em class="ml-1">
17+
Hello, {$name}
18+
</em>
19+
</div>
20+
HTML);
21+
}
22+
}

app/Command/Demo/TableController.php

+11-14
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@
22

33
namespace App\Command\Demo;
44

5-
use Minicli\Command\CommandController;
6-
use Minicli\Output\Filter\ColorOutputFilter;
7-
use Minicli\Output\Helper\TableHelper;
5+
use App\Command\BaseController;
86

9-
class TableController extends CommandController
7+
class TableController extends BaseController
108
{
119
public function handle(): void
1210
{
13-
$this->getPrinter()->display('Testing Tables');
11+
$headers = ['Header 1', 'Header 2', 'Header 3'];
12+
$rows = [];
1413

15-
$table = new TableHelper();
16-
$table->addHeader(['Header 1', 'Header 2', 'Header 3']);
17-
18-
for($i = 1; $i <= 10; $i++) {
19-
$table->addRow([(string)$i, (string)rand(0, 10), "other string $i"]);
14+
for ($i = 1; $i <= 10; $i++) {
15+
$rows[] = [(string) $i, (string) rand(0, 10), "other string $i"];
2016
}
2117

22-
$this->getPrinter()->newline();
23-
$this->getPrinter()->rawOutput($table->getFormattedTable(new ColorOutputFilter()));
24-
$this->getPrinter()->newline();
18+
$this->view('table', [
19+
'headers' => $headers,
20+
'rows' => $rows
21+
]);
2522
}
26-
}
23+
}

app/Command/Demo/TestController.php

-16
This file was deleted.

app/Command/Todos/ListController.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace App\Command\Todos;
4+
5+
use App\Command\BaseController;
6+
7+
class ListController extends BaseController
8+
{
9+
public $baseApi = 'https://jsonplaceholder.typicode.com/todos';
10+
11+
public function handle(): void
12+
{
13+
// if (empty($this->hasParam('page')) || empty($this->hasParam('limit'))) {
14+
// $this->getPrinter()->error("Please, inform the name of Pokemon you want to fetch info.");
15+
// $this->getPrinter()->error("Usage: ./minicli demo todos page=\"1\" limit=\"5\" ");
16+
// return;
17+
// }
18+
19+
$page = $this->getParam('page') ?? 1;
20+
$limit = $this->getParam('limit') ?? 5;
21+
22+
$this->getPrinter()->info('Showing todos');
23+
$this->getPrinter()->newline();
24+
$this->getPrinter()->display("Page: $page, Limit: $limit");
25+
$this->fetchPokemonInfo($page, $limit);
26+
}
27+
28+
public function fetchPokemonInfo(string $page, string $limit): void
29+
{
30+
$pokemonInfo = $this->get($this->baseApi . "?_page=$page&_limit=$limit");
31+
32+
if (is_string($pokemonInfo)) {
33+
$this->printPokemonTableInfo($pokemonInfo);
34+
}
35+
}
36+
37+
public function printPokemonTableInfo(string $info): void
38+
{
39+
try {
40+
$rows = json_decode($info, true);
41+
42+
$headers = ['User ID', 'ID', 'Title', 'Completed'];
43+
44+
$this->view('table', ['headers' => $headers, 'rows' => $rows]);
45+
46+
} catch (\Throwable $th) {
47+
if ($this->getApp()->config->debug) {
48+
$this->getPrinter()->error("An error occurred:");
49+
$this->getPrinter()->error($th->getMessage());
50+
}
51+
return;
52+
}
53+
}
54+
55+
public function get($url): string|bool
56+
{
57+
$ch = curl_init($url);
58+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
59+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
60+
61+
$response = curl_exec($ch);
62+
curl_close($ch);
63+
64+
return $response;
65+
}
66+
}

0 commit comments

Comments
 (0)