From 18c22d1e1d5b932adbc6d19159215125bf86607c Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Tue, 17 Sep 2024 23:56:18 +0800 Subject: [PATCH] [11.x] Move Defer classes to Support component and add `Illuminate\Support\defer` function (#52801) * [11.x] Move Defer classes to Concurrency component Also avoid using global `defer()` function when it can be defined in environment such as Swoole Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * Update DeferredCallbackCollection.php * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki --------- Signed-off-by: Mior Muhammad Zaki Co-authored-by: Taylor Otwell --- src/Illuminate/Cache/Repository.php | 8 +++---- src/Illuminate/Concurrency/ForkDriver.php | 4 +++- src/Illuminate/Concurrency/ProcessDriver.php | 4 +++- src/Illuminate/Concurrency/SyncDriver.php | 4 +++- src/Illuminate/Concurrency/composer.json | 2 +- .../Contracts/Concurrency/Driver.php | 2 +- .../Middleware/InvokeDeferredCallbacks.php | 2 +- .../Providers/FoundationServiceProvider.php | 2 +- src/Illuminate/Foundation/helpers.php | 11 +--------- .../Defer/DeferredCallback.php | 2 +- .../Defer/DeferredCallbackCollection.php | 2 +- .../Support/Facades/Concurrency.php | 2 +- src/Illuminate/Support/functions.php | 22 +++++++++++++++++++ 13 files changed, 42 insertions(+), 25 deletions(-) rename src/Illuminate/{Foundation => Support}/Defer/DeferredCallback.php (96%) rename src/Illuminate/{Foundation => Support}/Defer/DeferredCallbackCollection.php (98%) diff --git a/src/Illuminate/Cache/Repository.php b/src/Illuminate/Cache/Repository.php index 77fbb52a2e5f..e5337cd4a094 100755 --- a/src/Illuminate/Cache/Repository.php +++ b/src/Illuminate/Cache/Repository.php @@ -24,6 +24,8 @@ use Illuminate\Support\InteractsWithTime; use Illuminate\Support\Traits\Macroable; +use function Illuminate\Support\defer; + /** * @mixin \Illuminate\Contracts\Cache\Store */ @@ -517,11 +519,7 @@ public function flexible($key, $ttl, $callback, $lock = null) }); }; - if (function_exists('defer')) { - defer($refresh, "illuminate:cache:refresh:{$key}"); - } else { - $refresh(); - } + defer($refresh, "illuminate:cache:refresh:{$key}"); return $value; } diff --git a/src/Illuminate/Concurrency/ForkDriver.php b/src/Illuminate/Concurrency/ForkDriver.php index a02ee54a94a8..b385e4fbf7e6 100644 --- a/src/Illuminate/Concurrency/ForkDriver.php +++ b/src/Illuminate/Concurrency/ForkDriver.php @@ -4,10 +4,12 @@ use Closure; use Illuminate\Contracts\Concurrency\Driver; -use Illuminate\Foundation\Defer\DeferredCallback; use Illuminate\Support\Arr; +use Illuminate\Support\Defer\DeferredCallback; use Spatie\Fork\Fork; +use function Illuminate\Support\defer; + class ForkDriver implements Driver { /** diff --git a/src/Illuminate/Concurrency/ProcessDriver.php b/src/Illuminate/Concurrency/ProcessDriver.php index 8ba3345f9006..0f4ac17363c0 100644 --- a/src/Illuminate/Concurrency/ProcessDriver.php +++ b/src/Illuminate/Concurrency/ProcessDriver.php @@ -5,12 +5,14 @@ use Closure; use Illuminate\Console\Application; use Illuminate\Contracts\Concurrency\Driver; -use Illuminate\Foundation\Defer\DeferredCallback; 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 { /** diff --git a/src/Illuminate/Concurrency/SyncDriver.php b/src/Illuminate/Concurrency/SyncDriver.php index 62bb552ae664..ca43dc9d27c3 100644 --- a/src/Illuminate/Concurrency/SyncDriver.php +++ b/src/Illuminate/Concurrency/SyncDriver.php @@ -4,8 +4,10 @@ use Closure; use Illuminate\Contracts\Concurrency\Driver; -use Illuminate\Foundation\Defer\DeferredCallback; use Illuminate\Support\Arr; +use Illuminate\Support\Defer\DeferredCallback; + +use function Illuminate\Support\defer; class SyncDriver implements Driver { diff --git a/src/Illuminate/Concurrency/composer.json b/src/Illuminate/Concurrency/composer.json index 6476f73aafde..f7a9c57e37b8 100644 --- a/src/Illuminate/Concurrency/composer.json +++ b/src/Illuminate/Concurrency/composer.json @@ -20,7 +20,7 @@ }, "autoload": { "psr-4": { - "Illuminate\\Support\\": "" + "Illuminate\\Concurrency\\": "" } }, "extra": { diff --git a/src/Illuminate/Contracts/Concurrency/Driver.php b/src/Illuminate/Contracts/Concurrency/Driver.php index fd963ea6c672..901f613b349b 100644 --- a/src/Illuminate/Contracts/Concurrency/Driver.php +++ b/src/Illuminate/Contracts/Concurrency/Driver.php @@ -3,7 +3,7 @@ namespace Illuminate\Contracts\Concurrency; use Closure; -use Illuminate\Foundation\Defer\DeferredCallback; +use Illuminate\Support\Defer\DeferredCallback; interface Driver { diff --git a/src/Illuminate/Foundation/Http/Middleware/InvokeDeferredCallbacks.php b/src/Illuminate/Foundation/Http/Middleware/InvokeDeferredCallbacks.php index 9a804332e73e..9e31e026c87e 100644 --- a/src/Illuminate/Foundation/Http/Middleware/InvokeDeferredCallbacks.php +++ b/src/Illuminate/Foundation/Http/Middleware/InvokeDeferredCallbacks.php @@ -4,8 +4,8 @@ use Closure; use Illuminate\Container\Container; -use Illuminate\Foundation\Defer\DeferredCallbackCollection; use Illuminate\Http\Request; +use Illuminate\Support\Defer\DeferredCallbackCollection; use Symfony\Component\HttpFoundation\Response; class InvokeDeferredCallbacks diff --git a/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php b/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php index 6711dd2d770f..be497edaf88e 100644 --- a/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php @@ -13,7 +13,6 @@ use Illuminate\Database\ConnectionInterface; use Illuminate\Database\Grammar; use Illuminate\Foundation\Console\CliDumper; -use Illuminate\Foundation\Defer\DeferredCallbackCollection; use Illuminate\Foundation\Exceptions\Renderer\Listener; use Illuminate\Foundation\Exceptions\Renderer\Mappers\BladeMapper; use Illuminate\Foundation\Exceptions\Renderer\Renderer; @@ -26,6 +25,7 @@ use Illuminate\Log\Events\MessageLogged; use Illuminate\Queue\Events\JobAttempted; use Illuminate\Support\AggregateServiceProvider; +use Illuminate\Support\Defer\DeferredCallbackCollection; use Illuminate\Support\Facades\URL; use Illuminate\Testing\LoggedExceptionCollection; use Illuminate\Testing\ParallelTestingServiceProvider; diff --git a/src/Illuminate/Foundation/helpers.php b/src/Illuminate/Foundation/helpers.php index 8ce88f468033..09224da3e685 100644 --- a/src/Illuminate/Foundation/helpers.php +++ b/src/Illuminate/Foundation/helpers.php @@ -14,8 +14,6 @@ use Illuminate\Contracts\View\Factory as ViewFactory; use Illuminate\Foundation\Bus\PendingClosureDispatch; use Illuminate\Foundation\Bus\PendingDispatch; -use Illuminate\Foundation\Defer\DeferredCallback; -use Illuminate\Foundation\Defer\DeferredCallbackCollection; use Illuminate\Foundation\Mix; use Illuminate\Http\Exceptions\HttpResponseException; use Illuminate\Log\Context\Repository as ContextRepository; @@ -412,14 +410,7 @@ function decrypt($value, $unserialize = true) */ function defer(?callable $callback = null, ?string $name = null, bool $always = false) { - if ($callback === null) { - return app(DeferredCallbackCollection::class); - } - - return tap( - new DeferredCallback($callback, $name, $always), - fn ($deferred) => app(DeferredCallbackCollection::class)[] = $deferred - ); + return \Illuminate\Support\defer($callback, $name, $always); } } diff --git a/src/Illuminate/Foundation/Defer/DeferredCallback.php b/src/Illuminate/Support/Defer/DeferredCallback.php similarity index 96% rename from src/Illuminate/Foundation/Defer/DeferredCallback.php rename to src/Illuminate/Support/Defer/DeferredCallback.php index 14b8a85690dd..2bf6ad4cbd1c 100644 --- a/src/Illuminate/Foundation/Defer/DeferredCallback.php +++ b/src/Illuminate/Support/Defer/DeferredCallback.php @@ -1,6 +1,6 @@ app(DeferredCallbackCollection::class)[] = $deferred + ); +} + /** * Determine the PHP Binary. *