Skip to content

Commit 63b078b

Browse files
authored
Merge pull request #3 from minicli/update-minicli3
Updating to support Minicli 3
2 parents d4e1d40 + 6632c53 commit 63b078b

11 files changed

+3255
-140
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.idea
2+
.phpunit.result.cache
3+
.composer/
24
vendor/

README.md

+3-121
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,5 @@
1-
# minicli
1+
# Minicli Application Template
22

3-
[Minicli](https://github.com/minicli/minicli) is an experimental dependency-free toolkit for building CLI-only applications in PHP created by @erikaheidi.
4-
This repository is a template you can use to create a new application that has a single dependency: `minicli/minicli`.
3+
This repository is an application template for building command-line applications in PHP with [Minicli](https://github.com/minicli/minicli).
54

6-
### Why minicli
7-
8-
The current trend in software development is basing your project on a big pile of unknowns. There is nothing wrong in using third party software, but if more than 80% of your application is out of your control, things can get messy.
9-
What usually happens is that you don't even know what packages you're depending on, when using the most popular frameworks.
10-
11-
Minicli was created as [an educational experiment](https://dev.to/erikaheidi/bootstrapping-a-cli-php-application-in-vanilla-php-4ee) and a way to go dependency-free when building simple command-line applications in PHP. It can be used for microservices, personal dev tools, bots and little fun things.
12-
13-
14-
## Getting Started
15-
16-
You'll need `php-cli` and [Composer](https://getcomposer.org/) to get started.
17-
18-
Create a new project with:
19-
20-
```bash
21-
composer create-project --prefer-dist minicli/application myapp
22-
```
23-
24-
Once the installation is finished, you can run `minicli` it with:
25-
26-
```bash
27-
cd myapp
28-
./minicli
29-
```
30-
If that doesn't work for you, you may have to use instead:
31-
32-
```bash
33-
php minicli
34-
```
35-
This will show you the default app signature:
36-
37-
```bash
38-
usage: ./minicli help
39-
```
40-
41-
The default `help` command that comes with minicli (`app/Command/Help/DefaultController.php`) auto-generates a tree of available commands:
42-
43-
```bash
44-
./minicli help
45-
```
46-
47-
```bash
48-
Available Commands
49-
50-
help
51-
└──test
52-
53-
```
54-
55-
The `help test` command, defined in `app/Command/Help/TestController.php`, shows an echo test of parameters:
56-
57-
```bash
58-
./minicli help test user=erika name=value
59-
```
60-
61-
```bash
62-
Hello, erika!
63-
64-
Array
65-
(
66-
[user] => erika
67-
[name] => value
68-
)
69-
```
70-
71-
### The simplest app
72-
73-
The simplest minicli script doesn't require using Command Controllers at all. You can delete the `app` folder and use `registerCommand` with an anonymous function, like this:
74-
75-
```php
76-
#!/usr/bin/php
77-
<?php
78-
79-
if (php_sapi_name() !== 'cli') {
80-
exit;
81-
}
82-
83-
require __DIR__ . '/vendor/autoload.php';
84-
85-
use Minicli\App;
86-
use Minicli\Command\CommandCall;
87-
88-
$app = new App();
89-
$app->setSignature('./minicli mycommand');
90-
91-
$app->registerCommand('mycommand', function(CommandCall $input) {
92-
echo "My Command!";
93-
94-
var_dump($input);
95-
});
96-
97-
$app->runCommand($argv);
98-
```
99-
100-
## Tests
101-
To keep dependencies to a minimum, the `minicli/application` template repository doesn't require any specific testing framework, but we highly recommend using [PestPHP](https://pestphp.com), which is the testing framework used by [minicli/minicli](https://github.com/minicli/minicli).
102-
103-
### Bootstrapping Tests with PestPHP
104-
105-
To get started with PestPHP to test your Minicli application, first include the required dependencies with:
106-
107-
```bash
108-
composer require pestphp/pest --dev --with-all-dependencies
109-
```
110-
111-
Then, run the following command to create a tests folder and a couple example tests:
112-
113-
```bash
114-
./vendor/bin/pest --init
115-
```
116-
117-
Then you can use the following command to run the test suite:
118-
119-
```bash
120-
./vendor/bin/pest
121-
```
122-
123-
For more details on how to test your application with PestPHP, please refer to the [official documentation](https://pestphp.com/docs/writing-tests).
5+
Please check [the official documentation](https://docs.minicli.dev) for more information on how to use this application template.

app/Command/Help/DefaultController.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ class DefaultController extends CommandController
1010
/** @var array */
1111
protected $command_map = [];
1212

13-
public function boot(App $app)
13+
public function boot(App $app): void
1414
{
1515
parent::boot($app);
16-
$this->command_map = $app->command_registry->getCommandMap();
16+
$this->command_map = $app->commandRegistry->getCommandMap();
1717
}
1818

19-
public function handle()
19+
public function handle(): void
2020
{
2121
$this->getPrinter()->info('Available Commands');
2222

app/Command/Help/TableController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
class TableController extends CommandController
1010
{
11-
public function handle()
11+
public function handle(): void
1212
{
1313
$this->getPrinter()->display('Testing Tables');
1414

1515
$table = new TableHelper();
1616
$table->addHeader(['Header 1', 'Header 2', 'Header 3']);
1717

1818
for($i = 1; $i <= 10; $i++) {
19-
$table->addRow([$i, rand(0, 10), "other string $i"]);
19+
$table->addRow([(string)$i, (string)rand(0, 10), "other string $i"]);
2020
}
2121

2222
$this->getPrinter()->newline();

app/Command/Help/TestController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class TestController extends CommandController
88
{
9-
public function handle()
9+
public function handle(): void
1010
{
1111
$name = $this->hasParam('user') ? $this->getParam('user') : 'World';
1212
$this->getPrinter()->display(sprintf("Hello, %s!", $name));

composer.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
}
1111
},
1212
"require": {
13-
"minicli/minicli": "^2.0"
13+
"minicli/minicli": "^3.0"
14+
},
15+
"require-dev": {
16+
"pestphp/pest": "^1.21"
17+
},
18+
"scripts": {
19+
"test" : ["pest"]
1420
}
1521
}

0 commit comments

Comments
 (0)