| 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 |
/** |
| 22 |
* @var MigrationsRegister |
| 23 |
*/ |
| 24 |
private $migrationRegister; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var MigrationLogRepository |
| 28 |
*/ |
| 29 |
private $migrationRepository; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var MigrationLogModel[] |
| 33 |
*/ |
| 34 |
private $migrationsInDatabase; |
| 35 |
|
| 36 |
/** |
| 37 |
* MigrationOrder constructor. |
| 38 |
* |
| 39 |
* @param MigrationsRegister $migrationRegister |
| 40 |
* @param MigrationLogRepository $migrationRepository |
| 41 |
*/ |
| 42 |
public function __construct( |
| 43 |
MigrationsRegister $migrationRegister, |
| 44 |
MigrationLogRepository $migrationRepository |
| 45 |
) { |
| 46 |
$this->migrationRegister = $migrationRegister; |
| 47 |
$this->migrationRepository = $migrationRepository; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get migrations sorted by run order |
| 52 |
* |
| 53 |
* @since 2.10.0 |
| 54 |
* |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
private function getMigrationsSorted() |
| 58 |
{ |
| 59 |
static $migrations = []; |
| 60 |
|
| 61 |
if (empty($migrations)) { |
| 62 |
/* @var Migration $migrationClass */ |
| 63 |
foreach ($this->migrationRegister->getMigrations() as $migrationClass) { |
| 64 |
$migrations[$migrationClass::id()] = $migrationClass::timestamp(); |
| 65 |
} |
| 66 |
|
| 67 |
asort($migrations); |
| 68 |
} |
| 69 |
|
| 70 |
return $migrations; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get pending migrations |
| 75 |
* |
| 76 |
* @since 2.10.0 |
| 77 |
* |
| 78 |
* @return string[] |
| 79 |
*/ |
| 80 |
public function getPendingMigrations() |
| 81 |
{ |
| 82 |
return array_filter( |
| 83 |
$this->migrationRegister->getMigrations(), |
| 84 |
function ($migrationClass) { |
| 85 |
/* @var Migration $migrationClass */ |
| 86 |
foreach ($this->getMigrationsInDatabase() as $migration) { |
| 87 |
if ($migration->getId() === $migrationClass::id()) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return true; |
| 93 |
} |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get migration run order |
| 99 |
* |
| 100 |
* @since 2.10.0 |
| 101 |
* |
| 102 |
* @param string $migrationId |
| 103 |
* |
| 104 |
* @return int |
| 105 |
*/ |
| 106 |
public function getRunOrderForMigration($migrationId) |
| 107 |
{ |
| 108 |
return array_search($migrationId, array_keys($this->getMigrationsSorted())) + 1; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Retrieves the migrations from the database, caching the results for future retrieval |
| 113 |
* |
| 114 |
* @since 2.10.1 |
| 115 |
* |
| 116 |
* @return MigrationLogModel[] |
| 117 |
*/ |
| 118 |
private function getMigrationsInDatabase() |
| 119 |
{ |
| 120 |
if ($this->migrationsInDatabase === null) { |
| 121 |
$this->migrationsInDatabase = $this->migrationRepository->getMigrations(); |
| 122 |
} |
| 123 |
|
| 124 |
return $this->migrationsInDatabase; |
| 125 |
} |
| 126 |
} |
| 127 |
|