MigrationHelper.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\MigrationLog\Helpers; |
| 4 | |
| 5 | use Give\Framework\Migrations\Contracts\Migration; |
| 6 | use Give\Framework\Migrations\MigrationsRegister; |
| 7 | use Give\MigrationLog\MigrationLogModel; |
| 8 | use Give\MigrationLog\MigrationLogRepository; |
| 9 | |
| 10 | /** |
| 11 | * Class MigrationOrder |
| 12 | * @package Give\MigrationLog\Helpers |
| 13 | * |
| 14 | * Helper class used to get migration data |
| 15 | * |
| 16 | * @since 2.10.0 |
| 17 | */ |
| 18 | class MigrationHelper { |
| 19 | |
| 20 | /** |
| 21 | * @var MigrationsRegister |
| 22 | */ |
| 23 | private $migrationRegister; |
| 24 | |
| 25 | /** |
| 26 | * @var MigrationLogRepository |
| 27 | */ |
| 28 | private $migrationRepository; |
| 29 | |
| 30 | /** |
| 31 | * @var MigrationLogModel[] |
| 32 | */ |
| 33 | private $migrationsInDatabase; |
| 34 | |
| 35 | /** |
| 36 | * MigrationOrder constructor. |
| 37 | * |
| 38 | * @param MigrationsRegister $migrationRegister |
| 39 | * @param MigrationLogRepository $migrationRepository |
| 40 | */ |
| 41 | public function __construct( |
| 42 | MigrationsRegister $migrationRegister, |
| 43 | MigrationLogRepository $migrationRepository |
| 44 | ) { |
| 45 | $this->migrationRegister = $migrationRegister; |
| 46 | $this->migrationRepository = $migrationRepository; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get migrations sorted by run order |
| 51 | * |
| 52 | * @since 2.10.0 |
| 53 | * |
| 54 | * @return array |
| 55 | */ |
| 56 | private function getMigrationsSorted() { |
| 57 | static $migrations = []; |
| 58 | |
| 59 | if ( empty( $migrations ) ) { |
| 60 | /* @var Migration $migrationClass */ |
| 61 | foreach ( $this->migrationRegister->getMigrations() as $migrationClass ) { |
| 62 | $migrations[ $migrationClass::timestamp() . '_' . $migrationClass::id() ] = $migrationClass::id(); |
| 63 | } |
| 64 | |
| 65 | ksort( $migrations ); |
| 66 | } |
| 67 | |
| 68 | return $migrations; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get pending migrations |
| 73 | * |
| 74 | * @since 2.10.0 |
| 75 | * |
| 76 | * @return string[] |
| 77 | */ |
| 78 | public function getPendingMigrations() { |
| 79 | return array_filter( |
| 80 | $this->migrationRegister->getMigrations(), |
| 81 | function( $migrationClass ) { |
| 82 | /* @var Migration $migrationClass */ |
| 83 | foreach ( $this->getMigrationsInDatabase() as $migration ) { |
| 84 | if ( $migration->getId() === $migrationClass::id() ) { |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | return true; |
| 89 | } |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get migration run order |
| 95 | * |
| 96 | * @since 2.10.0 |
| 97 | * |
| 98 | * @param string $migrationId |
| 99 | * |
| 100 | * @return int |
| 101 | */ |
| 102 | public function getRunOrderForMigration( $migrationId ) { |
| 103 | return array_search( $migrationId, array_values( $this->getMigrationsSorted() ) ) + 1; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Retrieves the migrations from the database, caching the results for future retrieval |
| 108 | * |
| 109 | * @since 2.10.1 |
| 110 | * |
| 111 | * @return MigrationLogModel[] |
| 112 | */ |
| 113 | private function getMigrationsInDatabase() { |
| 114 | if ( $this->migrationsInDatabase === null ) { |
| 115 | $this->migrationsInDatabase = $this->migrationRepository->getMigrations(); |
| 116 | } |
| 117 | |
| 118 | return $this->migrationsInDatabase; |
| 119 | } |
| 120 | } |
| 121 |