Command-Line interface that can be used to execute commands written in PHP.
Note: Commands included in this package (excluding help command) were written for WordPress-MVC.
Create a php file that will be called in command-line, Sample, and include the following code lines:
use Ayuco\Listener;
Create a listener:
$ayuco = new Listener();
// or without use
$ayuco = new Ayuco\Listener()
Register your commands.
$ayuco->register($command1)
->register($command2)
->register(new MyCommand);
Start interpreting or listening:
$ayuco->interpret();
Use in command line:
php filename command_key arguments
If filename
is named ayuco.php
and command_key
is clear_cache
, command in command-line will be:
php ayuco.php clear_cache
Send arguments right after the command_key
, for example:
php ayuco.php cache clear
In the example above, cache
will be the command key and clear
will be an argument (in order, it will be $arg[2]
).
Send arguments as command options with the prefix --
, for example:
php ayuco.php cache clear --debug --note="Cache clear note"
Create your own class command by extending from Ayuco base command class:
use Ayuco\Command;
class MyCommand extends Command
{
protected $key = 'command_key';
protected $description = 'My command description.';
public function call($args = [])
{
// TODO command action.
}
}
Example for a clear cache command.
use Ayuco\Command;
class ClearCacheCommand extends Command
{
protected $key = 'clear_cache';
protected $description = 'Clears system cache.';
public function call($args = [])
{
Cache::flush(); // Example
}
}
Registration in listener would be:
$ayuco->register(new ClearCacheCommand);
For this command:
php ayuco.php cache clear
The arguments can be accessed like:
use Ayuco\Command;
class CacheCommand extends Command
{
protected $key = 'cache';
public function call($args = [])
{
// ayuco.php
$args[0];
// cache
$args[1];
// clear
$args[2];
}
}
For this command:
php ayuco.php cache clear --debug --note="Cache clear note"
The options can be accessed like:
use Ayuco\Command;
class CacheCommand extends Command
{
protected $key = 'cache';
public function call($args = [])
{
// ayuco.php
$args[0];
// cache
$args[1];
// clear
$args[2];
// --debug
$this->options['debug'];
// --note="..."
$this->options['note'];
}
}
Change the coloring of the output printed in the console using class Ayuco\Coloring
static method apply()
:
use Ayuco\Coloring;
use Ayuco\Command;
class ColoringCommand extends Command
{
public function call($args = [])
{
$this->_print(Coloring::apply('red', 'Print this message in red.'));
}
}
You can read more about coloring here.
AYUCO automatically will register its own help
command. This command can be used to display in command-line
the list of registered commands, use it like:
php ayuco.php help
- PHP >= 5.4
PSR-4.
The MIT License (MIT)
Copyright (c) 2016 10Quality.