Skip to content

Commit bf130ec

Browse files
author
erika
committed
Initial version
1 parent 833f3e3 commit bf130ec

File tree

7 files changed

+193
-2
lines changed

7 files changed

+193
-2
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
vendor/
3+
composer.lock

README.md

+98-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,98 @@
1-
# application
2-
Application template repository
1+
# minicli
2+
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`.
5+
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+
```
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+
```
27+
cd myapp
28+
./minicli
29+
```
30+
31+
This will show you the default app signature:
32+
33+
```
34+
usage: ./minicli help
35+
```
36+
37+
The default `help` command that comes with minicli (`app/Command/Help/DefaultController.php`) auto-generates a tree of available commands:
38+
39+
```
40+
./minicli help
41+
```
42+
43+
```
44+
Available Commands
45+
46+
help
47+
└──test
48+
49+
```
50+
51+
The `help test` command, defined in `app/Command/Help/TestController.php`, shows an echo test of parameters:
52+
53+
```
54+
./minicli help test user=erika name=value
55+
```
56+
57+
```
58+
Hello, erika!
59+
60+
Array
61+
(
62+
[user] => erika
63+
[name] => value
64+
)
65+
```
66+
67+
### The simplest app
68+
69+
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:
70+
71+
```
72+
#!/usr/bin/php
73+
<?php
74+
75+
if (php_sapi_name() !== 'cli') {
76+
exit;
77+
}
78+
79+
require __DIR__ . '/vendor/autoload.php';
80+
81+
use Minicli\App;
82+
use Minicli\Command\CommandCall;
83+
84+
$app = new App();
85+
$app->setSignature('./minicli mycommand');
86+
87+
$app->registerCommand('mycommand', function(CommandCall $input) {
88+
echo "My Command!";
89+
90+
var_dump($input);
91+
});
92+
93+
$app->runCommand($argv);
94+
```
95+
96+
## Created with Minicli
97+
98+
- [Dolphin](https://github.com/do-community/dolphin) - a CLI tool for managing DigitalOcean servers with Ansible.

app/.gitkeep

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Command\Help;
4+
5+
use Minicli\App;
6+
use Minicli\Command\CommandController;
7+
use Minicli\Command\CommandRegistry;
8+
9+
class DefaultController extends CommandController
10+
{
11+
/** @var array */
12+
protected $command_map = [];
13+
14+
public function boot(App $app)
15+
{
16+
parent::boot($app);
17+
$this->command_map = $app->command_registry->getCommandMap();
18+
}
19+
20+
public function handle()
21+
{
22+
$this->getPrinter()->info('Available Commands');
23+
24+
foreach ($this->command_map as $command => $sub) {
25+
26+
$this->getPrinter()->newline();
27+
$this->getPrinter()->out($command, 'info_alt');
28+
29+
if (is_array($sub)) {
30+
foreach ($sub as $subcommand) {
31+
if ($subcommand !== 'default') {
32+
$this->getPrinter()->newline();
33+
$this->getPrinter()->out(sprintf('%s%s','└──', $subcommand));
34+
}
35+
}
36+
}
37+
$this->getPrinter()->newline();
38+
}
39+
40+
$this->getPrinter()->newline();
41+
$this->getPrinter()->newline();
42+
}
43+
}

app/Command/Help/TestController.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Command\Help;
4+
5+
use Minicli\Command\CommandController;
6+
7+
class TestController extends CommandController
8+
{
9+
public function handle()
10+
{
11+
$name = $this->hasParam('user') ? $this->getParam('user') : 'World';
12+
$this->getPrinter()->display(sprintf("Hello, %s!", $name));
13+
14+
print_r($this->getParams());
15+
}
16+
}

composer.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "minicli/application",
3+
"description": "Minicli Application Template",
4+
"license": "MIT",
5+
"homepage": "https://github.com/minicli/application",
6+
"keywords": ["cli","command-line"],
7+
"autoload": {
8+
"psr-4": {
9+
"App\\": "app/"
10+
}
11+
},
12+
"require": {
13+
"minicli/minicli": "^1.0"
14+
}
15+
}

minicli

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/php
2+
<?php
3+
4+
if (php_sapi_name() !== 'cli') {
5+
exit;
6+
}
7+
8+
require __DIR__ . '/vendor/autoload.php';
9+
10+
use Minicli\App;
11+
12+
$app = new App([
13+
'theme' => 'regular',
14+
'app_path' => __DIR__ . '/app/Command'
15+
]);
16+
17+
$app->runCommand($argv);

0 commit comments

Comments
 (0)