profilers
2 years ago
versions
2 years ago
migration-exception.php
2 years ago
migration-model.php
2 years ago
migrator.php
2 years ago
view-migrations.php
2 years ago
migrator.php
129 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Migrations; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Instance_Trait; |
| 7 | use Jet_Form_Builder\Migrations\Versions\Version_3_1_7; |
| 8 | use JFB_Components\Repository\Repository_Pattern_Trait; |
| 9 | use Jet_Form_Builder\Db_Queries\Execution_Builder; |
| 10 | use Jet_Form_Builder\Exceptions\Query_Builder_Exception; |
| 11 | use Jet_Form_Builder\Migrations\Profilers\Base_Migration_Profiler; |
| 12 | use Jet_Form_Builder\Migrations\Versions\Base_Migration; |
| 13 | use Jet_Form_Builder\Migrations\Versions\Version_2_1_0; |
| 14 | use Jet_Form_Builder\Migrations\Versions\Version_2_1_7; |
| 15 | use Jet_Form_Builder\Migrations\Versions\Version_2_1_8; |
| 16 | |
| 17 | // If this file is called directly, abort. |
| 18 | if ( ! defined( 'WPINC' ) ) { |
| 19 | die; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * @method static Migrator instance() |
| 24 | * |
| 25 | * Class Migrator |
| 26 | * @package Jet_Form_Builder\Migrations |
| 27 | */ |
| 28 | class Migrator { |
| 29 | |
| 30 | use Instance_Trait; |
| 31 | use Repository_Pattern_Trait; |
| 32 | |
| 33 | private $installed_migrations = array(); |
| 34 | |
| 35 | public function __construct() { |
| 36 | $this->rep_install(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @return array |
| 41 | */ |
| 42 | public function rep_instances(): array { |
| 43 | return array( |
| 44 | new Version_2_1_0(), |
| 45 | new Version_2_1_7(), |
| 46 | new Version_2_1_8(), |
| 47 | new Version_3_1_7(), |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param null|Base_Migration_Profiler $profiler |
| 53 | * |
| 54 | * @throws Migration_Exception |
| 55 | */ |
| 56 | public function install( $profiler = null ) { |
| 57 | /** @var Base_Migration $migration */ |
| 58 | foreach ( $this->rep_get_items() as $migration ) { |
| 59 | $migration->set_profiler( $profiler )->install(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param null|Base_Migration_Profiler $profiler |
| 65 | * |
| 66 | * @throws Migration_Exception |
| 67 | */ |
| 68 | public function uninstall( $profiler = null ) { |
| 69 | /** @var Base_Migration $migration */ |
| 70 | foreach ( $this->rep_get_items() as $migration ) { |
| 71 | $migration->set_profiler( $profiler )->uninstall(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public function is_installed_all(): bool { |
| 76 | $items = $this->rep_get_items(); |
| 77 | |
| 78 | /** @var Base_Migration $item */ |
| 79 | foreach ( $items as $item ) { |
| 80 | if ( ! $item->is_installed() ) { |
| 81 | return false; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | public function is_installed( Base_Migration $migration ): bool { |
| 89 | $version = get_class( $migration ); |
| 90 | |
| 91 | if ( isset( $this->installed_migrations[ $version ] ) ) { |
| 92 | return $this->installed_migrations[ $version ]; |
| 93 | } |
| 94 | |
| 95 | $installed = true; |
| 96 | |
| 97 | try { |
| 98 | View_Migrations::findOne( |
| 99 | array( |
| 100 | 'version' => $version, |
| 101 | ) |
| 102 | )->query()->query_one(); |
| 103 | } catch ( Query_Builder_Exception $exception ) { |
| 104 | $exception->unset_log(); |
| 105 | $installed = false; |
| 106 | } |
| 107 | |
| 108 | $this->installed_migrations[ $version ] = $installed; |
| 109 | |
| 110 | return $this->installed_migrations[ $version ]; |
| 111 | } |
| 112 | |
| 113 | public function set_installed( Base_Migration $migration ): Migrator { |
| 114 | $version = get_class( $migration ); |
| 115 | |
| 116 | $this->installed_migrations[ $version ] = true; |
| 117 | |
| 118 | return $this; |
| 119 | } |
| 120 | |
| 121 | public function set_uninstalled( Base_Migration $migration ): Migrator { |
| 122 | $version = get_class( $migration ); |
| 123 | |
| 124 | $this->installed_migrations[ $version ] = false; |
| 125 | |
| 126 | return $this; |
| 127 | } |
| 128 | } |
| 129 |