-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement terminal for power shell and prompt command
- Loading branch information
1 parent
1f39034
commit f8a2147
Showing
5 changed files
with
567 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* Interface | ||
* | ||
* Interface of CrazyPHP | ||
* | ||
* PHP version 8.1.2 | ||
* | ||
* @package kzarshenas/crazyphp | ||
* @author kekefreedog <[email protected]> | ||
* @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 <[email protected]> | ||
* @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; | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ | |
* @author kekefreedog <[email protected]> | ||
* @copyright 2022-2024 Kévin Zarshenas | ||
*/ | ||
class Command{ | ||
class Command { | ||
|
||
/** Public static function | ||
****************************************************** | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* Terminal | ||
* | ||
* Library for manipulate command via terminal | ||
* | ||
* PHP version 8.1.2 | ||
* | ||
* @package kzarshenas/crazyphp | ||
* @author kekefreedog <[email protected]> | ||
* @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 <[email protected]> | ||
* @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; | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.