-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved shared code for plugins in traits
- Loading branch information
Showing
4 changed files
with
73 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Luzrain\PHPStreamServer\Plugin; | ||
|
||
use Amp\Future; | ||
use function Amp\async; | ||
|
||
trait NullStop | ||
{ | ||
public function stop(): Future | ||
{ | ||
return async(static fn() => null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Luzrain\PHPStreamServer\Plugin; | ||
|
||
use Luzrain\PHPStreamServer\Server; | ||
|
||
trait PcntlExecCommand | ||
{ | ||
/** | ||
* Prepare command for pcntl_exec acceptable format | ||
* | ||
* @return array{0: string, 1: list<string>} | ||
*/ | ||
private function prepareCommandForPcntlExec(string $command): array | ||
{ | ||
// Check if command contains logic operators such as && and || | ||
if (\preg_match('/(\'[^\']*\'|"[^"]*")(*SKIP)(*FAIL)|&&|\|\|/', $command) === 1) { | ||
throw new \RuntimeException(\sprintf( | ||
'%s does not directly support executing multiple commands with logical operators. Use shell with -c option e.g. "/bin/sh -c "%s"', | ||
Server::NAME, | ||
$command, | ||
)); | ||
} | ||
|
||
\preg_match_all('/\'[^\']*\'|"[^"]*"|\S+/', $command, $matches); | ||
$parts = \array_map(static fn (string $part): string => \trim($part, '"\''), $matches[0]); | ||
$binary = \array_shift($parts); | ||
$args = $parts; | ||
|
||
if (!\str_starts_with($binary, '/') && \is_string($absoluteBinaryPath = \shell_exec("command -v $binary"))) { | ||
$binary = \trim($absoluteBinaryPath); | ||
} | ||
|
||
return [$binary, $args]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters