PluginProbe
seQura / 3.0.6
seQura v3.0.6
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 3.0.6, at src/Services/Migration/class-migration-manager.php

132 lines 3.5 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\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 } catch ( Critical_Migration_Exception $e ) {
87 // ! Critical migration failed and the plugin cannot work properly. Deactivate the plugin and log the error.
88 $deactivate = true;
89 $this->log_error( $e );
90 } catch ( Throwable $e ) {
91 // ! Non-critical migration failed. Stop the process and log the error.
92 $this->log_error( $e );
93 } finally {
94 \delete_transient( self::MIGRATION_LOCK );
95 if ( $deactivate ) {
96 \deactivate_plugins( $this->plugin_basename );
97 }
98 }
99 }
100
101 /**
102 * Migrations to run when the plugin is uninstalled.
103 */
104 public function run_uninstall_migrations(): void {
105 // Implement run_uninstall_migrations() method.
106 }
107
108 /**
109 * Log an error using the default logger instead of the
110 * existing logger service that relies on the database
111 * configuration that may not be available during the
112 * migration process.
113 */
114 private function log_error( Throwable $error ): void {
115 if ( defined( 'WP_DEBUG' ) && WP_DEBUG && ( ! defined( 'WP_DEBUG_LOG' ) || ! empty( WP_DEBUG_LOG ) ) ) {
116 // phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r
117 error_log(
118 print_r(
119 array(
120 'error' => $error->getMessage(),
121 'file' => $error->getFile(),
122 'line' => $error->getLine(),
123 'trace' => $error->getTraceAsString(),
124 ),
125 true
126 )
127 );
128 // phpcs:enable WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r
129 }
130 }
131 }
132