Update
5 years ago
Updater
5 years ago
Installer.php
5 years ago
Update.php
5 years ago
Updater.php
5 years ago
Updater.php
59 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Plugin; |
| 4 | |
| 5 | abstract class Updater { |
| 6 | |
| 7 | /** |
| 8 | * @var Update[] |
| 9 | */ |
| 10 | protected $updates; |
| 11 | |
| 12 | /** |
| 13 | * @param Update $update |
| 14 | * |
| 15 | * @return $this |
| 16 | */ |
| 17 | public function add_update( Update $update ) { |
| 18 | $this->updates[ $update->get_version() ] = $update; |
| 19 | |
| 20 | return $this; |
| 21 | } |
| 22 | |
| 23 | public function parse_updates() { |
| 24 | if ( $this->is_new_install() ) { |
| 25 | $this->update_stored_version(); |
| 26 | |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | if ( empty( $this->updates ) ) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | // Sort by version number |
| 35 | uksort( $this->updates, 'version_compare' ); |
| 36 | |
| 37 | foreach ( $this->updates as $update ) { |
| 38 | if ( $update->needs_update() ) { |
| 39 | $update->apply_update(); |
| 40 | $this->update_stored_version( $update->get_version() ); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | $this->update_stored_version(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param null $version |
| 49 | * |
| 50 | * @return bool |
| 51 | */ |
| 52 | abstract protected function update_stored_version( $version = null ); |
| 53 | |
| 54 | /** |
| 55 | * @return bool |
| 56 | */ |
| 57 | abstract protected function is_new_install(); |
| 58 | |
| 59 | } |