PluginProbe
Boxzilla – WordPress Popup Builder / 3.2.18
Boxzilla – WordPress Popup Builder v3.2.18
3.4.11 3.4.10 3.4.9 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 trunk 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 All 72 releases
boxzilla / src / admin / class-migrations.php

class-migrations.php in Boxzilla – WordPress Popup Builder 3.2.18, at src/admin/class-migrations.php

97 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Boxzilla\Admin;
4
5 use Exception;
6
7 /**
8 *
9 */
10 class Migrations {
11
12
13 /**
14 * @var float
15 */
16 protected $version_from = 0;
17
18 /**
19 * @var float
20 */
21 protected $version_to = 0;
22
23 /**
24 * @var string
25 */
26 protected $migrations_dir = '';
27
28 /**
29 * @param float $from
30 * @param float $to
31 * @param string $migrations_dir
32 */
33 public function __construct( $from, $to, $migrations_dir ) {
34 $this->version_from = $from;
35 $this->version_to = $to;
36 $this->migrations_dir = $migrations_dir;
37 }
38
39 /**
40 * Run the various upgrade routines, all the way up to the latest version
41 */
42 public function run() {
43 $migrations = $this->find_migrations();
44 // run in sub-function for scope
45 array_map( array( $this, 'run_migration' ), $migrations );
46 }
47
48 /**
49 * @return array
50 */
51 public function find_migrations() {
52 $files = glob( rtrim( $this->migrations_dir, '/' ) . '/*.php' );
53 $migrations = array();
54
55 // return empty array when glob returns non-array value.
56 if ( ! is_array( $files ) ) {
57 return $migrations;
58 }
59
60 foreach ( $files as $file ) {
61 $migration = basename( $file );
62 $parts = explode( '-', $migration );
63 $version = $parts[0];
64
65 // check if migration file is not for an even higher version
66 if ( version_compare( $version, $this->version_to, '>' ) ) {
67 continue;
68 }
69
70 // check if we ran migration file before.
71 if ( version_compare( $this->version_from, $version, '>=' ) ) {
72 continue;
73 }
74
75 // schedule migration file for running
76 $migrations[] = $file;
77 }
78
79 return $migrations;
80 }
81
82 /**
83 * Include a migration file and runs it.
84 *
85 * @param string $file
86 *
87 * @throws Exception
88 */
89 protected function run_migration( $file ) {
90 if ( ! file_exists( $file ) ) {
91 throw new Exception( "Migration file $file does not exist." );
92 }
93
94 include $file;
95 }
96 }
97