diff --git a/assets/TenancyServiceProvider.stub.php b/assets/TenancyServiceProvider.stub.php index d2366fb5..9f49cf37 100644 --- a/assets/TenancyServiceProvider.stub.php +++ b/assets/TenancyServiceProvider.stub.php @@ -5,7 +5,7 @@ namespace App\Providers; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; -use Stancl\Tenancy\Listeners\JobPipeline; +use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Events\DatabaseCreated; diff --git a/composer.json b/composer.json index e6bef0a9..f7adfce3 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "ext-json": "*", "illuminate/support": "^6.0|^7.0", "facade/ignition-contracts": "^1.0", - "ramsey/uuid": "^3.7|^4.0" + "ramsey/uuid": "^3.7|^4.0", + "stancl/jobpipeline": "^1.0" }, "require-dev": { "vlucas/phpdotenv": "^3.3|^4.0", diff --git a/src/Listeners/JobPipeline.php b/src/Listeners/JobPipeline.php deleted file mode 100644 index a0ed489f..00000000 --- a/src/Listeners/JobPipeline.php +++ /dev/null @@ -1,95 +0,0 @@ -jobs = $jobs; - $this->send = $send ?? function ($event) { - // If no $send callback is set, we'll just pass the event through the jobs. - return $event; - }; - $this->shouldBeQueued = $shouldBeQueued ?? static::$shouldBeQueuedByDefault; - } - - /** @param callable[]|string[] $jobs */ - public static function make(array $jobs): self - { - return new static($jobs); - } - - public function send(callable $send): self - { - $this->send = $send; - - return $this; - } - - public function shouldBeQueued(bool $shouldBeQueued) - { - $this->shouldBeQueued = $shouldBeQueued; - - return $this; - } - - public function handle(): void - { - foreach ($this->jobs as $job) { - app()->call([new $job(...$this->passable), 'handle']); - } - } - - /** - * Generate a closure that can be used as a listener. - */ - public function toListener(): Closure - { - return function (...$args) { - $executable = $this->executable($args); - - if ($this->shouldBeQueued) { - dispatch($executable); - } else { - dispatch_now($executable); - } - }; - } - - /** - * Return a serializable version of the current object. - */ - public function executable($listenerArgs): self - { - $clone = clone $this; - - $passable = ($clone->send)(...$listenerArgs); - $passable = is_array($passable) ? $passable : [$passable]; - - $clone->passable = $passable; - unset($clone->send); - - return $clone; - } -} diff --git a/tests/BootstrapperTest.php b/tests/BootstrapperTest.php index 3e1f3799..88b76439 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -9,7 +9,7 @@ use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Storage; use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Listeners\BootstrapTenancy; -use Stancl\Tenancy\Listeners\JobPipeline; +use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyInitialized; diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index 0e7d69e6..c2229945 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -11,7 +11,7 @@ use Illuminate\Support\Facades\Schema; use Stancl\Tenancy\Tests\Etc\ExampleSeeder; use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Listeners\BootstrapTenancy; -use Stancl\Tenancy\Listeners\JobPipeline; +use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyInitialized; diff --git a/tests/DatabasePreparationTest.php b/tests/DatabasePreparationTest.php index da311b3a..9f2a1e2b 100644 --- a/tests/DatabasePreparationTest.php +++ b/tests/DatabasePreparationTest.php @@ -8,7 +8,7 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Schema; use Stancl\Tenancy\Tests\Etc\Tenant; -use Stancl\Tenancy\Listeners\JobPipeline; +use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Events\TenantCreated; use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Jobs\MigrateDatabase; diff --git a/tests/DatabaseUsersTest.php b/tests/DatabaseUsersTest.php index 41eec1d0..01b33157 100644 --- a/tests/DatabaseUsersTest.php +++ b/tests/DatabaseUsersTest.php @@ -13,7 +13,7 @@ use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager; use Stancl\Tenancy\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager; use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Events\TenancyInitialized; -use Stancl\Tenancy\Listeners\JobPipeline; +use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Events\TenantCreated; use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Listeners\BootstrapTenancy; diff --git a/tests/JobPipelineTest.php b/tests/JobPipelineTest.php deleted file mode 100644 index f0106a77..00000000 --- a/tests/JobPipelineTest.php +++ /dev/null @@ -1,180 +0,0 @@ - 'redis']); - - $this->valuestore = Valuestore::make(__DIR__ . '/Etc/tmp/jobpipelinetest.json')->flush(); - } - - /** @test */ - public function job_pipeline_can_listen_to_any_event() - { - Event::listen(TenantCreated::class, JobPipeline::make([ - FooJob::class, - ])->send(function () { - return $this->valuestore; - })->toListener()); - - $this->assertFalse($this->valuestore->has('foo')); - - Tenant::create(); - - $this->assertSame('bar', $this->valuestore->get('foo')); - } - - /** @test */ - public function job_pipeline_can_be_queued() - { - Queue::fake(); - - Event::listen(TenantCreated::class, JobPipeline::make([ - FooJob::class, - ])->send(function () { - return $this->valuestore; - })->shouldBeQueued(true)->toListener()); - - Queue::assertNothingPushed(); - - Tenant::create(); - $this->assertFalse($this->valuestore->has('foo')); - - Queue::pushed(JobPipeline::class, function (JobPipeline $pipeline) { - $this->assertSame([FooJob::class], $pipeline->jobs); - }); - } - - /** @test */ - public function job_pipelines_run_when_queued() - { - Event::listen(TenantCreated::class, JobPipeline::make([ - FooJob::class, - ])->send(function () { - return $this->valuestore; - })->shouldBeQueued(true)->toListener()); - - $this->assertFalse($this->valuestore->has('foo')); - Tenant::create(); - $this->artisan('queue:work --once'); - - $this->assertSame('bar', $this->valuestore->get('foo')); - } - - /** @test */ - public function job_pipeline_executes_jobs_and_passes_the_object_sequentially() - { - Event::listen(TenantCreated::class, JobPipeline::make([ - FirstJob::class, - SecondJob::class, - ])->send(function (TenantCreated $event) { - return [$event->tenant, $this->valuestore]; - })->toListener()); - - $this->assertFalse($this->valuestore->has('foo')); - - Tenant::create(); - - $this->assertSame('first job changed property', $this->valuestore->get('foo')); - } - - /** @test */ - public function send_can_return_multiple_arguments() - { - Event::listen(TenantCreated::class, JobPipeline::make([ - JobWithMultipleArguments::class - ])->send(function () { - return ['a', 'b']; - })->toListener()); - - $this->assertFalse(app()->bound('test_args')); - - Tenant::create(); - - $this->assertSame(['a', 'b'], app('test_args')); - } -} - -class FooJob -{ - protected $valuestore; - - public function __construct(Valuestore $valuestore) - { - $this->valuestore = $valuestore; - } - - public function handle() - { - $this->valuestore->put('foo', 'bar'); - } -}; - -class FirstJob -{ - public $tenant; - - public function __construct(Tenant $tenant) - { - $this->tenant = $tenant; - } - - public function handle() - { - $this->tenant->foo = 'first job changed property'; - } -} - -class SecondJob -{ - public $tenant; - - protected $valuestore; - - public function __construct(Tenant $tenant, Valuestore $valuestore) - { - $this->tenant = $tenant; - $this->valuestore = $valuestore; - } - - public function handle() - { - $this->valuestore->put('foo', $this->tenant->foo); - } -} - -class JobWithMultipleArguments -{ - protected $first; - protected $second; - - public function __construct($first, $second) - { - $this->first = $first; - $this->second = $second; - } - - public function handle() - { - // we dont queue this job so no need to use valuestore here - app()->instance('test_args', [$this->first, $this->second]); - } -} diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index 15f7e3e7..a4ab27cd 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -14,7 +14,7 @@ use Stancl\Tenancy\Database\Concerns\ResourceSyncing; use Stancl\Tenancy\Database\Models\TenantPivot; use Stancl\Tenancy\DatabaseConfig; use Stancl\Tenancy\Listeners\BootstrapTenancy; -use Stancl\Tenancy\Listeners\JobPipeline; +use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Listeners\UpdateSyncedResource; use Stancl\Tenancy\Events\SyncedResourceChangedInForeignDatabase; diff --git a/tests/TenantDatabaseManagerTest.php b/tests/TenantDatabaseManagerTest.php index 6fe22d07..ba579ba1 100644 --- a/tests/TenantDatabaseManagerTest.php +++ b/tests/TenantDatabaseManagerTest.php @@ -8,7 +8,7 @@ use Illuminate\Support\Facades\Event; use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\DatabaseManager; use Stancl\Tenancy\Listeners\BootstrapTenancy; -use Stancl\Tenancy\Listeners\JobPipeline; +use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Events\TenantCreated; use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException; diff --git a/tests/TenantModelTest.php b/tests/TenantModelTest.php index 2847d8e6..60a1e346 100644 --- a/tests/TenantModelTest.php +++ b/tests/TenantModelTest.php @@ -16,7 +16,7 @@ use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Listeners\BootstrapTenancy; -use Stancl\Tenancy\Listeners\JobPipeline; +use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Illuminate\Support\Str; use Stancl\Tenancy\Database\TenantCollection; diff --git a/tests/TenantUserImpersonationTest.php b/tests/TenantUserImpersonationTest.php index 3d10f069..81c9dc66 100644 --- a/tests/TenantUserImpersonationTest.php +++ b/tests/TenantUserImpersonationTest.php @@ -16,7 +16,7 @@ use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Events\TenantCreated; use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Listeners\BootstrapTenancy; -use Stancl\Tenancy\Listeners\JobPipeline; +use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Middleware\InitializeTenancyByDomain; use Stancl\Tenancy\Tests\Etc\Tenant;