From 1217f0f6b39251b6a044da9b1f014fc834fcd896 Mon Sep 17 00:00:00 2001 From: Kurt Friars Date: Thu, 6 Feb 2025 19:07:24 -0500 Subject: [PATCH 1/4] Fix github action dependencies --- .github/workflows/run-tests.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 727ba3c..05fe4b8 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -47,7 +47,6 @@ jobs: - name: Install dependencies run: | - composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update composer update --${{ matrix.stability }} --prefer-dist --no-interaction - name: List Installed Dependencies @@ -93,7 +92,6 @@ jobs: - name: Install dependencies run: | - composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update composer update --${{ matrix.stability }} --prefer-dist --no-interaction - name: List Installed Dependencies From c5abe408a0096988adde4d78cd064b02e9ff8671 Mon Sep 17 00:00:00 2001 From: Kurt Friars Date: Fri, 7 Feb 2025 11:00:33 -0500 Subject: [PATCH 2/4] Make the Conflict Schema be resolved from the container --- src/Listeners/DetectSchemaConflicts.php | 22 ++++++++-------------- src/PublisherServiceProvider.php | 16 ++++++++++++++-- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/Listeners/DetectSchemaConflicts.php b/src/Listeners/DetectSchemaConflicts.php index 797c3e4..bdbf298 100644 --- a/src/Listeners/DetectSchemaConflicts.php +++ b/src/Listeners/DetectSchemaConflicts.php @@ -49,20 +49,14 @@ protected function usingConflictSchema(Closure $callback): Collection { $active = $this->container->make('db.schema'); - /** @var Connection $db */ - $db = $this->container->make('db.connection'); - - /** @var class-string $class */ - $class = config()->get('publisher.conflicts.schema'); - $schema = new $class($db); - - $this->container->instance('db.schema', $schema); - - $db->pretend(function () use ($callback) { - return $callback(); - }); - - $this->container->instance('db.schema', $active); + try { + /** @var Builder&DetectsConflicts $schema */ + $schema = $this->container->make(DetectsConflicts::class); + $this->container->instance('db.schema', $schema); + $schema->getConnection()->pretend(fn () => $callback()); + } finally { + $this->container->instance('db.schema', $active); + } return $schema->getConflicts(); } diff --git a/src/PublisherServiceProvider.php b/src/PublisherServiceProvider.php index 3ac67de..f3c68f2 100644 --- a/src/PublisherServiceProvider.php +++ b/src/PublisherServiceProvider.php @@ -7,6 +7,7 @@ use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Gate; use Plank\Publisher\Commands\PublisherMigrations; +use Plank\Publisher\Contracts\DetectsConflicts; use Plank\Publisher\Middleware\PublisherMiddleware; use Plank\Publisher\Routing\PublisherUrlGenerator; use Plank\Publisher\Services\PublisherService; @@ -129,10 +130,21 @@ protected function registerMiddleware(): self protected function listenForSchemaChanges(): self { - if ($resolver = config()->get('publisher.conflicts.listener')) { - Event::listen(MigrationEnded::class, $resolver); + $resolver = config()->get('publisher.conflicts.listener'); + + if ($resolver === null) { + return $this; } + $this->app->bindIf(DetectsConflicts::class, function () { + $db = $this->app->make('db.connection'); + $class = config()->get('publisher.conflicts.schema'); + + return new $class($db); + }); + + Event::listen(MigrationEnded::class, $resolver); + return $this; } } From b88bebe663bb4f4573806b2e2dd8cdaa703c010e Mon Sep 17 00:00:00 2001 From: kfriars <3378675+kfriars@users.noreply.github.com> Date: Fri, 7 Feb 2025 16:01:02 +0000 Subject: [PATCH 3/4] Fix styling --- src/Listeners/DetectSchemaConflicts.php | 1 - src/PublisherServiceProvider.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Listeners/DetectSchemaConflicts.php b/src/Listeners/DetectSchemaConflicts.php index bdbf298..7d98262 100644 --- a/src/Listeners/DetectSchemaConflicts.php +++ b/src/Listeners/DetectSchemaConflicts.php @@ -4,7 +4,6 @@ use Closure; use Illuminate\Container\Container; -use Illuminate\Database\Connection; use Illuminate\Database\Events\MigrationEnded; use Illuminate\Database\Schema\Builder; use Illuminate\Support\Collection; diff --git a/src/PublisherServiceProvider.php b/src/PublisherServiceProvider.php index f3c68f2..def153f 100644 --- a/src/PublisherServiceProvider.php +++ b/src/PublisherServiceProvider.php @@ -139,7 +139,7 @@ protected function listenForSchemaChanges(): self $this->app->bindIf(DetectsConflicts::class, function () { $db = $this->app->make('db.connection'); $class = config()->get('publisher.conflicts.schema'); - + return new $class($db); }); From 1c06e6fcdad5619bf29067e0ba41e0f5f60c8fd0 Mon Sep 17 00:00:00 2001 From: Kurt Friars Date: Fri, 7 Feb 2025 15:28:16 -0500 Subject: [PATCH 4/4] Add convenience method getPublishingDependents for package consumers --- src/Concerns/SyncsPublishing.php | 14 +++++++++++--- src/Contracts/Publishable.php | 7 +++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Concerns/SyncsPublishing.php b/src/Concerns/SyncsPublishing.php index 679a9d1..bbd9bb1 100644 --- a/src/Concerns/SyncsPublishing.php +++ b/src/Concerns/SyncsPublishing.php @@ -34,12 +34,20 @@ public static function bootSyncsPublishing() public function syncPublishingToDependents(): void { - $this->publishingDependents() - ->map(fn (string $relation) => $this->nestedPluck($relation)) - ->flatten() + $this->getPublishingDependents() ->each(fn (Publishable&Model $model) => $model->syncPublishingFrom($this)); } + /** + * @return Collection + */ + public function getPublishingDependents(): Collection + { + return $this->publishingDependents() + ->map(fn (string $relation) => $this->nestedPluck($relation)) + ->flatten(); + } + public function publishingDependents(): Collection { if (property_exists($this, 'publishingDependents')) { diff --git a/src/Contracts/Publishable.php b/src/Contracts/Publishable.php index 329d01e..64dcfc3 100644 --- a/src/Contracts/Publishable.php +++ b/src/Contracts/Publishable.php @@ -107,6 +107,13 @@ public function syncPublishingFrom(Publishable&Model $from): void; */ public function publishingDependents(): Collection; + /** + * Resolve the publishing dependent models + * + * @return Collection + */ + public function getPublishingDependents(): Collection; + /** * Queue the model for deletion if it's owner is not published */