diff --git a/src/Interface/CrazyTerminal.php b/src/Interface/CrazyTerminal.php new file mode 100644 index 0000000..bb367e8 --- /dev/null +++ b/src/Interface/CrazyTerminal.php @@ -0,0 +1,81 @@ + + * @copyright 2022-2024 Kévin Zarshenas + */ +namespace CrazyPHP\Interface; + +/** + * Dependances + */ + +/** + * Crazy Router Type Interface + * + * Interface for define compatible your terminal + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2024 Kévin Zarshenas + */ +interface CrazyTerminal { + + /** Public static function + ****************************************************** + */ + + /** + * Is + * + * Check if is curent terminal + * + * @return bool + */ + public static function is():bool; + + /** Public static function | Get Command + ****************************************************** + */ + + /** + * Command Set Env + * + * Get command to set Env based on key value + * + * @param string $key + * @param mixed $value + * @return string + */ + public static function commandSetEnv(string $key, mixed $value):string; + + /** + * Command Chain + * + * @param string[] $commands + * @return string + */ + public static function commandChain(...$commands):string; + + /** Public static function | Action + ****************************************************** + */ + + /** + * Run + * + * Run command given + * + * @param string $command + * @param bool $liveResult + * @return mixed + */ + public static function run(string $command, bool $liveResult = true):mixed; + +} \ No newline at end of file diff --git a/src/Library/Cli/Command.php b/src/Library/Cli/Command.php index 4903687..8150d1f 100644 --- a/src/Library/Cli/Command.php +++ b/src/Library/Cli/Command.php @@ -28,7 +28,7 @@ * @author kekefreedog * @copyright 2022-2024 Kévin Zarshenas */ -class Command{ +class Command { /** Public static function ****************************************************** diff --git a/src/Library/System/Terminal/CommandPrompt.php b/src/Library/System/Terminal/CommandPrompt.php new file mode 100644 index 0000000..6fcc58b --- /dev/null +++ b/src/Library/System/Terminal/CommandPrompt.php @@ -0,0 +1,157 @@ + + * @copyright 2022-2024 Kévin Zarshenas + */ +namespace CrazyPHP\Library\System\Terminal; + +/** Dependances + * + */ +use CrazyPHP\Interface\CrazyTerminal; +use CrazyPHP\Library\Cli\Command; + +/** + * Powershell + * + * Methods for interacting with Command Prompt + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2024 Kévin Zarshenas + */ +class CommandPrompt implements CrazyTerminal { + + /** Public static function + ****************************************************** + */ + + /** + * Is + * + * Check if is curent terminal + * + * @return bool + */ + public static function is():bool { + + # Set result + $result = false; + + # Check for the ComSpec environment variable typical in CMD that includes 'cmd.exe' + $comSpec = getenv('ComSpec'); + + # Return check + $result = ($comSpec !== false && strpos(strtolower($comSpec), 'cmd.exe') !== false); + + # Return result + return $result; + + } + + /** Public static function | Command + ****************************************************** + */ + + /** + * Command Set Env + * + * Get command to set Env based on key value + * + * @param string $key + * @param mixed $value + * @return string + */ + public static function commandSetEnv(string $key, mixed $value):string { + + # Set result + $result = ""; + + # Check if the value is boolean and convert it to the correct Command Prompt string representation + if(is_bool($value)) + + # Set value + $value = $value ? 'True' : 'False'; + + # Escape percent signs for strings in Command Prompt + $escapedValue = str_replace('%', '%%', $value); + + # Set uppercase on key + $key = strtoupper($key); + + # Format the command to set the environment variable in Command Prompt + $result = "set $key=$escapedValue"; + + # Return result + return $result; + + } + + /** + * Command Chain + * + * @param string $command + * @return string + */ + public static function commandChain(...$commands):string { + + # Set array temp + $arrayTemp = []; + + # Set result + $result = ""; + + # Check commands + if(!empty($commands)) + + # Iteration commands + foreach ($commands as $command) + + # Check command is sting + if(is_string($command)) + + # Append in result + $arrayTemp[] = $command; + + # Set result + $result = implode(" && ", $arrayTemp); + + # Return result + return $result; + + } + + /** Public static function | Action + ****************************************************** + */ + + /** + * Run + * + * Run command given + * + * @param string $command + * @param bool $liveResult + * @return mixed + */ + public static function run(string $command, bool $liveResult = true):mixed { + + # Prepare command + $cmdCommand = "cmd /c " . escapeshellarg($command); + + # Set result + $result = Command::exec($cmdCommand, "", $liveResult); + + # Return result + return $result; + + } + +} \ No newline at end of file diff --git a/src/Library/System/Terminal/Powershell.php b/src/Library/System/Terminal/Powershell.php new file mode 100644 index 0000000..f23e027 --- /dev/null +++ b/src/Library/System/Terminal/Powershell.php @@ -0,0 +1,155 @@ + + * @copyright 2022-2024 Kévin Zarshenas + */ +namespace CrazyPHP\Library\System\Terminal; + +/** Dependances + * + */ +use CrazyPHP\Interface\CrazyTerminal; +use CrazyPHP\Library\Cli\Command; + +/** + * Powershell + * + * Methods for interacting with Powershell + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2024 Kévin Zarshenas + */ +class Powershell implements CrazyTerminal { + + /** Public static function + ****************************************************** + */ + + /** + * Is + * + * Check if is curent terminal + * + * @return bool + */ + public static function is():bool { + + # Set result + $result = false; + + # Check for a common PowerShell environment variable + $result = getenv('PSModulePath') !== false; + + # Return result + return $result; + + } + + /** Public static function | Command + ****************************************************** + */ + + /** + * Command Set Env + * + * Get command to set Env based on key value + * + * @param string $key + * @param mixed $value + * @return string + */ + public static function commandSetEnv(string $key, mixed $value):string { + + # Set result + $result = ""; + + # Check value is bool + if(is_bool($value)) + + # Set value + $value = $value ? '$true' : '$false'; + + else{ + + # Set value + $value = str_replace("'", "''", $value); + + # Set value + $value = "'$value'"; + + } + + # Set uppercase on key + $key = strtoupper($key); + + # Format the command to set the environment variable in PowerShell + $result = "\$env:$key = $value"; + + # Return result + return $result; + + } + + /** + * Command Chain + * + * @param string $command + * @return string + */ + public static function commandChain(...$commands):string { + + # Set result + $result = ""; + + # Check commands + if(!empty($commands)) + + # Iteration commands + foreach ($commands as $command) + + # Check command is sting + if(is_string($command)) + + # Append in result + $result .= $command . "; "; + + # Return result + return rtrim($result); + + } + + /** Public static function | Action + ****************************************************** + */ + + /** + * Run + * + * Run command given + * + * @param string $command + * @param bool $live + * @return mixed + */ + public static function run(string $command, bool $liveResult = true):mixed { + + # Prepare command + $psCommand = "powershell -command " . escapeshellarg($command); + + # Set result + $result = Command::exec($psCommand, "", $liveResult); + + # Return result + return $result; + + } + +} \ No newline at end of file diff --git a/tests/Library/System/TerminalTest.php b/tests/Library/System/TerminalTest.php new file mode 100644 index 0000000..a6f385b --- /dev/null +++ b/tests/Library/System/TerminalTest.php @@ -0,0 +1,173 @@ + + * @copyright 2022-2024 Kévin Zarshenas + */ +namespace Tests\Library\File; + +/** + * Dependances + */ +use PHPUnit\Framework\TestCase; +use CrazyPHP\Model\Env; +use PHPUnit\TextUI\Configuration\Constant; + +/** + * Page Test + * + * Methods for test terminal classes + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2024 Kévin Zarshenas + */ +class TerminalTest extends TestCase { + + /** Public constant + ****************************************************** + */ + + /* Value and result */ + public const SET_ENV = [ + [ + "_command" => "commandSetEnv", + "commandPrompt" => [ + "input" => ["key", "value"], + "result" => "set KEY=value" + ], + "powershell" => [ + "input" => ["key", "value"], + "result" => "\$env:KEY = 'value'" + ], + ], + [ + "_command" => "commandSetEnv", + "commandPrompt" => [ + "input" => ["key", true], + "result" => "set KEY=True" + ], + "powershell" => [ + "input" => ["key", true], + "result" => "\$env:KEY = \$true" + ], + ], + [ + "_command" => "commandChain", + "commandPrompt" => [ + "input" => [ + "echo Starting process...", + "set DATE_VAR=%date%", + "echo Today's date is: %DATE_VAR%" + ], + "result" => "echo Starting process... && set DATE_VAR=%date% && echo Today's date is: %DATE_VAR%" + ], + "powershell" => [ + "input" => [ + "Write-Output 'Starting process...'", + "\$date = Get-Date", + "Write-Output \"The current date and time is: \$date\"" + ], + "result" => "Write-Output 'Starting process...'; \$date = Get-Date; Write-Output \"The current date and time is: \$date\";" + ], + ], + ]; + + /** Private method | Preparation + ****************************************************** + */ + + /** + * Get Terminals + * + * @return array + */ + private function _getTerminals():array { + + # Set result + $result = [ + "commandPrompt" => "\CrazyPHP\Library\System\Terminal\CommandPrompt", + "powershell" => "\CrazyPHP\Library\System\Terminal\Powershell" + ]; + + # Retun result + return $result; + + } + + /** Public method | Preparation + ****************************************************** + */ + + /** + * Set Up Before Class + * + * This method is called before the first test of this test class is run. + * + * @return void + */ + public static function setUpBeforeClass():void { + + # Setup env + Env::set([ + # App root for composer class + "crazyphp_root" => getcwd(), + "phpunit_test" => true, + ]); + + } + + + /** + * Tear Down After Class + * + * This method is called after the last test of this test class is run. + * + * @return void + */ + public static function tearDownAfterClass():void { + + # Reset env variables + Env::reset(); + + } + + /** Public method | Tests + ****************************************************** + */ + + /** + * Test Command Set Env + * + * @return void + */ + public function testCommandSetEnv():void { + + # Get terminals + $terminals = $this->_getTerminals(); + + # Iteration terminals + foreach($terminals as $terminalName => $terminal){ + + # Iteration key value + foreach(self::SET_ENV as $data){ + + # Get result + $result = call_user_func_array([$terminal, $data["_command"]], $data[$terminalName]["input"]); + + # Asset + $this->assertEquals($data[$terminalName]["result"], $result); + + } + + } + + } + +} \ No newline at end of file