Skip to content

Commit

Permalink
Code style
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVanderbist committed Aug 25, 2023
1 parent b9b6210 commit 6cf5612
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
20 changes: 10 additions & 10 deletions src/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Pool implements ArrayAccess

protected $binary = PHP_BINARY;

protected $max_input_size = 100000;
protected $maxTaskPayloadInBytes = 100000;

public function __construct()
{
Expand All @@ -66,7 +66,7 @@ public static function isSupported(): bool
function_exists('pcntl_async_signals')
&& function_exists('posix_kill')
&& function_exists('proc_open')
&& ! self::$forceSynchronous;
&& !self::$forceSynchronous;
}

public function forceSynchronous(): self
Expand Down Expand Up @@ -111,9 +111,9 @@ public function withBinary(string $binary): self
return $this;
}

public function maxInputSize(int $max_size): self
public function maxTaskPayload(int $maxSizeInBytes): self
{
$this->max_input_size = $max_size;
$this->maxTaskPayloadInBytes = $maxSizeInBytes;

return $this;
}
Expand All @@ -126,7 +126,7 @@ public function notify()

$process = array_shift($this->queue);

if (! $process) {
if (!$process) {
return;
}

Expand All @@ -141,16 +141,16 @@ public function notify()
*/
public function add($process, ?int $outputLength = null): Runnable
{
if (! is_callable($process) && ! $process instanceof Runnable) {
if (!is_callable($process) && !$process instanceof Runnable) {
throw new InvalidArgumentException('The process passed to Pool::add should be callable.');
}

if (! $process instanceof Runnable) {
if (!$process instanceof Runnable) {
$process = ParentRuntime::createProcess(
$process,
$outputLength,
$this->binary,
$this->max_input_size
$this->maxTaskPayloadInBytes
);
}

Expand All @@ -176,7 +176,7 @@ public function wait(?callable $intermediateCallback = null): array
}
}

if (! $this->inProgress) {
if (!$this->inProgress) {
break;
}

Expand Down Expand Up @@ -350,7 +350,7 @@ protected function handleFinishedProcess(int $pid, int $status)
{
$process = $this->inProgress[$pid] ?? null;

if (! $process) {
if (!$process) {
return;
}

Expand Down
45 changes: 23 additions & 22 deletions src/Runtime/ParentRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class ParentRuntime

public static function init(string $autoloader = null)
{
if (! $autoloader) {
if (!$autoloader) {
$existingAutoloaderFiles = array_filter([
__DIR__.'/../../../../autoload.php',
__DIR__.'/../../../autoload.php',
__DIR__.'/../../vendor/autoload.php',
__DIR__.'/../../../vendor/autoload.php',
__DIR__ . '/../../../../autoload.php',
__DIR__ . '/../../../autoload.php',
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../../../vendor/autoload.php',
], function (string $path) {
return file_exists($path);
});
Expand All @@ -42,7 +42,7 @@ public static function init(string $autoloader = null)
}

self::$autoloader = $autoloader;
self::$childProcessScript = __DIR__.'/ChildRuntime.php';
self::$childProcessScript = __DIR__ . '/ChildRuntime.php';

self::$isInitialised = true;
}
Expand All @@ -55,11 +55,11 @@ public static function init(string $autoloader = null)
*/
public static function createProcess($task, ?int $outputLength = null, ?string $binary = 'php', ?int $max_input_size = 100000): Runnable
{
if (! self::$isInitialised) {
if (!self::$isInitialised) {
self::init();
}

if (! Pool::isSupported()) {
if (!Pool::isSupported()) {
return SynchronousProcess::create($task, self::getId());
}

Expand All @@ -79,35 +79,36 @@ public static function createProcess($task, ?int $outputLength = null, ?string $
*
* @return string
*/
public static function encodeTask($task, ?int $max_input_size = 100000): string
public static function encodeTask($task, ?int $maxTaskPayloadInBytes = 100000): string
{
if ($task instanceof Closure) {
$task = new SerializableClosure($task);
}

//serialize the task. If it's too big to pass on the command line, then we'll have to write it to a file and pass the filename instead...
$serialized_task = base64_encode(serialize($task));
if (strlen($serialized_task) > $max_input_size) {
//write the serialized task to a temporary file...
$serializedTask = base64_encode(serialize($task));

if (strlen($serializedTask) > $maxTaskPayloadInBytes) {
// Write the serialized task to a temporary file and package it as a `FileTask`:
$filename = tempnam(sys_get_temp_dir(), 'spatie_async_task_');
file_put_contents($filename, $serialized_task);
file_put_contents($filename, $serializedTask);
$file_task = new FileTask($filename);
$serialized_task = base64_encode(serialize($file_task));
$serializedTask = base64_encode(serialize($file_task));
}

return $serialized_task;
return $serializedTask;
}

public static function decodeTask(string $task)
{
$decoded_task = unserialize(base64_decode($task));
if (get_class($decoded_task) == 'Spatie\Async\FileTask') {
$filename = $decoded_task->file;
$decoded_task = unserialize(base64_decode(file_get_contents($filename)));
$decodedTask = unserialize(base64_decode($task));

if (get_class($decodedTask) === 'Spatie\Async\FileTask') {
$filename = $decodedTask->file;
$decodedTask = unserialize(base64_decode(file_get_contents($filename)));
unlink($filename);
}

return $decoded_task;
return $decodedTask;
}

protected static function getId(): string
Expand All @@ -118,6 +119,6 @@ protected static function getId(): string

self::$currentId += 1;

return (string) self::$currentId.(string) self::$myPid;
return (string)self::$currentId . (string)self::$myPid;
}
}

0 comments on commit 6cf5612

Please sign in to comment.