diff --git a/src/Pool.php b/src/Pool.php index 6ea85a8..3d5866f 100644 --- a/src/Pool.php +++ b/src/Pool.php @@ -41,7 +41,7 @@ class Pool implements ArrayAccess protected $binary = PHP_BINARY; - protected $max_input_size = 100000; + protected $maxTaskPayloadInBytes = 100000; public function __construct() { @@ -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 @@ -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; } @@ -126,7 +126,7 @@ public function notify() $process = array_shift($this->queue); - if (! $process) { + if (!$process) { return; } @@ -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 ); } @@ -176,7 +176,7 @@ public function wait(?callable $intermediateCallback = null): array } } - if (! $this->inProgress) { + if (!$this->inProgress) { break; } @@ -350,7 +350,7 @@ protected function handleFinishedProcess(int $pid, int $status) { $process = $this->inProgress[$pid] ?? null; - if (! $process) { + if (!$process) { return; } diff --git a/src/Runtime/ParentRuntime.php b/src/Runtime/ParentRuntime.php index 646f4ef..bc84d3b 100644 --- a/src/Runtime/ParentRuntime.php +++ b/src/Runtime/ParentRuntime.php @@ -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); }); @@ -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; } @@ -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()); } @@ -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 @@ -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; } }