<commit_before><?php

namespace EE\Migration;

use EE;
use EE\Model\Migration;
use EE\Utils;
use Symfony\Component\Finder\Finder;

class Executor {

	/**
	 * Executes all pending migrations
	 */
	public static function execute_migrations() {

		Utils\delem_log( 'ee migration start' );
		EE::debug( 'Executing migrations' );

		$migrations = self::get_all_migrations();

		if ( empty( $migrations ) ) {
			EE::debug( 'Nothing to migrate' );
			return;
		}

		sort( $migrations );

		try {
			self::execute_migration_stack( $migrations );
		} catch ( \Throwable $e ) {
			Utils\delem_log( 'ee migration ended abruptly' );
			exit( 1 );
		}

		EE::debug( 'Successfully migrated EasyEngine' );
	}

	/**
	 * @return array of available migrations
	 */
	private static function get_all_migrations() {

		$packages_path = scandir( EE_VENDOR_DIR . '/easyengine' );

		// get migrations from packages.
		if ( ! empty( $packages_path ) ) {
			foreach ( $packages_path as $package ) {
				if ( '.' === $package || '..' === $package || is_file( $package ) ) {
					continue;
				}

				$migration_path = EE_VENDOR_DIR . '/easyengine/' . $package . '/migrations';
				if ( is_dir( $migration_path ) ) {
					$files = scandir( $migration_path );
					if ( \EE\Utils\inside_phar() ) {
						$migrations[] = $files;
					} else{
						$migrations[] = array_slice( $files,2);
					}
				}
			}
		}

		// get migrations from core.
		if ( file_exists( EE_ROOT . '/migrations' ) ) {
			$files = scandir( EE_ROOT . '/migrations' );
			if ( \EE\Utils\inside_phar() ) {
				$migrations[] = $files;
			} else{
				$migrations[] = array_slice( $files,2);
			}
		}

		$migrations = array_merge( ...$migrations );

		return self::get_migrations_to_execute( $migrations );
	}

	/**
	 * Executes all migrations passed to it recursively.
	 * Also undo'es all migration if there was error executing any migration
	 */
	private static function execute_migration_stack( $migrations ) {
		if ( empty( $migrations ) ) {
			return;
		}

		$migration_path       = self::get_migration_path( $migrations[0] );
		$migration_class_name = self::get_migration_class_name( $migrations[0] );

		if ( ! file_exists( $migration_path ) ) {
			EE::error( "Unable to find migration file at $migration_path", false );
			throw new Exception();<commit_msg>Declare migration array before use<commit_after><?php

namespace EE\Migration;

use EE;
use EE\Model\Migration;
use EE\Utils;
use Symfony\Component\Finder\Finder;

class Executor {

	/**
	 * Executes all pending migrations
	 */
	public static function execute_migrations() {

		Utils\delem_log( 'ee migration start' );
		EE::debug( 'Executing migrations' );

		$migrations = self::get_all_migrations();

		if ( empty( $migrations ) ) {
			EE::debug( 'Nothing to migrate' );
			return;
		}

		sort( $migrations );

		try {
			self::execute_migration_stack( $migrations );
		} catch ( \Throwable $e ) {
			Utils\delem_log( 'ee migration ended abruptly' );
			exit( 1 );
		}

		EE::debug( 'Successfully migrated EasyEngine' );
	}

	/**
	 * @return array of available migrations
	 */
	private static function get_all_migrations() {
		$migrations = [];
		$packages_path = scandir( EE_VENDOR_DIR . '/easyengine' );

		// get migrations from packages.
		if ( ! empty( $packages_path ) ) {
			foreach ( $packages_path as $package ) {
				if ( '.' === $package || '..' === $package || is_file( $package ) ) {
					continue;
				}

				$migration_path = EE_VENDOR_DIR . '/easyengine/' . $package . '/migrations';
				if ( is_dir( $migration_path ) ) {
					$files = scandir( $migration_path );
					if ( \EE\Utils\inside_phar() ) {
						$migrations[] = $files;
					} else{
						$migrations[] = array_slice( $files,2);
					}
				}
			}
		}

		// get migrations from core.
		if ( is_dir( EE_ROOT . '/migrations' ) ) {
			$files = scandir( EE_ROOT . '/migrations' );
			if ( \EE\Utils\inside_phar() ) {
				$migrations[] = $files;
			} else{
				$migrations[] = array_slice( $files,2);
			}
		}

		if ( ! empty( $migrations ) ) {
			$migrations = array_merge( ...$migrations );
		}

		return self::get_migrations_to_execute( $migrations );
	}

	/**
	 * Executes all migrations passed to it recursively.
	 * Also undo'es all migration if there was error executing any migration
	 */
	private static function execute_migration_stack( $migrations ) {
		if ( empty( $migrations ) ) {
			return;
		}

		$migration_path       = self::get_migration_path( $migrations[0] );
		$migration_class_name = self::get_migration_class_name( $migrations[0] );

		if ( ! file_exists( $migration_path ) ) {
			EE::error( "Unable to find migration file at $migration_path", false );
			throw new Exception();
{'fraction_non_alphanumeric': 0.14673216574113626, 'fraction_numerical': 0.0021358393848782574, 'mean_word_length': 3.215121512151215, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '944387', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1624, 'n_words': 367}