|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WP_Forge\UpgradeHandler; |
| 4 | + |
| 5 | +/** |
| 6 | + * Class UpgradeHandler |
| 7 | + * |
| 8 | + * @package WP_Forge\UpgradeHandler |
| 9 | + */ |
| 10 | +class UpgradeHandler { |
| 11 | + |
| 12 | + /** |
| 13 | + * The previous version. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $old_version; |
| 18 | + |
| 19 | + /** |
| 20 | + * The current version. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $new_version; |
| 25 | + |
| 26 | + /** |
| 27 | + * The directory containing upgrade routines. |
| 28 | + * |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + protected $upgrade_dir; |
| 32 | + |
| 33 | + /** |
| 34 | + * Class constructor. |
| 35 | + * |
| 36 | + * @param string $upgrade_dir The directory containing upgrade routines. |
| 37 | + * @param string $old_version The previous version. |
| 38 | + * @param string $new_version The current version. |
| 39 | + */ |
| 40 | + public function __construct( $upgrade_dir, $old_version, $new_version ) { |
| 41 | + $this->upgrade_dir = $upgrade_dir; |
| 42 | + $this->old_version = $old_version; |
| 43 | + $this->new_version = $new_version; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Detect when an upgrade is necessary and run the updates. |
| 48 | + * |
| 49 | + * @return bool Whether or not an upgrade routine ran. |
| 50 | + */ |
| 51 | + public function maybe_upgrade() { |
| 52 | + $should_upgrade = $this->should_upgrade(); |
| 53 | + if ( $should_upgrade ) { |
| 54 | + $available_routines = $this->get_available_upgrade_routines(); |
| 55 | + $required_routines = $this->get_required_upgrade_routines( $available_routines ); |
| 56 | + $this->run_upgrade_routines( $required_routines ); |
| 57 | + } |
| 58 | + |
| 59 | + return $should_upgrade; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Check if we should upgrade. |
| 64 | + * |
| 65 | + * @return bool |
| 66 | + */ |
| 67 | + public function should_upgrade() { |
| 68 | + return $this->old_version !== $this->new_version; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Get a collection of available upgrade routines. |
| 73 | + * |
| 74 | + * @return array A collection of filepaths indexed by versions. |
| 75 | + */ |
| 76 | + public function get_available_upgrade_routines() { |
| 77 | + $routines = array(); |
| 78 | + $filepaths = glob( rtrim( $this->upgrade_dir, '/' ) . '/*.php' ); |
| 79 | + if ( $filepaths ) { |
| 80 | + $versions = str_replace( '.php', '', array_map( 'basename', $filepaths ) ); |
| 81 | + $routines = array_combine( $versions, $filepaths ); |
| 82 | + } |
| 83 | + |
| 84 | + return $routines; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get a collection of the required upgrade routines. |
| 89 | + * |
| 90 | + * @param array $available_routines A collection of available upgrade routines. |
| 91 | + * |
| 92 | + * @return array A collection of filepaths indexed by versions. |
| 93 | + */ |
| 94 | + public function get_required_upgrade_routines( array $available_routines ) { |
| 95 | + $routines = array(); |
| 96 | + $required = array_filter( array_keys( $available_routines ), array( $this, 'filter_upgrade_routines' ) ); |
| 97 | + if ( $required ) { |
| 98 | + $routines = array_intersect_key( $available_routines, array_combine( $required, $required ) ); |
| 99 | + } |
| 100 | + |
| 101 | + return $routines; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Run an ordered set of upgrade routines. |
| 106 | + * |
| 107 | + * @param array $routines A collection of filepaths indexed by versions. |
| 108 | + */ |
| 109 | + public function run_upgrade_routines( array $routines ) { |
| 110 | + foreach ( $routines as $file ) { |
| 111 | + if ( file_exists( $file ) ) { |
| 112 | + require $file; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Filter to find the versions for which we need to run an upgrade routine. |
| 119 | + * |
| 120 | + * @param string $version The current version. |
| 121 | + * |
| 122 | + * @return bool Whether or not to keep the routine. |
| 123 | + */ |
| 124 | + protected function filter_upgrade_routines( $version ) { |
| 125 | + return version_compare( $this->old_version, $version, '<' ) && version_compare( $this->new_version, $version, '>=' ); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments