From 0f8c03a5b4f05aadaf1ce6986e92058b8dfefb80 Mon Sep 17 00:00:00 2001 From: Italo Date: Fri, 15 Mar 2024 04:09:51 -0300 Subject: [PATCH] [1.x] Adds support for multiple closures --- src/CustomizableMigration.php | 39 ++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/CustomizableMigration.php b/src/CustomizableMigration.php index 078371b..946d1e2 100644 --- a/src/CustomizableMigration.php +++ b/src/CustomizableMigration.php @@ -7,6 +7,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; +use function array_push; use function sprintf; use function strtolower; @@ -28,17 +29,17 @@ abstract class CustomizableMigration extends Migration * Create a new Customizable Migration instance. * * @param class-string<\Illuminate\Database\Eloquent\Model> $model - * @param (\Closure(\Illuminate\Database\Schema\Blueprint $table):void)|null $with - * @param (\Closure(\Illuminate\Database\Schema\Blueprint $table):void)|null $afterUp - * @param (\Closure(\Illuminate\Database\Schema\Blueprint $table):void)|null $beforeDown + * @param (\Closure(\Illuminate\Database\Schema\Blueprint $table):void)[] $with + * @param (\Closure(\Illuminate\Database\Schema\Blueprint $table):void)[] $afterUp + * @param (\Closure(\Illuminate\Database\Schema\Blueprint $table):void)[] $beforeDown * @param "numeric"|"uuid"|"ulid"|"" $morphType * @param string|null $morphIndexName */ public function __construct( string $model, - protected ?Closure $with = null, - protected ?Closure $afterUp = null, - protected ?Closure $beforeDown = null, + protected array $with = [], + protected array $afterUp = [], + protected array $beforeDown = [], protected string $morphType = '', protected ?string $morphIndexName = null, ) @@ -80,12 +81,12 @@ public function morph(string $type, string $indexName = null): static /** * Add additional columns to the table. * - * @param \Closure(\Illuminate\Database\Schema\Blueprint $table):void $callback + * @param (\Closure(\Illuminate\Database\Schema\Blueprint $table):void) ...$callbacks * @return $this */ - public function with(Closure $callback): static + public function with(Closure ...$callbacks): static { - $this->with = $callback; + array_push($this->with, ...$callbacks); return $this; } @@ -93,12 +94,12 @@ public function with(Closure $callback): static /** * Execute the callback after the "up" method. * - * @param \Closure(\Illuminate\Database\Schema\Blueprint $table):void $callback + * @param (\Closure(\Illuminate\Database\Schema\Blueprint $table):void) ...$callbacks * @return $this */ - public function afterUp(Closure $callback): static + public function afterUp(Closure ...$callbacks): static { - $this->afterUp = $callback; + array_push($this->afterUp, ...$callbacks); return $this; } @@ -106,12 +107,12 @@ public function afterUp(Closure $callback): static /** * Execute the callback before the "down" method. * - * @param \Closure(\Illuminate\Database\Schema\Blueprint $table):void $callback + * @param (\Closure(\Illuminate\Database\Schema\Blueprint $table):void) ...$callbacks * @return $this */ - public function beforeDown(Closure $callback): static + public function beforeDown(Closure ...$callbacks): static { - $this->beforeDown = $callback; + array_push($this->beforeDown, ...$callbacks); return $this; } @@ -168,8 +169,8 @@ public function up(): void { Schema::create($this->table, $this->create(...)); - if ($this->afterUp) { - Schema::table($this->table, $this->afterUp); + foreach ($this->afterUp as $callback) { + Schema::table($this->table, $callback); } } @@ -180,8 +181,8 @@ public function up(): void */ public function down(): void { - if ($this->beforeDown) { - Schema::table($this->table, $this->beforeDown); + foreach ($this->beforeDown as $callback) { + Schema::table($this->table, $callback); } Schema::dropIfExists($this->table);