<commit_before><?php

namespace EE\Migration;

use EE;
use Symfony\Component\Filesystem\Filesystem;

/**
 * Migrate site specific containers to new images.
 */
class SiteContainers {

	/**
	 * Take backup of site's docker-compose.yml file
	 *
	 * @param $source_path string path of docker-compose.yml
	 * @param $dest_path   string backup path for docker-compose.yml
	 *
	 * @throws \Exception
	 */
	public static function backup_site_docker_compose_file( $source_path, $dest_path ) {
		EE::debug( 'Start backing up site\'s docker-compose.yml' );
		$fs = new Filesystem();
		if ( ! $fs->exists( $source_path ) ) {
			throw new \Exception( ' site\'s docker-compose.yml does not exist' );
		}
		$fs->copy( $source_path, $dest_path, true );
		EE::debug( 'Complete backing up site\'s docker-compose.yml' );
	}

	/**
	 * Revert docker-compose.yml file from backup.
	 *
	 * @param $source_path string path of backed up docker-compose.yml
	 * @param $dest_path   string original path of docker-compose.yml
	 *
	 * @throws \Exception
	 */
	public static function revert_site_docker_compose_file( $source_path, $dest_path ) {
		EE::debug( 'Start restoring site\'s docker-compose.yml' );
		$fs = new Filesystem();
		if ( ! $fs->exists( $source_path ) ) {
			throw new \Exception( ' site\'s docker-compose.yml.backup does not exist' );
		}
		$fs->copy( $source_path, $dest_path, true );
		$fs->remove( $source_path );
		EE::debug( 'Complete restoring site\'s docker-compose.yml' );
	}

	/**
	 * Check if new image is available for site's services.
	 *
	 * @param $updated_images array of updated images.
	 * @param $site_info      array of site info
	 *
	 * @return bool
	 */
	public static function is_site_service_image_changed( $updated_images, $site_info ) {
		chdir( $site_info['site_fs_path'] );
		$launch   = EE::launch( 'docker-compose config --services' );
		$services = explode( PHP_EOL, trim( $launch->stdout ) );

		$site_images = array_map( function ( $service ) {
			return 'easyengine/' . $service;
		}, $services );

		$common_image = array_intersect( $updated_images, $site_images );

		if ( ! empty( $common_image ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Generate docker-compose.yml for specific site.
	 *
	 * @param $site_info array of site information.
	 */
	public static function generate_site_docker_compose_file( $site_info ) {
		EE::debug( "Start generating news docker-compose.yml for ${site_info['site_url']}" );
		$site_docker        = self::get_site_docker_object( $site_info['site_type'] );
		$filters            = self::get_site_filters( $site_info );
		$docker_yml_content = $site_docker->generate_docker_compose_yml( $filters );

		$fs = new Filesystem();
		$fs->dumpFile( $site_info['site_fs_path'] . '/docker-compose.yml', $docker_yml_content );
		EE::debug( "Complete generating news docker-compose.yml for ${site_info['site_url']}" );
	}

	/**
	 * Enable site.
	 *
	 * @param $site_info   array of site information.
	 * @param $site_object object of site-type( HTML, PHP, Wordpress ).
	 */
	public static function enable_site( $site_info, $site_object ) {
		EE::debug( "Start enabling ${site_info['site_url']}" );
		$site_object->enable( [ $site_info['site_url'] ], [] );
		EE::debug( "Complete enabling ${site_info['site_url']}" );
	}

	/**
	 * Disable site.
	 *
	 * @param $site_info   array of site information.
	 * @param $site_object object of site-type( HTML, PHP, Wordpress ).<commit_msg>Fix generation of docker-compose yml<commit_after><?php

namespace EE\Migration;

use EE;
use Symfony\Component\Filesystem\Filesystem;

/**
 * Migrate site specific containers to new images.
 */
class SiteContainers {

	/**
	 * Take backup of site's docker-compose.yml file
	 *
	 * @param $source_path string path of docker-compose.yml
	 * @param $dest_path   string backup path for docker-compose.yml
	 *
	 * @throws \Exception
	 */
	public static function backup_site_docker_compose_file( $source_path, $dest_path ) {
		EE::debug( 'Start backing up site\'s docker-compose.yml' );
		$fs = new Filesystem();
		if ( ! $fs->exists( $source_path ) ) {
			throw new \Exception( ' site\'s docker-compose.yml does not exist' );
		}
		$fs->copy( $source_path, $dest_path, true );
		EE::debug( 'Complete backing up site\'s docker-compose.yml' );
	}

	/**
	 * Revert docker-compose.yml file from backup.
	 *
	 * @param $source_path string path of backed up docker-compose.yml
	 * @param $dest_path   string original path of docker-compose.yml
	 *
	 * @throws \Exception
	 */
	public static function revert_site_docker_compose_file( $source_path, $dest_path ) {
		EE::debug( 'Start restoring site\'s docker-compose.yml' );
		$fs = new Filesystem();
		if ( ! $fs->exists( $source_path ) ) {
			throw new \Exception( ' site\'s docker-compose.yml.backup does not exist' );
		}
		$fs->copy( $source_path, $dest_path, true );
		$fs->remove( $source_path );
		EE::debug( 'Complete restoring site\'s docker-compose.yml' );
	}

	/**
	 * Check if new image is available for site's services.
	 *
	 * @param $updated_images array of updated images.
	 * @param $site_info      array of site info
	 *
	 * @return bool
	 */
	public static function is_site_service_image_changed( $updated_images, $site_info ) {
		chdir( $site_info['site_fs_path'] );
		$launch   = EE::launch( 'docker-compose config --services' );
		$services = explode( PHP_EOL, trim( $launch->stdout ) );

		$site_images = array_map( function ( $service ) {
			return 'easyengine/' . $service;
		}, $services );

		$common_image = array_intersect( $updated_images, $site_images );

		if ( ! empty( $common_image ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Generate docker-compose.yml for specific site.
	 *
	 * @param array $site_info of site information.
	 * @param object $site_obj Object of the particular site-type.
	 */
	public static function generate_site_docker_compose_file( $site_info, $site_obj ) {
		$site_obj->populate_site_info( $site_info['site_url'] );
		EE::debug( "Start generating news docker-compose.yml for ${site_info['site_url']}" );
		$site_obj->dump_docker_compose_yml( [ 'nohttps' => $site_info['site_ssl'] ] );
		EE::debug( "Complete generating news docker-compose.yml for ${site_info['site_url']}" );
	}

	/**
	 * Enable site.
	 *
	 * @param $site_info   array of site information.
	 * @param $site_object object of site-type( HTML, PHP, Wordpress ).
	 */
	public static function enable_site( $site_info, $site_object ) {
		EE::debug( "Start enabling ${site_info['site_url']}" );
		$site_object->enable( [ $site_info['site_url'] ], [] );
		EE::debug( "Complete enabling ${site_info['site_url']}" );
	}

	/**
	 * Disable site.
	 *
	 * @param $site_info   array of site information.
	 * @param $site_object object of site-type( HTML, PHP, Wordpress ).
{'fraction_non_alphanumeric': 0.14344807209336682, 'fraction_numerical': 0.0, 'mean_word_length': 3.870503597122302, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, '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': '5755879', 'n_tokens_mistral': 2476, 'n_tokens_neox': 2206, 'n_words': 572}