PluginProbe
Boxzilla – WordPress Popup Builder / 3.1.17
Boxzilla – WordPress Popup Builder v3.1.17
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.1.17, at src/admin/class-migrations.php

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