:anger: PHP console applications for cool kids :anger:
Conso (PHP console applications for cool kids).
Conso is a simple, lightweight PHP package that helps you create (executable, .phar
, shareable) command line applications easily.
composer require lotfio/conso
composer test
commands.php
file.conso
file (you can change the name as you like).commands.php
file into conso
executable file.#!/usr/bin/env php
<?php declare(strict_types=1);
use Conso\{
Conso,Input,Output
};
require 'vendor/autoload.php';
$conso = new Conso(new Input, new Output);
// include your commands
require_once 'commands.php';
$conso->run();
<?php
$conso->setSignature(); // set application signature (top logo)
$conso->setName(); // set application name
$conso->setVersion(); // set application version
$conso->setAuthor(); // set application author
$conso->disableBuiltInCommands(); // disable builtin commands
test
command in your commands.php
:<?php
// this is your commands file
// test command
$conso->command("test", function($input, $output){
$output->writeLn("\n hello from test \n", 'red');
});
php conso --commands
or ./conso --commands
in your terminal and you should see your command.php conso test
or ./conso test
.->description(string $description)
;<?php
// test command
$conso->command("test", function($input, $output){
$output->writeLn("\n hello from test \n", 'red');
})->description("This is test command description :) ^^");
->sub(string|array $subCommand)
;<?php
// test command
$conso->command("test", function($input, $output){
if($input->subCommand() == 'one')
exit($output->writeLn("\n hello from one \n", 'yellow'));
if($input->subCommand() == 'two')
$output->writeLn("\n hello from two \n", 'green');
})->description("This is test command description :) ^^")->sub('one', 'two');
->flags(string|array $flag)
['-h', '--help', '-v', '--version', '-c', '--commands', '-q', '--quiet', '--ansi', '--no-ansi']
-vv or --verbose
flags to get more details about the error--flag=value
or -f=value
<?php
// test command
$conso->command("test", function($input, $output){
if($input->flag('-t') !== false)
$output->writeLn("\n flag -t is defined for this command.\n", 'red');
// you can get flag values
// $output->writeLn($input->flag('-t'));
})->description("This is test command description :) ^^")->flags('-t');
->alias(string $alias)
<?php
// test command
$conso->command("test", function($input, $output){
$output->writeLn("\n test called by alias \n", 'red');
})->description("This is test command description :) ^^")->alias('alias');
->help(array $help)
-h
or --help
flags<?php
// test command
$conso->command("test", function($input, $output){
$output->writeLn("\n test called by alias \n", 'red');
})->description("This is test command description :) ^^")->sub('one')->flags('-t')
->help([
"sub commands" => [
"one" => " help text for sub command goes here"
],
"flags" => [
"-t" => "help text for flag goes here"
]
]);
group()
method<?php
$conso->group('my group of commands:', function($conso){
$conso->command("command", function(){})->description('This is command description');
$conso->command("test", function(){})->description('This is command description');
$conso->command("make", function(){})->description('This is command description');
});
app/Commands
folder.commands.php
to app
folder to clean up things.psr-4{ "App\\" : "app" }
// add this to your conso file before run method
$conso->setCommandsPath('app/Commands');
$conso->setCommandsNamespace('App\\Commands');
php conso command:make {command name}
php conso command:make test
Test
command class like this:<?php
namespace App\Commands;
use Conso\{Conso, Command};
use Conso\Contracts\{CommandInterface,InputInterface,OutputInterface};
class Test extends Command implements CommandInterface
{
/**
* sub commands
*
* @var array
*/
protected $sub = [
];
/**
* flags
*
* @var array
*/
protected $flags = [
];
/**
* command help
*
* @var array
*/
protected $help = [
];
/**
* command description
*
* @var string
*/
protected $description = 'This is Test command description.';
/**
* execute method
*
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
public function execute(InputInterface $input, OutputInterface $output) : void
{
commandHelp($this->app->invokedCommand, $output);
}
}
commands.php
file:
$conso->command('test', Your\NameSpace\Test::class);
test
command will run the execute
method if no sub command is provided<?php
// test command
$conso->command("test", function($input, $output){
// get app config
$this->getName();
$this->getVersion();
$this->getAuthor();
$this->getCommandsPath();
$this->getCommandsNamespace();
// calling another command
$this->call('command:subcommand -f --flags');
});
<?php
/**
* execute method
*
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
public function execute(InputInterface $input, OutputInterface $output) : void
{
// get app config
$this->app->getName();
$this->app->getVersion();
$this->app->getAuthor();
$this->app->getCommandsPath();
$this->app->getCommandsNamespace();
// calling another command
$this->app->call('command:subcommand -f --flags');
}
namespace()
method which makes things cleaner<?php
$conso->namespace('Conso\\Commands', function($conso){
// all commands withing Conso\Commands namespace
$conso->command("command", Command::class);
$conso->command("test", Test::class);
$conso->command("make", Make::class);
});
<?php declare(strict_types=1);
use Conso\{
Conso,Input,Output
};
require 'vendor/autoload.php';
// you can sanitize and pass your command her
$command = 'command:make HttpCommand';
$input = new Input($command);
$conso = new Conso($input, new Output);
require 'commands.php';
$conso->run();
.phar
file :2.0
and above..phar
file use the built in compile
command.php conso compile:init
to create a conso.json
build file.{
"src": [ /* your pacakge directories to compile should be added here */
"src\/Conso",
"vendor" /* package dependencies if any */
],
"build": "build", /* build location */
"stub": "conso", /* stub file (the entry point of your phar) */
"phar": "conso.phar" /* output (your phar file name) */
}
<?php // no need for shebang it will be added automatically
declare(strict_types=1);
use Conso\{
Conso,Input,Output
};
require 'vendor/autoload.php';
$conso = new Conso(new Input, new Output);
$conso->setName("app name");
$conso->setVersion("2.0.0");
$conso->setSignature(" app signature ");
$conso->disableBuiltInCommands(); // disable conso built in commands
// include your commands
// require 'app/commands.php';
$conso->run();
php conso compile
and you will get your package compiled to a phar
file.--no-shebang
flag to avoid adding shebang to your phar
file (this is useful if you want to invoke your phar
file from http
)Conso\Testing\TestCase
test helper class for testing which helps you to :
Thank you for considering to contribute to Conso. All the contribution guidelines are mentioned Here.
If this project helped you reduce time to develop, you can give me a cup of coffee :) : Paypal.
Conso is an open-source software licensed under the MIT license.