Skip to content

Commit

Permalink
Fixed API response
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaskleinschmidt committed Dec 3, 2020
1 parent b4fbb99 commit 2c966a8
Show file tree
Hide file tree
Showing 12 changed files with 352 additions and 41 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Kirby Terminal
Manage background processes from the panel. Define scripts like you might already be familiar with from `npm`. Start or stop predefined scripts and monitor the output directly in the panel.

## Commercial Usage
This plugin is free. Please consider to [make a donation](https://www.paypal.me/lukaskleinschmidt) if you use it in a commercial project.

![Terminal Preview](http://github.kleinschmidt.at/kirby-terminal/preview.gif)

## Installation
Expand Down
23 changes: 14 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use LukasKleinschmidt\Terminal\Terminal;

/**
* Creates a new Terminal instance
* Creates a new Scripts instance
*
* @param mixed $script
* @param mixed $model
Expand Down
4 changes: 1 addition & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,9 @@
$body = json_encode($terminal->toArray());
$size = strlen($body);

echo Response::json($body, null, null, [
return Response::json($body, null, null, [
'Content-Length' => $size,
]);

return true;
};

// Use the desired api endpoint
Expand Down
79 changes: 64 additions & 15 deletions src/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ public static function isWindows(): bool
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}

/**
* Get enviroment variables
*
* @return array
*/
protected static function env(): array
{
$env = [];

foreach ($_SERVER as $key => $value) {
if (is_string($value) && false !== $value = getenv($key)) {
$env[$key] = $value;
}
}

foreach ($_ENV as $key => $value) {
if (is_string($value)) {
$env[$key] = $value;
}
}

return $env;
}

/**
* Kill a process by pid
*
Expand All @@ -28,7 +52,7 @@ public static function kill(int $pid): ?string
return shell_exec("taskkill /pid $pid -t -f");
}

return shell_exec("kill -TERM -$pid");
return shell_exec("kill -- -$(ps -o pgid= $pid | grep -o [0-9]*)");
}

/**
Expand All @@ -37,15 +61,20 @@ public static function kill(int $pid): ?string
* @param Script $script
* @param string $stdout
* @param string $stderr
* @param array $env
* @return int
*/
public static function run(Script $script, string $stdout = '/dev/null', ?string $stderr = null): int
{
public static function run(
Script $script,
string $stdout = '/dev/null',
?string $stderr = null,
array $env = []
): int {
if (static::isWindows()) {
return static::runWindows($script, $stdout, $stderr);
return static::runWindows($script, $stdout, $stderr, $env);
}

return static::runUnix($script, $stdout, $stderr);
return static::runUnix($script, $stdout, $stderr, $env);
}

/**
Expand All @@ -54,22 +83,28 @@ public static function run(Script $script, string $stdout = '/dev/null', ?string
* @param Script $script
* @param string $stdout
* @param string $stderr
* @param array $env
* @return int
*/
protected static function runUnix(Script $script, string $stdout, ?string $stderr = null): int
{
protected static function runUnix(
Script $script,
string $stdout,
?string $stderr = null,
array $env = []
): int {
// Write the actual pid to file and start a new session to make sure we
// are able to kill any potential child processes as well
$file = tmpfile();
$path = stream_get_meta_data($file)['uri'];
$cmd = $script->cmd();
$cmd = "setsid $cmd & echo $! > $path";
$cmd = "$cmd & echo $! > $path";

static::spawn(
$cmd,
$script->cwd(),
$stdout,
$stderr
$stderr,
$env
);

$size = filesize($path);
Expand All @@ -86,10 +121,15 @@ protected static function runUnix(Script $script, string $stdout, ?string $stder
* @param Script $script
* @param string $stdout
* @param string $stderr
* @param array $env
* @return int
*/
protected static function runWindows(Script $script, string $stdout, ?string $stderr = null): int
{
protected static function runWindows(
Script $script,
string $stdout,
?string $stderr = null,
array $env = []
): int {
$cmd = $script->cmd();
$cmd = "start /b $cmd";

Expand All @@ -99,7 +139,8 @@ protected static function runWindows(Script $script, string $stdout, ?string $st
$cmd,
$script->cwd(),
$stdout,
$stderr
$stderr,
$env
);

// Find correct pid
Expand All @@ -118,21 +159,29 @@ protected static function runWindows(Script $script, string $stdout, ?string $st
* @param string $cwd
* @param string $stdout
* @param string $stderr
* @param array $env
* @throws Exception
* @return int
*/
protected static function spawn(string $cmd, string $cwd, string $stdout, ?string $stderr = null): int
{
protected static function spawn(
string $cmd,
string $cwd,
string $stdout,
?string $stderr = null,
array $env = []
): int {
if (is_null($stderr) === true) {
$stderr = $stdout;
}

$env = array_merge(static::env(), $env);

// Execute the script
$process = proc_open($cmd, [
['pipe', 'r'],
['file', $stdout, 'a'],
['file', $stderr, 'a'],
], $pipes, $cwd);
], $pipes, $cwd, $env);

if (is_resource($process) === false) {
throw new Exception('Unable to spawn process');
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitc076d605bc02f3a2cd89a5768757176b::getLoader();
return ComposerAutoloaderInitd592f846d44b411bfe2ccc611e92a733::getLoader();
6 changes: 3 additions & 3 deletions vendor/composer/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
* @see http://www.php-fig.org/psr/psr-0/
* @see http://www.php-fig.org/psr/psr-4/
* @see https://www.php-fig.org/psr/psr-0/
* @see https://www.php-fig.org/psr/psr-4/
*/
class ClassLoader
{
Expand All @@ -60,7 +60,7 @@ class ClassLoader
public function getPrefixes()
{
if (!empty($this->prefixesPsr0)) {
return call_user_func_array('array_merge', $this->prefixesPsr0);
return call_user_func_array('array_merge', array_values($this->prefixesPsr0));
}

return array();
Expand Down
Loading

0 comments on commit 2c966a8

Please sign in to comment.