| @@ -6,33 +6,34 @@ | ||
| 6 | 6 | |
| 7 | 7 | /** |
| 8 | 8 | * |
| 9 | 9 | */ |
| 10 | -class Migrations { | |
| 11 | - | |
| 10 | +class Migrations | |
| 11 | +{ | |
| 12 | 12 | /** |
| 13 | - * @var float | |
| 13 | + * @var string | |
| 14 | 14 | */ |
| 15 | - protected $version_from = 0; | |
| 15 | + protected $version_from; | |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | - * @var float | |
| 18 | + * @var string | |
| 19 | 19 | */ |
| 20 | - protected $version_to = 0; | |
| 20 | + protected $version_to; | |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * @var string |
| 24 | 24 | */ |
| 25 | - protected $migrations_dir = ''; | |
| 25 | + protected $migrations_dir; | |
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | - * @param float $from | |
| 29 | - * @param float $to | |
| 28 | + * @param string $from | |
| 29 | + * @param string $to | |
| 30 | 30 | * @param string $migrations_dir |
| 31 | 31 | */ |
| 32 | - public function __construct( $from, $to, $migrations_dir ) { | |
| 33 | - $this->version_from = $from; | |
| 34 | - $this->version_to = $to; | |
| 32 | + public function __construct($from, $to, $migrations_dir) | |
| 33 | + { | |
| 34 | + $this->version_from = $from; | |
| 35 | + $this->version_to = $to; | |
| 35 | 36 | $this->migrations_dir = $migrations_dir; |
| 36 | 37 | } |
| 37 | 38 | |
| 38 | 39 | /** |
| @@ -37,38 +38,40 @@ | ||
| 37 | 38 | |
| 38 | 39 | /** |
| 39 | 40 | * Run the various upgrade routines, all the way up to the latest version |
| 40 | 41 | */ |
| 41 | - public function run() { | |
| 42 | + public function run() | |
| 43 | + { | |
| 42 | 44 | $migrations = $this->find_migrations(); |
| 43 | 45 | // run in sub-function for scope |
| 44 | - array_map( array( $this, 'run_migration' ), $migrations ); | |
| 46 | + array_map([ $this, 'run_migration' ], $migrations); | |
| 45 | 47 | } |
| 46 | 48 | |
| 47 | 49 | /** |
| 48 | 50 | * @return array |
| 49 | 51 | */ |
| 50 | - public function find_migrations() { | |
| 51 | - $files = glob( rtrim( $this->migrations_dir, '/' ) . '/*.php' ); | |
| 52 | - $migrations = array(); | |
| 52 | + public function find_migrations() | |
| 53 | + { | |
| 54 | + $files = glob(rtrim($this->migrations_dir, '/') . '/*.php'); | |
| 55 | + $migrations = []; | |
| 53 | 56 | |
| 54 | 57 | // return empty array when glob returns non-array value. |
| 55 | - if( ! is_array( $files ) ) { | |
| 58 | + if (! is_array($files)) { | |
| 56 | 59 | return $migrations; |
| 57 | 60 | } |
| 58 | 61 | |
| 59 | - foreach( $files as $file ) { | |
| 60 | - $migration = basename( $file ); | |
| 61 | - $parts = explode( '-', $migration ); | |
| 62 | - $version = $parts[0]; | |
| 62 | + foreach ($files as $file) { | |
| 63 | + $migration = basename($file); | |
| 64 | + $parts = explode('-', $migration); | |
| 65 | + $version = $parts[0]; | |
| 63 | 66 | |
| 64 | 67 | // check if migration file is not for an even higher version |
| 65 | - if( version_compare( $version, $this->version_to, '>' ) ) { | |
| 68 | + if (version_compare($version, $this->version_to, '>')) { | |
| 66 | 69 | continue; |
| 67 | 70 | } |
| 68 | 71 | |
| 69 | 72 | // check if we ran migration file before. |
| 70 | - if( version_compare( $this->version_from, $version, '>=' ) ) { | |
| 73 | + if (version_compare($this->version_from, $version, '>=')) { | |
| 71 | 74 | continue; |
| 72 | 75 | } |
| 73 | 76 | |
| 74 | 77 | // schedule migration file for running |
| @@ -84,13 +87,13 @@ | ||
| 84 | 87 | * @param string $file |
| 85 | 88 | * |
| 86 | 89 | * @throws Exception |
| 87 | 90 | */ |
| 88 | - protected function run_migration( $file ) { | |
| 89 | - | |
| 90 | - if( ! file_exists( $file ) ) { | |
| 91 | - throw new Exception( "Migration file $file does not exist."); | |
| 91 | + protected function run_migration($file) | |
| 92 | + { | |
| 93 | + if (! file_exists($file)) { | |
| 94 | + throw new Exception(sprintf('Migration file %s does not exist.', esc_html(basename((string) $file)))); | |
| 92 | 95 | } |
| 93 | 96 | |
| 94 | 97 | include $file; |
| 95 | 98 | } |
| 96 | -} | |
| 99 | +} | |