Migration.php
| 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 | * @since 6.9.26 https://github.com/aamplugin/advanced-access-manager/issues/357 |
| 14 | * @since 6.0.0 Initial implementation of the class |
| 15 | * |
| 16 | * @package AAM |
| 17 | * @version 6.9.26 |
| 18 | */ |
| 19 | final class AAM_Core_Migration |
| 20 | { |
| 21 | |
| 22 | /** |
| 23 | * DB option that stores list of migration scripts that were completed |
| 24 | * |
| 25 | * @version 6.0.0 |
| 26 | */ |
| 27 | const DB_OPTION = 'aam_migrations'; |
| 28 | |
| 29 | /** |
| 30 | * DB option that stores the entire migration log |
| 31 | * |
| 32 | * @version 6.0.0 |
| 33 | */ |
| 34 | const DB_FAILURE_OPTION = 'aam_migration_failures'; |
| 35 | |
| 36 | /** |
| 37 | * Run the pending scripts |
| 38 | * |
| 39 | * @return void |
| 40 | * |
| 41 | * @access public |
| 42 | * @version 6.9.10 |
| 43 | */ |
| 44 | public static function run() |
| 45 | { |
| 46 | foreach(self::getPending() as $script) { |
| 47 | self::executeScript($script); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get list of migrations that are still pending to be executed |
| 53 | * |
| 54 | * @return array |
| 55 | * |
| 56 | * @since 6.9.26 https://github.com/aamplugin/advanced-access-manager/issues/357 |
| 57 | * @since 6.3.0 Optimized AAM_Core_API::getOption call |
| 58 | * @since 6.0.0 Initial implementation of the method |
| 59 | * |
| 60 | * @access public |
| 61 | * @version 6.9.26 |
| 62 | */ |
| 63 | public static function getPending() |
| 64 | { |
| 65 | $completed = AAM_Core_API::getOption(self::DB_OPTION); |
| 66 | $pending = array(); |
| 67 | $iterator = self::getDirectoryIterator(); |
| 68 | |
| 69 | if (is_a($iterator, DirectoryIterator::class)) { |
| 70 | foreach ($iterator as $mg) { |
| 71 | if ($mg->isFile() && !in_array($mg->getFilename(), $completed, true)) { |
| 72 | $pending[] = $mg->getPathname(); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return $pending; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Store failure log |
| 82 | * |
| 83 | * @param array $log |
| 84 | * |
| 85 | * @return boolean |
| 86 | * |
| 87 | * @access public |
| 88 | * @version 6.0.0 |
| 89 | */ |
| 90 | public static function storeFailureLog($log) |
| 91 | { |
| 92 | return AAM_Core_API::updateOption(self::DB_FAILURE_OPTION, $log); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get migration failure log |
| 97 | * |
| 98 | * @return array |
| 99 | * |
| 100 | * @since 6.3.0 Optimized AAM_Core_API::getOption call |
| 101 | * @since 6.0.0 Initial implementation of the method |
| 102 | * |
| 103 | * @access public |
| 104 | * @version 6.3.0 |
| 105 | */ |
| 106 | public static function getFailureLog() |
| 107 | { |
| 108 | return AAM_Core_API::getOption(self::DB_FAILURE_OPTION); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Clear failure log from the database |
| 113 | * |
| 114 | * @return boolean |
| 115 | * |
| 116 | * @access public |
| 117 | * @version 6.0.1 |
| 118 | */ |
| 119 | public static function resetFailureLog() |
| 120 | { |
| 121 | return AAM_Core_API::deleteOption(self::DB_FAILURE_OPTION); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Store completed script |
| 126 | * |
| 127 | * @param string $file_name |
| 128 | * |
| 129 | * @return boolean |
| 130 | * |
| 131 | * @since 6.3.0 Optimized AAM_Core_API::getOption call |
| 132 | * @since 6.0.0 Initial implementation of the method |
| 133 | * |
| 134 | * @access public |
| 135 | * @version 6.3.0 |
| 136 | */ |
| 137 | public static function storeCompletedScript($file_name) |
| 138 | { |
| 139 | $completed = AAM_Core_API::getOption(self::DB_OPTION); |
| 140 | $completed[] = $file_name; |
| 141 | |
| 142 | return AAM_Core_API::updateOption(self::DB_OPTION, $completed); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Execute migration script |
| 147 | * |
| 148 | * @param string $file_path |
| 149 | * |
| 150 | * @return array |
| 151 | * |
| 152 | * @access public |
| 153 | * @version 6.0.0 |
| 154 | */ |
| 155 | public static function executeScript($file_path) |
| 156 | { |
| 157 | if (file_exists($file_path)) { |
| 158 | $results = include $file_path; |
| 159 | } else { |
| 160 | $results = array(); |
| 161 | } |
| 162 | |
| 163 | return $results; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Check if there is at least one pending migration script |
| 168 | * |
| 169 | * @return boolean |
| 170 | * |
| 171 | * @access public |
| 172 | * @version 6.0.0 |
| 173 | */ |
| 174 | public static function hasPending() |
| 175 | { |
| 176 | return (count(self::getPending()) > 0); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Get migration scripts directory iterator |
| 181 | * |
| 182 | * @return DirectoryIterator |
| 183 | * |
| 184 | * @since 6.9.26 https://github.com/aamplugin/advanced-access-manager/issues/357 |
| 185 | * @since 6.0.0 Initial implementation of the method |
| 186 | * |
| 187 | * @access protected |
| 188 | * @version 6.9.26 |
| 189 | */ |
| 190 | protected static function getDirectoryIterator() |
| 191 | { |
| 192 | $iterator = null; |
| 193 | $dirname = dirname(__DIR__) . '/Migration'; |
| 194 | |
| 195 | if (file_exists($dirname)) { |
| 196 | $iterator = new DirectoryIterator($dirname); |
| 197 | } |
| 198 | |
| 199 | return $iterator; |
| 200 | } |
| 201 | |
| 202 | } |