<?php

/* use something like this to manually test the migrations
require_once INTENSE_PLUGIN_FOLDER . '/inc/migrations/migrations.php';

$migration = new Intense_Migrations();

$migration->migrate('2.5.0', '2.5.1');
 */

class Intense_Migration {
	public function up() {

	}

	public function down() {

	}
}

class Intense_Migrations {
	public function migrate( $from, $to ) {
		$migrations = scandir( INTENSE_PLUGIN_FOLDER . '/inc/migrations/' );

        foreach ($migrations as $key => $migration) {
            if ($migration != '.' && $migration != '..' && substr( $migration, -strlen( ".php" )) == ".php" ) {            
                require_once( INTENSE_PLUGIN_FOLDER . '/inc/migrations/' . $migration );
            }
        }

       	$migrate_up = version_compare( $from, $to, '<' );

        foreach ( get_declared_classes() as $class ) {
            if ( is_subclass_of( $class, 'Intense_Migration' ) ) {                
                //the version is found by getting the value after Intense_Migration and replacing '_' with '.'
                $version = str_replace( '_', '.', str_replace( 'Intense_Migration_', '', $class ) );

                if ( $migrate_up && version_compare( $version, $from, '>' ) && version_compare( $version, $to, '<=' ) ) {
                	$migration = new $class();	

                	$migration->up();
                } else if ( !$migrate_up && version_compare( $version, $from, '<=' ) && version_compare( $version, $to, '>' )) {
					$migration = new $class();	

                	$migration->down();
                }
            }
        }
	}
}
