Last active
March 25, 2021 19:58
-
-
Save michyaraque/d57d6200316de61af471b14e768b9612 to your computer and use it in GitHub Desktop.
Command Line variable control class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can set the parameters in any order | |
FILE ARGUMENT - VALUE | |
php example.php --user:"Michael Araque" --data:"Not register" | |
// Make is no set so it going to make throw event echoing the available parameters | |
php example.php --user:Foo --make:test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* CommandLine control for accept variables that user set at constructor | |
* | |
* PHP version 7.4 | |
* | |
* LICENSE: This source file is subject to version 3.0 of the GNU license | |
* that is available through the world-wide-web at the following URI: | |
* https://www.gnu.org/licenses/gpl-3.0.txt. If you did not receive a copy of | |
* the GNU License and are unable to obtain it through the web, please | |
* send a note to license@gnu.org so we can mail you a copy immediately. | |
* | |
* @category Scripting, Helpers, CommandLine, CLI | |
* @author Michael Araque https://t.me/michyaraque | |
* @since File available since Release 1.0.0 | |
* @datetime 23/03/2021 | |
*/ | |
class CommandLine { | |
/** | |
* @var array | |
*/ | |
private array $data = []; | |
/** | |
* @var array | |
*/ | |
private array $argvs = []; | |
/** | |
* @var string | |
*/ | |
public string $argument_symbol; | |
/** | |
* @var string | |
*/ | |
public string $argument_value; | |
/** | |
* @param array|null $arguments this argument need the ARGV that represent the CLI array | |
* @param array $available_parameters you can set your own parameters in a array | |
* @param string $argument_symbol this argument represents the symbol that the " $available_parameters " needs | |
*/ | |
public function __construct(?array $arguments = [], array $available_parameters = ['make', 'create'], string $argument_symbol = '--') { | |
$this->isCommandLineInterface(); | |
$this->argvs = $arguments; | |
$this->argument_symbol = $argument_symbol; | |
$this->constructParameters($available_parameters); | |
$this->deconstructParameters(); | |
} | |
/** | |
* Used to control if the request is from Browser or CLI | |
* @return void | |
*/ | |
private function isCommandLineInterface(): void { | |
try { | |
if(php_sapi_name() !== 'cli') { | |
throw new Exception("You have to use this script on command line"); | |
} | |
} catch (Exception $e) { | |
die($e->getMessage()); | |
} | |
} | |
/** | |
* @param array $parameters this argument receive a array of available_parameters | |
* | |
* @return void | |
*/ | |
private function constructParameters(array $parameters): void { | |
foreach($parameters as $parameter) { | |
$this->available_parameters[] = $this->argument_symbol . $parameter; | |
} | |
} | |
/** | |
* Split and get the value or not of the parameters | |
* @return void | |
*/ | |
private function deconstructParameters(): void { | |
foreach($this->argvs as $i => $argument) { | |
$arg_explode = explode(':', $argument); | |
if(!empty($arg_explode) && in_array($arg_explode[0], $this->available_parameters)){ | |
$this->data[$arg_explode[0]]['content'] = $arg_explode[1]; | |
} | |
} | |
$this->checkParameters($arg_explode); | |
} | |
/** | |
* @param mixed $content Check if any parameter is setted if not they return a message | |
* | |
* @return void | |
*/ | |
private function checkParameters($content): void { | |
if(!in_array($content[0], $this->available_parameters)) { | |
die("No valid parameter found, valid values: " . implode(', ', $this->available_parameters)); | |
} | |
} | |
/** | |
* @param string $argument this argument receive the unique parameter that you want to get the data | |
*/ | |
public function getParameter(string $argument = '') { | |
try { | |
$this->argument_value = $argument; | |
if(empty($argument)) { | |
throw new Exception("Argument not seted"); | |
} elseif(!empty($this->data[$argument]) && !empty($this->data[$argument]['content'])) { | |
return $this->data[$argument]['content']; | |
} else { | |
return false; | |
} | |
} catch (Exception $e) { | |
echo '[' . __FUNCTION__ . ']: ' . $e->getMessage(); | |
} | |
} | |
/** | |
* @return string|null get value of the parameter selected in getParameter function | |
*/ | |
public function getValue(): ?string { | |
if(!empty($this->argument_value)) { | |
return $this->data[$this->argument_value]['content']; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Import the library | |
require 'commandLine.php'; | |
/* Create a new instance of CommandLine class | |
* Argument [1]: must be $argv | |
* Argument [2]: you have to make your own array list of accepted parameters | |
* Argument [3]: symbol to affect directly the array list of argument 2 | |
*/ | |
$arguments = new CommandLine($argv, ['user', 'data'], '--'); | |
// Use if + instace of the CommandLine + getParameter to get true or false | |
// Argument [1]: must be one of the array list setted on the constructor | |
if($arguments->getParameter('--user')) { | |
// echo the value of the argument | |
echo $arguments->getValue(); | |
} | |
if($arguments->getParameter('--data')) { | |
echo $arguments->getValue(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment