Skip to content

Commit 98ee367

Browse files
committed
framework: use new Command Line Options API (hasn't been completed yet)
1 parent ecc0803 commit 98ee367

File tree

10 files changed

+135
-135
lines changed

10 files changed

+135
-135
lines changed

src/iTXTech/SimpleFramework/Console/Logger.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ class Logger{
2828
const INFO = "info";
2929
const DEBUG = "debug";
3030

31-
public static $noColor = false;
32-
public static $fullDisplay = true;
33-
public static $noOutput = false;
31+
public static $hasPrefix = true;
32+
public static $disableOutput = false;
33+
public static $disableClass = false;
3434

3535
private static $logfile = "";
3636
/** @var LoggerHandler */
@@ -153,28 +153,31 @@ public static function log($level, $message){
153153
}
154154

155155
public static function send(string $message, string $prefix, string $color){
156+
if(self::$disableOutput){
157+
return;
158+
}
156159
if(self::$loggerHandler !== ""){
157160
self::$loggerHandler::send($message, $prefix, $color);
158161
return;
159162
}
160-
if(self::$fullDisplay){
163+
if(self::$hasPrefix){
161164
$now = time();
162-
$class = @end(explode('\\', debug_backtrace()[2]['class']));
163-
if(strlen($class) > 20){
164-
$class = substr($class, 0, 20);
165+
$class = "Console";
166+
if(!self::$disableClass){
167+
$class = @end(explode("\\", debug_backtrace()[2]['class']));
168+
if(strlen($class) > 20){
169+
$class = substr($class, 0, 20);
170+
}
171+
$class = $class == "" ? "Console" : $class;
165172
}
166-
$class = $class == "" ? "Console" : $class;
167173
$message = TextFormat::toANSI(TextFormat::AQUA . "[" . date("G:i:s", $now) . "] " .
168-
TextFormat::RESET . $color . $class . "/" . $prefix . ">" . " " . $message . TextFormat::RESET);
174+
TextFormat::RESET . $color . "<" . $class . "/" . $prefix . ">" . " " . $message . TextFormat::RESET);
169175
}else{
170-
$message = TextFormat::toANSI($message);
176+
$message = TextFormat::toANSI($message . TextFormat::RESET);
171177
}
172178
$cleanMessage = TextFormat::clean($message);
173179

174-
if(self::$noOutput){
175-
return;
176-
}
177-
if(!Terminal::hasFormattingCodes() or self::$noColor){
180+
if(!Terminal::hasFormattingCodes()){
178181
echo $cleanMessage . PHP_EOL;
179182
}else{
180183
echo $message . PHP_EOL;

src/iTXTech/SimpleFramework/Console/Option/CommandLine.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function hasOption($option){
4040
$option = $this->resolveOption($option);
4141
}
4242
foreach($this->options as $opt){
43-
if($option == $opt){
43+
if($opt->equals($option)){
4444
return true;
4545
}
4646
}
@@ -82,7 +82,7 @@ public function getOptionValues($option){
8282
$values = [];
8383

8484
foreach($this->options as $processedOption){
85-
if($processedOption === $option){
85+
if($processedOption->equals($option)){
8686
$values = array_merge($values, $processedOption->getValues());
8787
}
8888
}

src/iTXTech/SimpleFramework/Console/Option/Option.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Option{
4242
* @param bool $hasArg
4343
* @param string $description
4444
*
45-
* @throws \Exception
45+
* @throws \InvalidArgumentException
4646
*/
4747
public function __construct($builder, string $longOpt = null, bool $hasArg = false, string $description = null){
4848
if($builder instanceof OptionBuilder){
@@ -362,4 +362,18 @@ public function requiresArg() : bool{
362362
}
363363
return $this->acceptsArg();
364364
}
365+
366+
public function equals(?Option $option) : bool{
367+
if($option === null){
368+
return false;
369+
}
370+
if($this->opt != null ? $this->opt !== $option->opt : $option->opt != null){
371+
return false;
372+
}
373+
if($this->longOpt != null ? $this->longOpt !== $option->longOpt : $option->longOpt != null){
374+
return false;
375+
}
376+
377+
return true;
378+
}
365379
}

src/iTXTech/SimpleFramework/Console/Option/OptionBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class OptionBuilder{
3131
*
3232
* @param $opt
3333
*
34-
* @throws \Exception
34+
* @throws \InvalidArgumentException
3535
*/
3636
public function __construct($opt){
3737
OptionValidator::validateOption($opt);
@@ -142,11 +142,11 @@ public function hasArgs(){
142142
* Constructs an Option with the values declared by this {@link Builder}.
143143
*
144144
* @return Option
145-
* @throws \Exception
145+
* @throws \InvalidArgumentException
146146
*/
147147
public function build() : Option{
148148
if($this->opt == null && $this->longOpt == null){
149-
throw new \Exception("Either opt or longOpt must be specified");
149+
throw new \InvalidArgumentException("Either opt or longOpt must be specified");
150150
}
151151
return new Option($this);
152152
}

src/iTXTech/SimpleFramework/Console/Option/OptionValidator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ abstract class OptionValidator{
2222
*
2323
* @param string $opt
2424
*
25-
* @throws \Exception
25+
* @throws \InvalidArgumentException
2626
*/
2727
public static function validateOption(string $opt){
2828
// if opt is NULL do not check further
@@ -35,13 +35,13 @@ public static function validateOption(string $opt){
3535
$ch = $opt{0};
3636

3737
if(!self::isValidOpt($ch)){
38-
throw new \Exception("Illegal option name '" . $ch . "'");
38+
throw new \InvalidArgumentException("Illegal option name '" . $ch . "'");
3939
}
4040
}else{
4141
for($i = 0; $i < strlen($opt); $i++){
4242
if(!self::isValidChar($opt{$i})){
43-
throw new \Exception("The option '" . $opt . "' contains an illegal "
44-
. "character : '" . $opt{$i} . "'");
43+
throw new \InvalidArgumentException("The option '" . $opt .
44+
"' contains an illegal character : '" . $opt{$i} . "'");
4545
}
4646
}
4747
}

src/iTXTech/SimpleFramework/Console/Option/Parser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct(){
5151
* @param array|null $properties
5252
*
5353
* @return CommandLine
54-
* @throws \Exception
54+
* @throws ParseException
5555
*/
5656
public function parse(Options $options, array $arguments = null,
5757
bool $stopAtNonOption = false, array $properties = null) : CommandLine{
@@ -116,7 +116,7 @@ private function handleProperties(?array $properties){
116116
}
117117

118118
/**
119-
* @throws \Exception
119+
* @throws MissingOptionException
120120
*/
121121
protected function checkRequiredOptions(){
122122
if(!empty($this->expectedOpts)){

src/iTXTech/SimpleFramework/Console/Terminal.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace iTXTech\SimpleFramework\Console;
1818

19+
use iTXTech\SimpleFramework\Util\StringUtil;
1920
use iTXTech\SimpleFramework\Util\Util;
2021

2122
abstract class Terminal{
@@ -48,13 +49,11 @@ abstract class Terminal{
4849

4950
public static function hasFormattingCodes(){
5051
if(self::$formattingCodes === null){
51-
$opts = getopt("", ["enable-ansi", "disable-ansi"]);
52-
if(isset($opts["disable-ansi"])){
53-
self::$formattingCodes = false;
54-
}else{
55-
self::$formattingCodes = ((Util::getOS() !== "win" and getenv("TERM") != "" and
56-
(!function_exists("posix_ttyname") or !defined("STDOUT")
57-
or posix_ttyname(STDOUT) !== false)) or isset($opts["enable-ansi"]));
52+
if((Util::getOS() !== Util::OS_WINDOWS and getenv("TERM") != "" and
53+
(!function_exists("posix_ttyname") or !defined("STDOUT")
54+
or posix_ttyname(STDOUT) !== false)) or
55+
(Util::getOS() === Util::OS_WINDOWS and StringUtil::contains(php_uname("v"), "Windows 10"))){
56+
self::$formattingCodes = true;
5857
}
5958
}
6059

@@ -133,19 +132,16 @@ public static function init(){
133132
}
134133

135134
switch(Util::getOS()){
136-
case "linux":
137-
case "mac":
138-
case "bsd":
135+
case Util::OS_LINUX:
136+
case Util::OS_MACOS:
137+
case Util::OS_BSD:
139138
self::getEscapeCodes();
140139
return;
141-
142-
case "win":
143-
case "android":
140+
case Util::OS_ANDROID:
141+
case Util::OS_WINDOWS:
144142
self::getFallbackEscapeCodes();
145143
return;
146144
}
147-
148-
//TODO: iOS
149145
}
150146

151147
}

0 commit comments

Comments
 (0)