"3[39m", "black" => "3[30m", "red" => "3[31m", "green" => "3[32m", "yellow" => "3[33m", "blue" => "3[34m", "magenta" => "3[35m", "cyan" => "3[36m", "light-gray" => "3[37m", "dark-gray" => "3[90m", "light-red" => "3[91m", "light-green" => "3[92m", "light-yellow" => "3[93m", "light-blue" => "3[94m", "light-magenta" => "3[95m", "light-cyan" => "3[96m", "white" => "3[97m", ]; /** * Constructeur */ public function __construct(){} /** * Affiche une chaîne */ public function print($string, $httpHeaders=array()) { // Vérifier si le script s’exécute en mode CLI if(defined('STDIN')){ // Affichage dans la console print_r($string . PHP_EOL); } else { // Sinon, gérer la sortie pour le navigateur et les en-têtes if (!headers_sent()) { header_remove('Set-Cookie'); if (is_array($httpHeaders) && count($httpHeaders)) { foreach ($httpHeaders as $httpHeader) { header($httpHeader); } } if(is_array($string) || is_object($string)){ $string = json_encode($string, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); } echo $string; exit; } } } /** * Affiche une chaîne colorée */ public function set($string, $color = 'default'){ if(defined('STDIN') && isset($this->Colors[$color])){ return $this->Colors[$color] . $string . $this->Colors['default']; } else { return $string; } } public function error($string) { $this->print($this->set($string, 'red')); } public function success($string) { $this->print($this->set($string, 'green')); } public function warning($string) { $this->print($this->set($string, 'yellow')); } public function info($string) { $this->print($this->set($string, 'cyan')); } /** * Affiche l’aide en ligne de commande */ public function help($string = null) { // Vérifier si le script s’exécute en mode CLI if(!defined('STDIN')){ return; } // Récupérer les variables globales global $REQUEST, $CONFIG; // Récupérer les arguments $arguments = $REQUEST->getArguments(); // Variables initiales $file = $arguments[0] ?? null; $command = $arguments[1] ?? null; $action = $arguments[2] ?? null; // Si une chaîne est fournie, l’afficher if($string){ $this->print($string); } // Tenter de localiser une commande spécifique if($command){ $path = $CONFIG->root() . "/Command/" . ucfirst($command) . "Command" . ".php"; if(!is_file($path)){ $path = $CONFIG->root() . "/lib/plugins/" . ucfirst($command) . "/Command.php"; if(!is_file($path)){ $command = null; } } } if($command){ if(!class_exists(ucfirst($command) . "Command")){ require_once $path; if(!class_exists(ucfirst($command) . "Command")){ $command = null; } } } if($command){ // Si une commande est trouvée, afficher l’utilisation pour cette commande $class = ucfirst($command) . "Command"; $class = new $class(); if(!method_exists($class, $action . "Action")){ $action = null; } $this->print("Usage: $file $command [action] [options]"); $this->print("Actions disponibles :"); foreach(get_class_methods($class) as $method){ if(substr($method,-6) == 'Action'){ $this->print(" - " . strtolower(str_replace('Action','',$method))); } } } else { // Aide générale $this->print("Usage: $file [command] [action] [options]"); $this->print("Commandes disponibles :"); // Depuis /Command/ foreach(scandir($CONFIG->root() . "/Command/") as $command){ if(str_contains($command, 'Command.php')){ $this->print(" - " . strtolower(str_replace('Command.php','',$command))); } } // Depuis /lib/plugins/ foreach(scandir($CONFIG->root() . "/lib/plugins/") as $command){ if(is_file($CONFIG->root() . "/lib/plugins/" . $command . "/Command.php")){ $this->print(" - " . strtolower($command)); } } } // Arrêter l’exécution exit(); } }