A command line app is an application that can be run by a terminal. Here we have described a simple command line app with PHP.
To create a command we use the `$argv` global variable. If we run the following command in any terminal:
php ghanicmd create:folder ghani
Then the $argv variable will be as follows.
$argv[0] = ‘ghanicmd’;
$argv[1] = ‘create:folder’;
$argv[2] = ‘ghani’;
Here we have written a simple code that will create any file or folder if not exist already.
You can find the repository
here
Code :
namespace App\Ghani;
class Ghani
{
public function __construct()
{
$this->init();
}
public function init(){
/*
*
*
* $arv[0] = 'ghanicmd'
* $arg[1] = 'action:action-arg'
* $arg[2] = 'arg'
*
* */
global $argv;
if (count($argv) > 1) {
$arr = explode(':', $argv[1]);
switch ($arr[0]) {
case 'create': {
$this->create($arr);
break;
}
default: {
echo "could not found command $arr[0]";
break;
}
}
} else {
echo "Not enough arguments";
}
}
public function create($arr){
if (count($arr) == 2) {
$type = $arr[1];
switch ($type) {
case 'folder': {
$this->createFolder();
break;
}
default: {
echo "could not create $arr[1]";
}
}
} else {
echo "Not enough arguments";
}
}
public function createFolder()
{
global $argv;
if (count($argv) > 2) {
$folderName = $argv[2];
if (!file_exists($folderName)) {
if (!mkdir($folderName, 0777, true)) {
die('Failed to create directories...');
}
echo "Folder Created!";
} else {
echo "Folder Already There";
}
} else {
echo "Not enough arguments";
}
}
}
Code repository