Migration.php
82 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ====================================================================== |
| 5 | * LICENSE: This file is subject to the terms and conditions defined in * |
| 6 | * file 'license.txt', which is part of this source code package. * |
| 7 | * ====================================================================== |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * AAM Core Migration class |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @version 7.0.0 |
| 15 | */ |
| 16 | final class AAM_Core_Migration |
| 17 | { |
| 18 | |
| 19 | /** |
| 20 | * DB option that stores list of migration scripts that were completed |
| 21 | * |
| 22 | * @version 7.0.0 |
| 23 | */ |
| 24 | const DB_OPTION = 'aam_migrations'; |
| 25 | |
| 26 | /** |
| 27 | * Run the pending scripts |
| 28 | * |
| 29 | * @return void |
| 30 | * @access public |
| 31 | * |
| 32 | * @version 7.0.0 |
| 33 | */ |
| 34 | public static function run() |
| 35 | { |
| 36 | $completed = AAM::api()->db->read(self::DB_OPTION); |
| 37 | |
| 38 | foreach(self::get_pending() as $script) { |
| 39 | if (file_exists($script)) { |
| 40 | $results = include $script; |
| 41 | $completed[] = basename($script); |
| 42 | |
| 43 | AAM::api()->db->write(self::DB_OPTION, $completed); |
| 44 | } else { |
| 45 | $results = []; |
| 46 | } |
| 47 | |
| 48 | return $results; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get list of migrations that are still pending to be executed |
| 54 | * |
| 55 | * @return array |
| 56 | * @access public |
| 57 | * |
| 58 | * @version 7.0.0 |
| 59 | */ |
| 60 | public static function get_pending() |
| 61 | { |
| 62 | $completed = AAM::api()->db->read(self::DB_OPTION, []); |
| 63 | $pending = []; |
| 64 | $iterator = null; |
| 65 | $dirname = dirname(__DIR__) . '/Migration'; |
| 66 | |
| 67 | if (file_exists($dirname)) { |
| 68 | $iterator = new DirectoryIterator($dirname); |
| 69 | } |
| 70 | |
| 71 | if (is_a($iterator, DirectoryIterator::class)) { |
| 72 | foreach ($iterator as $mg) { |
| 73 | if ($mg->isFile() && !in_array($mg->getFilename(), $completed, true)) { |
| 74 | $pending[] = $mg->getPathname(); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return $pending; |
| 80 | } |
| 81 | |
| 82 | } |