|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EE\Migration; |
| 4 | + |
| 5 | +use EE; |
| 6 | +use EE\Migration\Base; |
| 7 | +use EE\RevertableStepProcessor; |
| 8 | +use EE\Model\Site; |
| 9 | + |
| 10 | +class AddExtConfig8_1 extends Base { |
| 11 | + |
| 12 | + private $sites; |
| 13 | + /** @var RevertableStepProcessor $rsp Keeps track of migration state. Reverts on error */ |
| 14 | + private static $rsp; |
| 15 | + |
| 16 | + public function __construct() { |
| 17 | + |
| 18 | + parent::__construct(); |
| 19 | + $this->sites = Site::all(); |
| 20 | + if ( $this->is_first_execution || ! $this->sites ) { |
| 21 | + $this->skip_this_migration = true; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public function remove_extension( $config_file_tmp, $config_site_path ) { |
| 26 | + if ( $this->fs->exists( $config_file_tmp ) ) { |
| 27 | + $this->fs->remove( $config_file_tmp ); |
| 28 | + } |
| 29 | + |
| 30 | + if ( $this->fs->exists( $config_site_path ) ) { |
| 31 | + $this->fs->remove( $config_site_path ); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public function add_extension( $file_name, $extension_name, $site_fs_path ) { |
| 36 | + |
| 37 | + $config_file_tmp = EE\Utils\trailingslashit( EE_BACKUP_DIR ) . $file_name; |
| 38 | + $this->fs->dumpFile( $config_file_tmp, "extension=$extension_name" ); |
| 39 | + |
| 40 | + $config_site_path = $site_fs_path . '/config/php/php/conf.d/' . $file_name; |
| 41 | + |
| 42 | + self::$rsp->add_step( |
| 43 | + "to-$site_fs_path-add-$file_name", |
| 44 | + 'EE\Migration\SiteContainers::backup_restore', |
| 45 | + [ $this, 'remove_extension' ], |
| 46 | + [ $config_file_tmp, $config_site_path ], |
| 47 | + [ $config_file_tmp, $config_site_path ] |
| 48 | + ); |
| 49 | + |
| 50 | + if ( ! self::$rsp->execute() ) { |
| 51 | + throw new \Exception( 'Unable to run add-ext-config-8.1 upadte migrations.' ); |
| 52 | + } |
| 53 | + |
| 54 | + $this->fs->remove( $config_file_tmp ); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Execute php config updates. |
| 60 | + * |
| 61 | + * @throws EE\ExitException |
| 62 | + */ |
| 63 | + public function up() { |
| 64 | + |
| 65 | + if ( $this->skip_this_migration ) { |
| 66 | + EE::debug( 'Skipping add-ext-config-8.1 update migration as it is not needed.' ); |
| 67 | + |
| 68 | + return; |
| 69 | + } |
| 70 | + self::$rsp = new RevertableStepProcessor(); |
| 71 | + |
| 72 | + foreach ( $this->sites as $site ) { |
| 73 | + |
| 74 | + if ( ! in_array( $site->site_type, [ 'php', 'wp' ], true ) ) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + if ( $site->php_version != '8.1' ) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + EE::debug( "Found site: $site->site_url of type: $site->site_type" ); |
| 83 | + EE::debug( "Starting add-ext-config-8.1 updates for: $site->site_url" ); |
| 84 | + |
| 85 | + $this->add_extension( |
| 86 | + 'docker-php-ext-timezonedb.ini', |
| 87 | + 'timezonedb', |
| 88 | + $site->site_fs_path |
| 89 | + ); |
| 90 | + |
| 91 | + $this->add_extension( |
| 92 | + 'docker-php-ext-apcu.ini', |
| 93 | + 'apcu', |
| 94 | + $site->site_fs_path |
| 95 | + ); |
| 96 | + |
| 97 | + $this->add_extension( |
| 98 | + 'docker-php-ext-mcrypt.ini', |
| 99 | + 'mcrypt', |
| 100 | + $site->site_fs_path |
| 101 | + ); |
| 102 | + |
| 103 | + $this->add_extension( |
| 104 | + 'docker-php-ext-redis.ini', |
| 105 | + 'redis', |
| 106 | + $site->site_fs_path |
| 107 | + ); |
| 108 | + |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Bring back the existing old php config. |
| 114 | + * |
| 115 | + * @throws EE\ExitException |
| 116 | + */ |
| 117 | + public function down() { |
| 118 | + |
| 119 | + if ( $this->skip_this_migration ) { |
| 120 | + EE::debug( 'Skipping add-ext-config-8.1 update migration as it is not needed.' ); |
| 121 | + |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + foreach ( $this->sites as $site ) { |
| 126 | + |
| 127 | + if ( ! in_array( $site->site_type, [ 'php', 'wp' ], true ) ) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + if ( '8.1' != $site->php_version ) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + EE::debug( "Reverting add-ext-config updates for: $site->site_url" ); |
| 136 | + |
| 137 | + $configs = [ |
| 138 | + $site->site_fs_path . '/config/php/php/conf.d/docker-php-ext-timezonedb.ini', |
| 139 | + $site->site_fs_path . '/config/php/php/conf.d/docker-php-ext-apcu.ini', |
| 140 | + $site->site_fs_path . '/config/php/php/conf.d/docker-php-ext-mcrypt.ini', |
| 141 | + $site->site_fs_path . '/config/php/php/conf.d/docker-php-ext-redis.ini', |
| 142 | + ]; |
| 143 | + |
| 144 | + foreach ( $configs as $config ) { |
| 145 | + |
| 146 | + if ( $this->fs->exists( $config ) ) { |
| 147 | + $this->fs->remove( $config ); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments