Skip to content

Commit b5a3d9a

Browse files
committed
🔨 improve serve method
Signed-off-by: otengkwame <[email protected]>
1 parent ef93a54 commit b5a3d9a

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

Core/core/Console/Console.php

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class Console
2828

2929
private const WEBBY_CLI_VERSION = '1.8.0';
3030

31+
private const DEFAULT_HOST = "localhost";
32+
33+
private const DEFAULT_PORT = 8085;
34+
3135
private static $sylynderEngine = 'sylynder/engine';
3236

3337
/**
@@ -922,24 +926,49 @@ private static function serve($args = []): void
922926
{
923927
static::$rootpath = static::userConstants()->WEBBY_ROOTPATH;
924928

925-
$number = isset($args[3]) ? (int)$args[3] : "";
926-
$project_dir = static::$rootpath; //__DIR__;
927-
$dir = realpath($project_dir . '/public/');
928-
$port = (isset($number) && is_int($number)) ? $number : 8085;
929-
$port = intval($port);
929+
$host = static::DEFAULT_HOST;
930+
$port = 0;
931+
$number = static::DEFAULT_PORT;
932+
933+
if (isset($args[2]) && $args[2] === '--port') {
934+
$number = isset($args[3]) ? (int)$args[3] : "";
935+
$port = (isset($number) && is_int($number)) ? $number : static::DEFAULT_PORT;
936+
$port = intval($port);
937+
938+
} else if ((isset($args[2]) && ($args[2] === '--host'))
939+
&& (isset($args[2]) && $args[4] === '--port')
940+
) {
941+
942+
$host = isset($args[3]) ? (string)$args[3] : "localhost";
943+
$number = isset($args[5]) ? (int)$args[5] : "";
944+
$port = (isset($number) && is_int($number)) ? $number : static::DEFAULT_PORT;
945+
$port = intval($port);
946+
947+
} else {
948+
949+
$port = $number;
950+
$port = intval($port);
951+
952+
}
930953

931954
if ($port === 0) {
932-
echo ConsoleColor::red("Please choose a valid port!") . "\n";
955+
echo ConsoleColor::red("\n\tPlease choose a valid port number!\n") . "\n";
933956
exit;
934957
}
935958

936-
$output = "PHP Webserver Started for Webby \n";
937-
$output .= "Navigate to http://localhost:{$port} ";
938-
$output .= "to view your project.\nPress Ctrl+C to stop it!";
959+
$project_dir = static::$rootpath; //__DIR__;
960+
$dir = realpath($project_dir . '/public/');
961+
962+
$output = ConsoleColor::green("\n\tPHP Built-In Web Server Started for Webby \n\n");
963+
$output .= ConsoleColor::green("\tNavigate to ");
964+
$output .= ConsoleColor::cyan("http://{$host}:{$port} ");
965+
$output .= ConsoleColor::green("to view your project.\n");
966+
$output .= ConsoleColor::yellow("\n\tPress Ctrl+C to stop Webby Server!");
939967
" \n";
940968

941-
echo $output . "\n";
942-
system('php -S localhost:' . $port . ' -t ' . $dir);
969+
echo ConsoleColor::green($output) . "\n\n";
970+
971+
static::runSystemCommand("php -S {$host}:" . $port . " -t " . $dir);
943972
}
944973

945974
/**
@@ -948,10 +977,14 @@ private static function serve($args = []): void
948977
* @param array $args
949978
* @return void
950979
*/
951-
public static function run($args): void
980+
public static function run(array $args): void
952981
{
953-
954-
if (isset($args[2]) && $args[2] === '--port') {
982+
if (
983+
(isset($args[2]) && $args[2] === '--host')
984+
&& (isset($args[4]) && $args[4] === '--port')
985+
) {
986+
Console::serve($args);
987+
} else if (isset($args[2]) && $args[2] === '--port' && $args[1] !== 'quit') {
955988
Console::serve($args);
956989
} else if (isset($args[1]) && $args[1] === 'serve') {
957990
Console::serve();

0 commit comments

Comments
 (0)