first commit

This commit is contained in:
Ariyutta
2026-08-07 10:47:09 +07:00
commit 1c094fbe7f
11630 changed files with 1580900 additions and 0 deletions
@@ -0,0 +1,102 @@
<?php
namespace Illuminate\Concurrency;
use Illuminate\Process\Factory as ProcessFactory;
use Illuminate\Support\MultipleInstanceManager;
use RuntimeException;
use Spatie\Fork\Fork;
/**
* @mixin \Illuminate\Contracts\Concurrency\Driver
*/
class ConcurrencyManager extends MultipleInstanceManager
{
/**
* Get a driver instance by name.
*
* @param string|null $name
* @return mixed
*/
public function driver($name = null)
{
return $this->instance($name);
}
/**
* Create an instance of the process concurrency driver.
*
* @return \Illuminate\Concurrency\ProcessDriver
*/
public function createProcessDriver()
{
return new ProcessDriver($this->app->make(ProcessFactory::class));
}
/**
* Create an instance of the fork concurrency driver.
*
* @return \Illuminate\Concurrency\ForkDriver
*
* @throws \RuntimeException
*/
public function createForkDriver()
{
if (! $this->app->runningInConsole()) {
throw new RuntimeException('Due to PHP limitations, the fork driver may not be used within web requests.');
}
if (! class_exists(Fork::class)) {
throw new RuntimeException('Please install the "spatie/fork" Composer package in order to utilize the "fork" driver.');
}
return new ForkDriver;
}
/**
* Create an instance of the sync concurrency driver.
*
* @return \Illuminate\Concurrency\SyncDriver
*/
public function createSyncDriver()
{
return new SyncDriver;
}
/**
* Get the default instance name.
*
* @return string
*/
public function getDefaultInstance()
{
return $this->app['config']['concurrency.default']
?? $this->app['config']['concurrency.driver']
?? 'process';
}
/**
* Set the default instance name.
*
* @param string $name
* @return void
*/
public function setDefaultInstance($name)
{
$this->app['config']['concurrency.default'] = $name;
$this->app['config']['concurrency.driver'] = $name;
}
/**
* Get the instance specific configuration.
*
* @param string $name
* @return array
*/
public function getInstanceConfig($name)
{
return $this->app['config']->get(
'concurrency.driver.'.$name, ['driver' => $name],
);
}
}
@@ -0,0 +1,33 @@
<?php
namespace Illuminate\Concurrency;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
class ConcurrencyServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton(ConcurrencyManager::class, function ($app) {
return new ConcurrencyManager($app);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
ConcurrencyManager::class,
];
}
}
@@ -0,0 +1,81 @@
<?php
namespace Illuminate\Concurrency\Console;
use Illuminate\Console\Command;
use ReflectionClass;
use Symfony\Component\Console\Attribute\AsCommand;
use Throwable;
#[AsCommand(name: 'invoke-serialized-closure')]
class InvokeSerializedClosureCommand extends Command
{
/**
* The console command signature.
*
* @var string
*/
protected $signature = 'invoke-serialized-closure {code? : The serialized closure}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Invoke the given serialized closure';
/**
* Indicates whether the command should be shown in the Artisan command list.
*
* @var bool
*/
protected $hidden = true;
/**
* Execute the console command.
*
* @return void
*
* @throws \RuntimeException
*/
public function handle()
{
try {
$this->output->write(json_encode([
'successful' => true,
'result' => serialize($this->laravel->call(match (true) {
! is_null($this->argument('code')) => unserialize($this->argument('code')),
isset($_SERVER['LARAVEL_INVOKABLE_CLOSURE']) => unserialize(
base64_decode($_SERVER['LARAVEL_INVOKABLE_CLOSURE'])
),
default => fn () => null,
})),
]));
} catch (Throwable $e) {
report($e);
$reflection = new ReflectionClass($e);
$constructor = $reflection->getConstructor();
$parameters = [];
if ($constructor) {
$declaringClass = $constructor->getDeclaringClass()->getName();
if ($declaringClass === $reflection->getName()) {
foreach ($constructor->getParameters() as $parameter) {
$parameters[$parameter->name] = $e->{$parameter->name} ?? null;
}
}
}
$this->output->write(json_encode([
'successful' => false,
'exception' => get_class($e),
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'parameters' => $parameters,
]));
}
}
}
@@ -0,0 +1,40 @@
<?php
namespace Illuminate\Concurrency;
use Closure;
use Illuminate\Contracts\Concurrency\Driver;
use Illuminate\Support\Arr;
use Illuminate\Support\Defer\DeferredCallback;
use Spatie\Fork\Fork;
use function Illuminate\Support\defer;
class ForkDriver implements Driver
{
/**
* Run the given tasks concurrently and return an array containing the results.
*/
public function run(Closure|array $tasks): array
{
$tasks = Arr::wrap($tasks);
$keys = array_keys($tasks);
$values = array_values($tasks);
/** @phpstan-ignore class.notFound */
$results = Fork::new()->run(...$values);
ksort($results);
return array_combine($keys, $results);
}
/**
* Start the given tasks in the background after the current task has finished.
*/
public function defer(Closure|array $tasks): DeferredCallback
{
return defer(fn () => $this->run($tasks));
}
}
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Taylor Otwell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@@ -0,0 +1,86 @@
<?php
namespace Illuminate\Concurrency;
use Closure;
use Exception;
use Illuminate\Console\Application;
use Illuminate\Contracts\Concurrency\Driver;
use Illuminate\Process\Factory as ProcessFactory;
use Illuminate\Process\Pool;
use Illuminate\Support\Arr;
use Illuminate\Support\Defer\DeferredCallback;
use Laravel\SerializableClosure\SerializableClosure;
use function Illuminate\Support\defer;
class ProcessDriver implements Driver
{
/**
* Create a new process based concurrency driver.
*/
public function __construct(protected ProcessFactory $processFactory)
{
//
}
/**
* Run the given tasks concurrently and return an array containing the results.
*/
public function run(Closure|array $tasks): array
{
$command = Application::formatCommandString('invoke-serialized-closure');
$results = $this->processFactory->pool(function (Pool $pool) use ($tasks, $command) {
foreach (Arr::wrap($tasks) as $key => $task) {
$pool->as($key)->path(base_path())->env([
'LARAVEL_INVOKABLE_CLOSURE' => base64_encode(
serialize(new SerializableClosure($task))
),
])->command($command);
}
})->start()->wait();
return $results->collect()->mapWithKeys(function ($result, $key) {
if ($result->failed()) {
throw new Exception('Concurrent process failed with exit code ['.$result->exitCode().']. Message: '.$result->errorOutput());
}
$output = $result->output();
if (($pos = strpos($output, "\x1f\x8b")) !== false) {
$output = substr($output, 0, $pos);
}
$result = json_decode($output, true);
if (! $result['successful']) {
throw new $result['exception'](
...(! empty(array_filter($result['parameters']))
? $result['parameters']
: [$result['message']])
);
}
return [$key => unserialize($result['result'])];
})->all();
}
/**
* Start the given tasks in the background after the current task has finished.
*/
public function defer(Closure|array $tasks): DeferredCallback
{
$command = Application::formatCommandString('invoke-serialized-closure');
return defer(function () use ($tasks, $command) {
foreach (Arr::wrap($tasks) as $task) {
$this->processFactory->path(base_path())->env([
'LARAVEL_INVOKABLE_CLOSURE' => base64_encode(
serialize(new SerializableClosure($task))
),
])->run($command.' 2>&1 &');
}
});
}
}
@@ -0,0 +1,31 @@
<?php
namespace Illuminate\Concurrency;
use Closure;
use Illuminate\Contracts\Concurrency\Driver;
use Illuminate\Support\Collection;
use Illuminate\Support\Defer\DeferredCallback;
use function Illuminate\Support\defer;
class SyncDriver implements Driver
{
/**
* Run the given tasks concurrently and return an array containing the results.
*/
public function run(Closure|array $tasks): array
{
return Collection::wrap($tasks)->map(
fn ($task) => $task()
)->all();
}
/**
* Start the given tasks in the background after the current task has finished.
*/
public function defer(Closure|array $tasks): DeferredCallback
{
return defer(fn () => Collection::wrap($tasks)->each(fn ($task) => $task()));
}
}
@@ -0,0 +1,38 @@
{
"name": "illuminate/concurrency",
"description": "The Illuminate Concurrency package.",
"license": "MIT",
"homepage": "https://laravel.com",
"support": {
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"require": {
"php": "^8.2",
"illuminate/console": "^12.0",
"illuminate/contracts": "^12.0",
"illuminate/process": "^12.0",
"illuminate/support": "^12.0",
"laravel/serializable-closure": "^1.3|^2.0"
},
"autoload": {
"psr-4": {
"Illuminate\\Concurrency\\": ""
}
},
"extra": {
"branch-alias": {
"dev-master": "11.x-dev"
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev"
}