Skip to content

Commit

Permalink
[1.x] Adds support for multiple closures
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Mar 15, 2024
1 parent 2b54f8c commit 0f8c03a
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/CustomizableMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
)
Expand Down Expand Up @@ -80,38 +81,38 @@ 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;
}

/**
* 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;
}

/**
* 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;
}
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
Expand Down

0 comments on commit 0f8c03a

Please sign in to comment.