Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/config.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ return [
'constants.php',
# <std> #
'std/array.php',
'std/exec.php',
# <ext> #
'ext/curl.php',
# <core> #
Expand Down
26 changes: 26 additions & 0 deletions library/std/exec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
function swoole_exec(string $command, &$output = null, &$returnVar = null)
{
$result = Swoole\Coroutine::exec($command);
if ($result) {
$outputList = explode(PHP_EOL, $result['output']);
if ($output) {
$output = array_merge($output, $outputList);
} else {
$output = $outputList;
}
$returnVar = $result['code'];
return end($outputList);
} else {
return false;
}
}

function swoole_shell_exec(string $cmd)
{
$result = Swoole\Coroutine::exec($cmd);
if ($result && '' !== $result['output']) {
return $result['output'];
}
return null;
}
58 changes: 56 additions & 2 deletions php_swoole_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ static const char* swoole_library_source_std_array =
" return true;\n"
"}\n";

static const char* swoole_library_source_std_exec =
"\n"
"function swoole_exec(string $command, &$output = null, &$returnVar = null)\n"
"{\n"
" $result = Swoole\\Coroutine::exec($command);\n"
" if ($result) {\n"
" $outputList = explode(PHP_EOL, $result['output']);\n"
" if ($output) {\n"
" $output = array_merge($output, $outputList);\n"
" } else {\n"
" $output = $outputList;\n"
" }\n"
" $returnVar = $result['code'];\n"
" return end($outputList);\n"
" } else {\n"
" return false;\n"
" }\n"
"}\n"
"\n"
"function swoole_shell_exec(string $cmd)\n"
"{\n"
" $result = Swoole\\Coroutine::exec($cmd);\n"
" if ($result && '' !== $result['output']) {\n"
" return $result['output'];\n"
" }\n"
" return null;\n"
"}\n";

static const char* swoole_library_source_ext_curl =
"\n"
"\n"
Expand Down Expand Up @@ -1980,11 +2008,15 @@ static const char* swoole_library_source_functions =
" }\n"
"\n"
" /**\n"
" * @return \\Swoole\\Coroutine\\Scheduler\n"
" * @return Swoole\\Coroutine\\Scheduler\n"
" */\n"
" function scheduler()\n"
" {\n"
" return new Swoole\\Coroutine\\Scheduler();\n"
" static $scheduler = null;\n"
" if (!$scheduler) {\n"
" $scheduler = new Swoole\\Coroutine\\Scheduler();\n"
" }\n"
" return $scheduler;\n"
" }\n"
"}\n"
"\n"
Expand Down Expand Up @@ -2024,10 +2056,31 @@ static const char* swoole_library_source_alias =
" class_alias(Swoole\\Coroutine\\Server::class, Co\\Server::class, false);\n"
"}\n";

static const char* swoole_library_source_alias_ns =
"\n"
"\n"
"namespace Swoole\\Coroutine {\n"
"\n"
" function run(callable $fn, ...$args)\n"
" {\n"
" $s = new Scheduler();\n"
" $s->add($fn, ...$args);\n"
" return $s->start();\n"
" }\n"
"}\n"
"\n"
"namespace Co {\n"
" function run(callable $fn, ...$args)\n"
" {\n"
" return \\Swoole\\Coroutine\\Run($fn, ...$args);\n"
" }\n"
"}\n";

static void php_swoole_load_library()
{
zend::eval(swoole_library_source_constants, "@swoole-src/library/constants.php");
zend::eval(swoole_library_source_std_array, "@swoole-src/library/std/array.php");
zend::eval(swoole_library_source_std_exec, "@swoole-src/library/std/exec.php");
zend::eval(swoole_library_source_ext_curl, "@swoole-src/library/ext/curl.php");
zend::eval(swoole_library_source_core_coroutine_wait_group, "@swoole-src/library/core/Coroutine/WaitGroup.php");
zend::eval(swoole_library_source_core_coroutine_object_pool, "@swoole-src/library/core/Coroutine/ObjectPool.php");
Expand All @@ -2037,4 +2090,5 @@ static void php_swoole_load_library()
zend::eval(swoole_library_source_core_coroutine_server_connection, "@swoole-src/library/core/Coroutine/Server/Connection.php");
zend::eval(swoole_library_source_functions, "@swoole-src/library/functions.php");
zend::eval(swoole_library_source_alias, "@swoole-src/library/alias.php");
zend::eval(swoole_library_source_alias_ns, "@swoole-src/library/alias_ns.php");
}
4 changes: 4 additions & 0 deletions swoole_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1156,13 +1156,17 @@ bool PHPCoroutine::enable_hook(int flags)
if (!(hook_flags & SW_HOOK_BLOCKING_FUNCTION))
{
hook_func(ZEND_STRL("gethostbyname"), PHP_FN(swoole_coroutine_gethostbyname));
hook_func(ZEND_STRL("exec"));
hook_func(ZEND_STRL("shell_exec"));
}
}
else
{
if (hook_flags & SW_HOOK_BLOCKING_FUNCTION)
{
SW_UNHOOK_FUNC(gethostbyname);
unhook_func(ZEND_STRL("exec"));
unhook_func(ZEND_STRL("shell_exec"));
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/swoole_library/exec/exec/1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
swoole_library/std/exec: Test exec
--SKIPIF--
<?php require __DIR__ . '/../../../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../../../include/bootstrap.php';

Swoole\Runtime::enableCoroutine();
go(function () {
$data = exec('md5sum ' . TEST_IMAGE, $output, $returnVar);
Assert::eq($returnVar, 0);
Assert::eq(strstr(implode(PHP_EOL, $output), ' ', true), md5_file(TEST_IMAGE));
});

?>
--EXPECT--
16 changes: 16 additions & 0 deletions tests/swoole_library/exec/shell_exec/1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
swoole_library/std/exec: Test exec
--SKIPIF--
<?php require __DIR__ . '/../../../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../../../include/bootstrap.php';

Swoole\Runtime::enableCoroutine();
go(function () {
$output = shell_exec('md5sum ' . TEST_IMAGE);
Assert::eq(strstr($output, ' ', true), md5_file(TEST_IMAGE));
});

?>
--EXPECT--