PluginProbe
seQura / 4.3.3
seQura v4.3.3
4.3.4 4.3.3 4.3.2 4.3.1 trunk 2.0.0 2.0.10 2.0.11 2.0.12 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.2 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 4.0.0 All 30 releases
sequra / src / Services / Migration / class-migration-manager.php

class-migration-manager.php in seQura 4.3.3, at src/Services/Migration/class-migration-manager.php

160 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Core\Infrastructure\Configuration\ConfigurationManager;
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 * Configuration key for the module version.
23 */
24 private const CONF_VERSION = 'version';
25
26 /**
27 * Migration list.
28 *
29 * @var Migration[]
30 */
31 private $migrations;
32
33 /**
34 * Current version of the plugin.
35 *
36 * @var string
37 */
38 private $current_version;
39
40 /**
41 * Plugin basename.
42 *
43 * @var string
44 */
45 private $plugin_basename;
46
47 /**
48 * Configuration manager.
49 *
50 * @var ConfigurationManager
51 */
52 private $configuration_manager;
53
54 private const MIGRATION_LOCK = 'sequra_migration_lock';
55
56 /**
57 * Constructor
58 *
59 * @param Migration[] $migrations Migration list.
60 */
61 public function __construct(
62 string $plugin_basename,
63 ConfigurationManager $configuration_manager,
64 string $current_version,
65 array $migrations
66 ) {
67 $this->plugin_basename = $plugin_basename;
68 $this->migrations = $migrations;
69 $this->current_version = $current_version;
70 $this->configuration_manager = $configuration_manager;
71 }
72
73 /**
74 * Migration to run when the plugin was updated.
75 */
76 public function run_install_migrations(): void {
77 $db_module_version = $this->get_module_version();
78 if ( -1 !== version_compare( $db_module_version, $this->current_version ) ) {
79 return;
80 }
81
82 if ( (bool) \get_transient( self::MIGRATION_LOCK ) ) {
83 return;
84 }
85
86 set_transient( self::MIGRATION_LOCK, 1, 10 * 60 ); // Set 10 minutes as the maximum time to run the migrations.
87 $deactivate = false;
88
89 try {
90 foreach ( $this->migrations as $migration ) {
91 if ( -1 === version_compare( $db_module_version, $migration->get_version() ) ) {
92 $migration->run();
93 $this->set_module_version( $migration->get_version() );
94 }
95 }
96 // Prevent the migrations from running again for the same plugin version.
97 $this->set_module_version( $this->current_version );
98 } catch ( Critical_Migration_Exception $e ) {
99 // ! Critical migration failed and the plugin cannot work properly. Deactivate the plugin and log the error.
100 $deactivate = true;
101 $this->log_error( $e );
102 } catch ( Throwable $e ) {
103 // ! Non-critical migration failed. Stop the process and log the error.
104 $this->log_error( $e );
105 } finally {
106 \delete_transient( self::MIGRATION_LOCK );
107 if ( $deactivate ) {
108 \deactivate_plugins( $this->plugin_basename );
109 }
110 }
111 }
112
113 /**
114 * Migrations to run when the plugin is uninstalled.
115 */
116 public function run_uninstall_migrations(): void {
117 // Implement run_uninstall_migrations() method.
118 }
119
120 /**
121 * Log an error using the default logger instead of the
122 * existing logger service that relies on the database
123 * configuration that may not be available during the
124 * migration process.
125 */
126 private function log_error( Throwable $error ): void {
127 if ( defined( 'WP_DEBUG' ) && WP_DEBUG && ( ! defined( 'WP_DEBUG_LOG' ) || ! empty( WP_DEBUG_LOG ) ) ) {
128 // phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r
129 error_log(
130 print_r(
131 array(
132 'error' => $error->getMessage(),
133 'file' => $error->getFile(),
134 'line' => $error->getLine(),
135 'trace' => $error->getTraceAsString(),
136 ),
137 true
138 )
139 );
140 // phpcs:enable WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r
141 }
142 }
143
144 /**
145 * Gets the current version of the module/integration.
146 */
147 public function get_module_version(): string {
148 return strval( $this->configuration_manager->getConfigValue( self::CONF_VERSION, '' ) );
149 }
150
151 /**
152 * Gets the current version of the module/integration.
153 *
154 * @param string $version The version number.
155 */
156 public function set_module_version( $version ): void {
157 $this->configuration_manager->saveConfigValue( self::CONF_VERSION, $version );
158 }
159 }
160