2021-08-17 14:21:07 +00:00
|
|
|
<?php
|
2022-09-29 11:01:36 +00:00
|
|
|
// Serve extension, https://github.com/annaesvensson/yellow-serve
|
2021-08-17 14:21:07 +00:00
|
|
|
|
|
|
|
class YellowServe {
|
2024-04-04 21:54:22 +00:00
|
|
|
const VERSION = "0.9.1";
|
2021-08-17 14:21:07 +00:00
|
|
|
public $yellow; // access to API
|
|
|
|
|
|
|
|
// Handle initialisation
|
|
|
|
public function onLoad($yellow) {
|
|
|
|
$this->yellow = $yellow;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle command
|
|
|
|
public function onCommand($command, $text) {
|
|
|
|
switch ($command) {
|
|
|
|
case "serve": $statusCode = $this->processCommandServe($command, $text); break;
|
|
|
|
default: $statusCode = 0;
|
|
|
|
}
|
|
|
|
return $statusCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle command help
|
|
|
|
public function onCommandHelp() {
|
2022-10-30 01:08:16 +00:00
|
|
|
return "serve [url]";
|
2021-08-17 14:21:07 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 21:14:16 +00:00
|
|
|
// Process command to start web server
|
2021-08-17 14:21:07 +00:00
|
|
|
public function processCommandServe($command, $text) {
|
2022-03-20 20:34:45 +00:00
|
|
|
list($url) = $this->yellow->toolbox->getTextArguments($text);
|
2022-11-04 07:59:40 +00:00
|
|
|
if (is_string_empty($url)) $url = "http://localhost:8000/";
|
2021-08-17 14:21:07 +00:00
|
|
|
list($scheme, $address, $base) = $this->yellow->lookup->getUrlInformation($url);
|
2022-11-04 07:59:40 +00:00
|
|
|
if ($scheme=="http" && !is_string_empty($address) && is_string_empty($base)) {
|
2022-04-17 09:19:22 +00:00
|
|
|
if (!preg_match("/\:\d+$/", $address)) $address .= ":8000";
|
|
|
|
if ($this->checkServerSettings("$scheme://$address/")) {
|
2023-05-25 21:14:16 +00:00
|
|
|
echo "Starting web server. Open a web browser and go to $scheme://$address/\n";
|
2022-04-16 19:35:19 +00:00
|
|
|
echo "Press Ctrl+C to quit...\n";
|
2023-04-30 20:00:37 +00:00
|
|
|
exec(PHP_BINARY." -S $address yellow.php 2>&1", $outputLines, $returnStatus);
|
2022-04-16 19:35:19 +00:00
|
|
|
$statusCode = $returnStatus!=0 ? 500 : 200;
|
|
|
|
if ($statusCode!=200) {
|
2022-11-04 07:59:40 +00:00
|
|
|
$output = !is_array_empty($outputLines) ? end($outputLines) : "Please check arguments!";
|
2022-04-16 19:35:19 +00:00
|
|
|
if (preg_match("/^\[(.*?)\]\s*(.*)$/", $output, $matches)) $output = $matches[2];
|
|
|
|
echo "ERROR starting web server: $output\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$statusCode = 400;
|
|
|
|
$fileName = $this->yellow->system->get("coreExtensionDirectory").$this->yellow->system->get("coreSystemFile");
|
|
|
|
echo "ERROR starting web server: Please configure `CoreServerUrl: auto` in file '$fileName'!\n";
|
2021-08-17 14:21:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$statusCode = 400;
|
|
|
|
echo "Yellow $command: Invalid arguments\n";
|
|
|
|
}
|
|
|
|
return $statusCode;
|
|
|
|
}
|
2022-04-16 19:35:19 +00:00
|
|
|
|
|
|
|
// Check server settings
|
2022-04-17 09:19:22 +00:00
|
|
|
public function checkServerSettings($url) {
|
|
|
|
return $this->yellow->system->get("coreServerUrl")=="auto" ||
|
|
|
|
$this->yellow->system->get("coreServerUrl")==$url;
|
2022-04-16 19:35:19 +00:00
|
|
|
}
|
2021-08-17 14:21:07 +00:00
|
|
|
}
|