From 79ac1474068f2c6fb80a745c0396dd8dad017e75 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 6 Sep 2023 00:27:46 +0100 Subject: [PATCH 1/3] Fix blueprint error when publishing Eloquent Driver migrations --- src/ServiceProvider.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 769a5dcf..aebdf116 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -7,6 +7,7 @@ use DoubleThreeDigital\Runway\Search\Searchable; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Gate; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Traits\Conditionable; use Statamic\Facades\CP\Nav; use Statamic\Facades\GraphQL; @@ -74,7 +75,17 @@ public function boot() ], 'runway-config'); Statamic::booted(function () { - Runway::discoverResources(); + // We're try/catching this to cover cases where blueprints aren't available yet + // (eg. migrating to the Eloquent driver but haven't yet published the migrations) + try { + Runway::discoverResources(); + } catch (\Exception $e) { + if (! $this->app->runningInConsole()) { + throw $e; + } + + Log::error($e->getMessage()); + } $this->registerPermissions(); $this->registerPolicies(); From e33db9c78aafca34117773b5ca29f49971dab951 Mon Sep 17 00:00:00 2001 From: duncanmcclean Date: Tue, 5 Sep 2023 23:28:11 +0000 Subject: [PATCH 2/3] Fix styling --- src/Routing/RoutingModel.php | 2 +- src/Runway.php | 2 +- src/Search/Searchable.php | 4 ++-- src/Traits/HasRunwayResource.php | 2 +- tests/TestCase.php | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Routing/RoutingModel.php b/src/Routing/RoutingModel.php index e941ea45..008fd462 100644 --- a/src/Routing/RoutingModel.php +++ b/src/Routing/RoutingModel.php @@ -10,7 +10,7 @@ use Statamic\Data\ContainsSupplementalData; use Statamic\Data\HasAugmentedData; -class RoutingModel implements Responsable, Augmentable +class RoutingModel implements Augmentable, Responsable { use ContainsSupplementalData, HasAugmentedData; diff --git a/src/Runway.php b/src/Runway.php index 30cce1a3..f812e48f 100644 --- a/src/Runway.php +++ b/src/Runway.php @@ -74,7 +74,7 @@ public static function findResource(string $resourceHandle): ?Resource public static function findResourceByModel(object $model): ?Resource { - $resource = collect(static::$resources)->filter(fn (Resource $resource) => $resource->model()::class === $model::class)->first(); + $resource = collect(static::$resources)->filter(fn (Resource $resource) => $model::class === $resource->model()::class)->first(); if (! $resource) { throw new ResourceNotFound($model::class); diff --git a/src/Search/Searchable.php b/src/Search/Searchable.php index 64b6ea2d..2457d65b 100644 --- a/src/Search/Searchable.php +++ b/src/Search/Searchable.php @@ -14,9 +14,9 @@ use Statamic\Facades\Site; use Statamic\Search\Result as ResultInstance; -class Searchable implements Contract, ContainsQueryableValues, Augmentable +class Searchable implements Augmentable, ContainsQueryableValues, Contract { - use HasAugmentedInstance, ContainsSupplementalData; + use ContainsSupplementalData, HasAugmentedInstance; protected $model; diff --git a/src/Traits/HasRunwayResource.php b/src/Traits/HasRunwayResource.php index bf215d67..61a3660a 100644 --- a/src/Traits/HasRunwayResource.php +++ b/src/Traits/HasRunwayResource.php @@ -12,7 +12,7 @@ trait HasRunwayResource { - use HasAugmentedInstance, FluentlyGetsAndSets; + use FluentlyGetsAndSets, HasAugmentedInstance; use ResolvesValues { resolveGqlValue as traitResolveGqlValue; } diff --git a/tests/TestCase.php b/tests/TestCase.php index 2b1f100e..f86d9006 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -246,7 +246,7 @@ public function authorFactory(int $count = 1, array $attributes = []) class Post extends Model { - use RunwayRoutes, HasRunwayResource; + use HasRunwayResource, RunwayRoutes; protected $fillable = [ 'title', 'slug', 'body', 'values', 'author_id', 'sort_order', From e2bb865fc575146482be6c9f3c9a3ec3adf4afa3 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 6 Sep 2023 00:36:43 +0100 Subject: [PATCH 3/3] Re-work the fix --- src/Runway.php | 15 ++++++++++++--- src/ServiceProvider.php | 13 +------------ 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/Runway.php b/src/Runway.php index 30cce1a3..8df3f02e 100644 --- a/src/Runway.php +++ b/src/Runway.php @@ -26,13 +26,22 @@ public static function discoverResources(): self if (! in_array(Traits\HasRunwayResource::class, class_uses_recursive($model))) { throw new \Exception(__('The HasRunwayResource trait is missing from the [:model] model.', ['model' => $model])); } - if (! isset($config['blueprint'])) { throw new \Exception(__('The [:model] model is missing a blueprint.', ['model' => $model])); } if (is_string($config['blueprint'])) { - $blueprint = Blueprint::find($config['blueprint']); + try { + $blueprint = Blueprint::find($config['blueprint']); + } catch (\Exception $e) { + // If we're running in a console & the blueprint doesn't exist, let's ignore the resource. + // https://github.com/duncanmcclean/runway/pull/320 + if (app()->runningInConsole()) { + return; + } + + throw $e; + } } if (is_array($config['blueprint'])) { @@ -74,7 +83,7 @@ public static function findResource(string $resourceHandle): ?Resource public static function findResourceByModel(object $model): ?Resource { - $resource = collect(static::$resources)->filter(fn (Resource $resource) => $resource->model()::class === $model::class)->first(); + $resource = collect(static::$resources)->filter(fn (Resource $resource) => $model::class === $resource->model()::class)->first(); if (! $resource) { throw new ResourceNotFound($model::class); diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index aebdf116..769a5dcf 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -7,7 +7,6 @@ use DoubleThreeDigital\Runway\Search\Searchable; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Gate; -use Illuminate\Support\Facades\Log; use Illuminate\Support\Traits\Conditionable; use Statamic\Facades\CP\Nav; use Statamic\Facades\GraphQL; @@ -75,17 +74,7 @@ public function boot() ], 'runway-config'); Statamic::booted(function () { - // We're try/catching this to cover cases where blueprints aren't available yet - // (eg. migrating to the Eloquent driver but haven't yet published the migrations) - try { - Runway::discoverResources(); - } catch (\Exception $e) { - if (! $this->app->runningInConsole()) { - throw $e; - } - - Log::error($e->getMessage()); - } + Runway::discoverResources(); $this->registerPermissions(); $this->registerPolicies();