profilers
3 years ago
rest-api
3 years ago
versions
3 years ago
migration-exception.php
3 years ago
migration-model.php
3 years ago
migrator.php
3 years ago
view-migrations.php
3 years ago
migrator.php
118 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\Classes\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 | |
| 14 | /** |
| 15 | * @method static Migrator instance() |
| 16 | * |
| 17 | * Class Migrator |
| 18 | * @package Jet_Form_Builder\Migrations |
| 19 | */ |
| 20 | class Migrator { |
| 21 | |
| 22 | use Instance_Trait; |
| 23 | use Repository_Pattern_Trait; |
| 24 | |
| 25 | private $installed_migrations = array(); |
| 26 | |
| 27 | public function __construct() { |
| 28 | $this->rep_install(); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @return array |
| 33 | */ |
| 34 | public function rep_instances(): array { |
| 35 | return array( |
| 36 | new Version_2_1_0(), |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param null|Base_Migration_Profiler $profiler |
| 42 | * |
| 43 | * @throws Migration_Exception |
| 44 | */ |
| 45 | public function install( $profiler = null ) { |
| 46 | /** @var Base_Migration $migration */ |
| 47 | foreach ( $this->rep_get_items() as $migration ) { |
| 48 | $migration->set_profiler( $profiler )->install(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param null|Base_Migration_Profiler $profiler |
| 54 | * |
| 55 | * @throws Migration_Exception |
| 56 | */ |
| 57 | public function uninstall( $profiler = null ) { |
| 58 | /** @var Base_Migration $migration */ |
| 59 | foreach ( $this->rep_get_items() as $migration ) { |
| 60 | $migration->set_profiler( $profiler )->uninstall(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public function is_installed_all(): bool { |
| 65 | $items = $this->rep_get_items(); |
| 66 | |
| 67 | /** @var Base_Migration $item */ |
| 68 | foreach ( $items as $item ) { |
| 69 | if ( ! $item->is_installed() ) { |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | public function is_installed( Base_Migration $migration ): bool { |
| 78 | $version = get_class( $migration ); |
| 79 | |
| 80 | if ( isset( $this->installed_migrations[ $version ] ) ) { |
| 81 | return $this->installed_migrations[ $version ]; |
| 82 | } |
| 83 | |
| 84 | $installed = true; |
| 85 | |
| 86 | try { |
| 87 | View_Migrations::findOne( |
| 88 | array( |
| 89 | 'version' => $version, |
| 90 | ) |
| 91 | )->query()->query_one(); |
| 92 | } catch ( Query_Builder_Exception $exception ) { |
| 93 | $exception->unset_from_logger(); |
| 94 | $installed = false; |
| 95 | } |
| 96 | |
| 97 | $this->installed_migrations[ $version ] = $installed; |
| 98 | |
| 99 | return $this->installed_migrations[ $version ]; |
| 100 | } |
| 101 | |
| 102 | public function set_installed( Base_Migration $migration ): Migrator { |
| 103 | $version = get_class( $migration ); |
| 104 | |
| 105 | $this->installed_migrations[ $version ] = true; |
| 106 | |
| 107 | return $this; |
| 108 | } |
| 109 | |
| 110 | public function set_uninstalled( Base_Migration $migration ): Migrator { |
| 111 | $version = get_class( $migration ); |
| 112 | |
| 113 | $this->installed_migrations[ $version ] = false; |
| 114 | |
| 115 | return $this; |
| 116 | } |
| 117 | } |
| 118 |