Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/v10.x #51

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Bus/ServesFeatures.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lucid\Bus;

use App;
use Illuminate\Support\Collection;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Lucid\Events\FeatureStarted;
Expand All @@ -15,14 +16,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 ([email protected])
*
*/
$method = App::version() >= "10.0.0" ? "dispatchSync" : "dispatch";

event(new FeatureStarted($feature, $arguments));

return $this->dispatch($this->marshal($feature, new Collection(), $arguments));
return $this->{$method}($this->marshal($feature, new Collection(), $arguments));
}
}
25 changes: 17 additions & 8 deletions src/Bus/UnitDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([email protected])
*
*/
$method = App::version() >= "10.0.0" ? "dispatchSync" : "dispatch";

if (is_object($unit) && !App::runningUnitTests()) {
$result = $this->dispatch($unit);
$result = $this->{$method}($unit);
} elseif ($arguments instanceof Request) {
$result = $this->dispatch($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),
Expand All @@ -58,7 +67,7 @@ public function run($unit, $arguments = [], $extra = [])
);
}

$result = $this->dispatch($unit);
$result = $this->{$method}($unit);
}

if ($unit instanceof Operation) {
Expand Down Expand Up @@ -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);
}
Expand Down