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

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