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