Skip to content

Commit

Permalink
[11.x] Move Defer classes to Support component and add `Illuminate\Su…
Browse files Browse the repository at this point in the history
…pport\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 <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Update DeferredCallbackCollection.php

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
crynobone and taylorotwell authored Sep 17, 2024
1 parent d582690 commit 18c22d1
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 25 deletions.
8 changes: 3 additions & 5 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Illuminate\Support\InteractsWithTime;
use Illuminate\Support\Traits\Macroable;

use function Illuminate\Support\defer;

/**
* @mixin \Illuminate\Contracts\Cache\Store
*/
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Concurrency/ForkDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Concurrency/ProcessDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Concurrency/SyncDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Concurrency/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"autoload": {
"psr-4": {
"Illuminate\\Support\\": ""
"Illuminate\\Concurrency\\": ""
}
},
"extra": {
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Contracts/Concurrency/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Illuminate\Contracts\Concurrency;

use Closure;
use Illuminate\Foundation\Defer\DeferredCallback;
use Illuminate\Support\Defer\DeferredCallback;

interface Driver
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
11 changes: 1 addition & 10 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Illuminate\Foundation\Defer;
namespace Illuminate\Support\Defer;

use Illuminate\Support\Str;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Illuminate\Foundation\Defer;
namespace Illuminate\Support\Defer;

use ArrayAccess;
use Closure;
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Facades/Concurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @method static \Illuminate\Concurrency\ConcurrencyManager extend(string $name, \Closure $callback)
* @method static \Illuminate\Concurrency\ConcurrencyManager setApplication(\Illuminate\Contracts\Foundation\Application $app)
* @method static array run(\Closure|array $tasks)
* @method static \Illuminate\Foundation\Defer\DeferredCallback defer(\Closure|array $tasks)
* @method static \Illuminate\Support\Defer\DeferredCallback defer(\Closure|array $tasks)
*
* @see \Illuminate\Concurrency\ConcurrencyManager
*/
Expand Down
22 changes: 22 additions & 0 deletions src/Illuminate/Support/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,30 @@

namespace Illuminate\Support;

use Illuminate\Support\Defer\DeferredCallback;
use Illuminate\Support\Defer\DeferredCallbackCollection;
use Illuminate\Support\Process\PhpExecutableFinder;

/**
* Defer execution of the given callback.
*
* @param callable|null $callback
* @param string|null $name
* @param bool $always
* @return \Illuminate\Support\Defer\DeferredCallback
*/
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
);
}

/**
* Determine the PHP Binary.
*
Expand Down

0 comments on commit 18c22d1

Please sign in to comment.