From bce8b71ca3b35534233d089bc4a9449e19715336 Mon Sep 17 00:00:00 2001 From: Nay Thu Khant Date: Mon, 13 Mar 2023 13:12:27 +0630 Subject: [PATCH 1/3] Unit dispatcher is updated to align with Laravel 10.x --- src/Bus/ServesFeatures.php | 2 +- src/Bus/UnitDispatcher.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Bus/ServesFeatures.php b/src/Bus/ServesFeatures.php index 9c0c1e4..577504d 100644 --- a/src/Bus/ServesFeatures.php +++ b/src/Bus/ServesFeatures.php @@ -23,6 +23,6 @@ public function serve($feature, $arguments = []) { event(new FeatureStarted($feature, $arguments)); - return $this->dispatch($this->marshal($feature, new Collection(), $arguments)); + return $this->dispatchSync($this->marshal($feature, new Collection(), $arguments)); } } diff --git a/src/Bus/UnitDispatcher.php b/src/Bus/UnitDispatcher.php index a7b422e..1745b37 100644 --- a/src/Bus/UnitDispatcher.php +++ b/src/Bus/UnitDispatcher.php @@ -35,9 +35,9 @@ trait UnitDispatcher public function run($unit, $arguments = [], $extra = []) { if (is_object($unit) && !App::runningUnitTests()) { - $result = $this->dispatch($unit); + $result = $this->dispatchSync($unit); } elseif ($arguments instanceof Request) { - $result = $this->dispatch($this->marshal($unit, $arguments, $extra)); + $result = $this->dispatchSync($this->marshal($unit, $arguments, $extra)); } else { if (!is_object($unit)) { $unit = $this->marshal($unit, new Collection(), $arguments); @@ -58,7 +58,7 @@ public function run($unit, $arguments = [], $extra = []) ); } - $result = $this->dispatch($unit); + $result = $this->dispatchSync($unit); } if ($unit instanceof Operation) { From 5fa6da3378e2b20e9018f6951dac1bcad629809b Mon Sep 17 00:00:00 2001 From: Nay Thu Khant Date: Mon, 20 Mar 2023 17:46:30 +0630 Subject: [PATCH 2/3] Before and after of Laravel 10.0.0 are supported now --- src/Bus/ServesFeatures.php | 13 +++++++++++-- src/Bus/UnitDispatcher.php | 25 +++++++++++++++++-------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/Bus/ServesFeatures.php b/src/Bus/ServesFeatures.php index 577504d..7721ed9 100644 --- a/src/Bus/ServesFeatures.php +++ b/src/Bus/ServesFeatures.php @@ -15,14 +15,23 @@ trait ServesFeatures * Serve the given feature with the given arguments. * * @param string $feature - * @param array $arguments + * @param array $arguments * * @return mixed */ + public function serve($feature, $arguments = []) { + /** + * Laravel change the behaviour of the dispatch after the release of 10.0.0 and we have to explictly handle this run method + * https://github.com/laravel/framework/commit/5f61fd1af0fa0b37a8888637578459eae21faeb + * @author Nay Thu Khant (naythukhant644@gmail.com) + * + */ + $method = App::version() >= "10.0.0" ? "dispatchSync" : "dispatch"; + event(new FeatureStarted($feature, $arguments)); - return $this->dispatchSync($this->marshal($feature, new Collection(), $arguments)); + return $this->{$method}($this->marshal($feature, new Collection(), $arguments)); } } diff --git a/src/Bus/UnitDispatcher.php b/src/Bus/UnitDispatcher.php index 1745b37..5d23715 100644 --- a/src/Bus/UnitDispatcher.php +++ b/src/Bus/UnitDispatcher.php @@ -26,30 +26,39 @@ trait UnitDispatcher * When the $arguments is an instance of Request * it will call dispatchFrom instead. * - * @param mixed $unit + * @param mixed $unit * @param array|\Illuminate\Http\Request $arguments - * @param array $extra + * @param array $extra * * @return mixed */ public function run($unit, $arguments = [], $extra = []) { + + /** + * Laravel change the behaviour of the dispatch after the release of 10.0.0 and we have to explictly handle this run method + * https://github.com/laravel/framework/commit/5f61fd1af0fa0b37a8888637578459eae21faeb + * @author Nay Thu Khant (naythukhant644@gmail.com) + * + */ + $method = App::version() >= "10.0.0" ? "dispatchSync" : "dispatch"; + if (is_object($unit) && !App::runningUnitTests()) { - $result = $this->dispatchSync($unit); + $result = $this->{$method}($unit); } elseif ($arguments instanceof Request) { - $result = $this->dispatchSync($this->marshal($unit, $arguments, $extra)); + $result = $this->{$method}($this->marshal($unit, $arguments, $extra)); } else { if (!is_object($unit)) { $unit = $this->marshal($unit, new Collection(), $arguments); - // don't dispatch unit when in tests and have a mock for it. + // don't $this->dispatch() unit when in tests and have a mock for it. } elseif (App::runningUnitTests() && app(UnitMockRegistry::class)->has(get_class($unit))) { /** @var UnitMock $mock */ $mock = app(UnitMockRegistry::class)->get(get_class($unit)); $mock->compareTo($unit); // Reaching this step confirms that the expected mock is similar to the passed instance, so we - // get the unit's mock counterpart to be dispatched. Otherwise, the previous step would + // get the unit's mock counterpart to be $this->dispatch()ed. Otherwise, the previous step would // throw an exception when the mock doesn't match the passed instance. $unit = $this->marshal( get_class($unit), @@ -58,7 +67,7 @@ public function run($unit, $arguments = [], $extra = []) ); } - $result = $this->dispatchSync($unit); + $result = $this->{$method}($unit); } if ($unit instanceof Operation) { @@ -87,7 +96,7 @@ public function runInQueue($unit, array $arguments = [], $queue = 'default') // instantiate and queue the unit $reflection = new ReflectionClass($unit); $instance = $reflection->newInstanceArgs($arguments); - $instance->onQueue((string) $queue); + $instance->onQueue((string)$queue); return $this->dispatch($instance); } From 9d9c8c89c202d4fafda7115a8a2a22426e2929b2 Mon Sep 17 00:00:00 2001 From: Nay Thu Khant Date: Mon, 20 Mar 2023 18:29:34 +0630 Subject: [PATCH 3/3] bug fixed --- src/Bus/ServesFeatures.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Bus/ServesFeatures.php b/src/Bus/ServesFeatures.php index 7721ed9..cef11d2 100644 --- a/src/Bus/ServesFeatures.php +++ b/src/Bus/ServesFeatures.php @@ -2,6 +2,7 @@ namespace Lucid\Bus; +use App; use Illuminate\Support\Collection; use Illuminate\Foundation\Bus\DispatchesJobs; use Lucid\Events\FeatureStarted;