Skip to content

Commit

Permalink
Implement terminal for power shell and prompt command
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Apr 20, 2024
1 parent 1f39034 commit f8a2147
Show file tree
Hide file tree
Showing 5 changed files with 567 additions and 1 deletion.
81 changes: 81 additions & 0 deletions src/Interface/CrazyTerminal.php
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;

}
2 changes: 1 addition & 1 deletion src/Library/Cli/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @author kekefreedog <[email protected]>
* @copyright 2022-2024 Kévin Zarshenas
*/
class Command{
class Command {

/** Public static function
******************************************************
Expand Down
157 changes: 157 additions & 0 deletions src/Library/System/Terminal/CommandPrompt.php
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;

}

}
Loading

0 comments on commit f8a2147

Please sign in to comment.