| 1 |
<?php |
| 2 |
/** |
| 3 |
* Run migrations to make changes to the database. |
| 4 |
* |
| 5 |
* @package SeQura/WC |
| 6 |
* @subpackage SeQura/WC/Services |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SeQura\WC\Services\Migration; |
| 10 |
|
| 11 |
use SeQura\WC\Core\Extension\Infrastructure\Configuration\Configuration; |
| 12 |
use SeQura\WC\Repositories\Migrations\Critical_Migration_Exception; |
| 13 |
use SeQura\WC\Repositories\Migrations\Migration; |
| 14 |
use Throwable; |
| 15 |
|
| 16 |
/** |
| 17 |
* Run migrations to make changes to the database. |
| 18 |
*/ |
| 19 |
class Migration_Manager implements Interface_Migration_Manager { |
| 20 |
|
| 21 |
/** |
| 22 |
* Migration list. |
| 23 |
* |
| 24 |
* @var Migration[] |
| 25 |
*/ |
| 26 |
private $migrations; |
| 27 |
|
| 28 |
/** |
| 29 |
* Current version of the plugin. |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
private $current_version; |
| 34 |
|
| 35 |
/** |
| 36 |
* Plugin basename. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
private $plugin_basename; |
| 41 |
|
| 42 |
/** |
| 43 |
* Configuration service. |
| 44 |
* |
| 45 |
* @var Configuration |
| 46 |
*/ |
| 47 |
private $configuration; |
| 48 |
|
| 49 |
private const MIGRATION_LOCK = 'sequra_migration_lock'; |
| 50 |
|
| 51 |
/** |
| 52 |
* Constructor |
| 53 |
* |
| 54 |
* @param Migration[] $migrations Migration list. |
| 55 |
*/ |
| 56 |
public function __construct( string $plugin_basename, Configuration $configuration, string $current_version, array $migrations ) { |
| 57 |
$this->plugin_basename = $plugin_basename; |
| 58 |
$this->migrations = $migrations; |
| 59 |
$this->current_version = $current_version; |
| 60 |
$this->configuration = $configuration; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Migration to run when the plugin was updated. |
| 65 |
*/ |
| 66 |
public function run_install_migrations(): void { |
| 67 |
$db_module_version = $this->configuration->get_module_version(); |
| 68 |
if ( -1 !== version_compare( $db_module_version, $this->current_version ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if ( (bool) \get_transient( self::MIGRATION_LOCK ) ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
set_transient( self::MIGRATION_LOCK, 1, 10 * 60 ); // Set 10 minutes as the maximum time to run the migrations. |
| 77 |
$deactivate = false; |
| 78 |
|
| 79 |
try { |
| 80 |
foreach ( $this->migrations as $migration ) { |
| 81 |
if ( -1 === version_compare( $db_module_version, $migration->get_version() ) ) { |
| 82 |
$migration->run(); |
| 83 |
$this->configuration->set_module_version( $migration->get_version() ); |
| 84 |
} |
| 85 |
} |
| 86 |
// Prevent the migrations from running again for the same plugin version. |
| 87 |
$this->configuration->set_module_version( $this->current_version ); |
| 88 |
} catch ( Critical_Migration_Exception $e ) { |
| 89 |
// ! Critical migration failed and the plugin cannot work properly. Deactivate the plugin and log the error. |
| 90 |
$deactivate = true; |
| 91 |
$this->log_error( $e ); |
| 92 |
} catch ( Throwable $e ) { |
| 93 |
// ! Non-critical migration failed. Stop the process and log the error. |
| 94 |
$this->log_error( $e ); |
| 95 |
} finally { |
| 96 |
\delete_transient( self::MIGRATION_LOCK ); |
| 97 |
if ( $deactivate ) { |
| 98 |
\deactivate_plugins( $this->plugin_basename ); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Migrations to run when the plugin is uninstalled. |
| 105 |
*/ |
| 106 |
public function run_uninstall_migrations(): void { |
| 107 |
// Implement run_uninstall_migrations() method. |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Log an error using the default logger instead of the |
| 112 |
* existing logger service that relies on the database |
| 113 |
* configuration that may not be available during the |
| 114 |
* migration process. |
| 115 |
*/ |
| 116 |
private function log_error( Throwable $error ): void { |
| 117 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && ( ! defined( 'WP_DEBUG_LOG' ) || ! empty( WP_DEBUG_LOG ) ) ) { |
| 118 |
// phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 119 |
error_log( |
| 120 |
print_r( |
| 121 |
array( |
| 122 |
'error' => $error->getMessage(), |
| 123 |
'file' => $error->getFile(), |
| 124 |
'line' => $error->getLine(), |
| 125 |
'trace' => $error->getTraceAsString(), |
| 126 |
), |
| 127 |
true |
| 128 |
) |
| 129 |
); |
| 130 |
// phpcs:enable WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|