Actions
5 years ago
Contracts
5 years ago
Controllers
5 years ago
Exceptions
5 years ago
MigrationsRegister.php
5 years ago
MigrationsRunner.php
5 years ago
MigrationsServiceProvider.php
5 years ago
MigrationsRegister.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\Migrations; |
| 4 | |
| 5 | use Give\Framework\Migrations\Contracts\Migration; |
| 6 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 7 | |
| 8 | class MigrationsRegister { |
| 9 | /** |
| 10 | * FQCN of Migration classes |
| 11 | * |
| 12 | * @since 2.9.0 |
| 13 | * |
| 14 | * @var string[] |
| 15 | */ |
| 16 | private $migrations = []; |
| 17 | |
| 18 | /** |
| 19 | * Returns all of the registered migrations |
| 20 | * |
| 21 | * @since 2.9.0 |
| 22 | * |
| 23 | * @return string[] |
| 24 | */ |
| 25 | public function getMigrations() { |
| 26 | return $this->migrations; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Checks to see if a migration is registered with the given ID |
| 31 | * |
| 32 | * @since 2.9.2 |
| 33 | * |
| 34 | * @param string $id |
| 35 | * |
| 36 | * @return bool |
| 37 | */ |
| 38 | public function hasMigration( $id ) { |
| 39 | return isset( $this->migrations[ $id ] ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Returns a migration with the given ID |
| 44 | * |
| 45 | * @since 2.9.2 |
| 46 | * |
| 47 | * @param string $id |
| 48 | * |
| 49 | * @return string |
| 50 | */ |
| 51 | public function getMigration( $id ) { |
| 52 | if ( ! isset( $this->migrations[ $id ] ) ) { |
| 53 | throw new InvalidArgumentException( "No migration exists with the ID {$id}" ); |
| 54 | } |
| 55 | |
| 56 | return $this->migrations[ $id ]; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns all of the registered migration ids |
| 61 | * |
| 62 | * @since 2.9.0 |
| 63 | * |
| 64 | * @return string[] |
| 65 | */ |
| 66 | public function getRegisteredIds() { |
| 67 | return array_keys( $this->migrations ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Add a migration to the list of migrations |
| 72 | * |
| 73 | * @since 2.9.0 |
| 74 | * |
| 75 | * @param string $migrationClass FQCN of the Migration Class |
| 76 | */ |
| 77 | public function addMigration( $migrationClass ) { |
| 78 | if ( ! is_subclass_of( $migrationClass, Migration::class ) ) { |
| 79 | throw new InvalidArgumentException( 'Class must extend the ' . Migration::class . ' class' ); |
| 80 | } |
| 81 | |
| 82 | $migrationId = $migrationClass::id(); |
| 83 | |
| 84 | if ( isset( $this->migrations[ $migrationId ] ) ) { |
| 85 | throw new InvalidArgumentException( 'A migration can only be added once. Make sure there are not id conflicts.' ); |
| 86 | } |
| 87 | |
| 88 | $this->migrations[ $migrationId ] = $migrationClass; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Helper for adding a bunch of migrations at once |
| 93 | * |
| 94 | * @since 2.9.0 |
| 95 | * |
| 96 | * @param string[] $migrationClasses |
| 97 | */ |
| 98 | public function addMigrations( array $migrationClasses ) { |
| 99 | foreach ( $migrationClasses as $migrationClass ) { |
| 100 | $this->addMigration( $migrationClass ); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 |