protected function setupDist()

in pgo/cases/pgo01org/TrainingCaseHandler.php [48:114]


	protected function setupDist() : void
	{
		$cmd_path_arg = "--path=" . $this->base;

		if (!is_dir($this->base)) {
			echo "Setting up " . $this->getName() . " in '{$this->base}'\n";
			/* XXX Use host PHP for this. */
			$php = new PHP\CLI($this->conf);
			$php->exec($this->getToolFn() . " core download --force $cmd_path_arg");
			unset($php);
		}
		
		$http_port = $this->getHttpPort();
		$http_host = $this->getHttpHost();
		$db_port = $this->getDbPort();
		$db_host = $this->getDbHost();
		$db_user = $this->getDbUser();
		$db_pass = $this->getDbPass();

		$vars = array(
			$this->conf->buildTplVarName($this->getName(), "docroot") => str_replace("\\", "/", $this->base),
		);
		$tpl_fn = $this->conf->getCasesTplDir($this->getName()) . DIRECTORY_SEPARATOR . "nginx.partial.conf";
		$this->nginx->addServer($tpl_fn, $vars);


		$php = new PHP\CLI($this->conf);

		$this->maria->up();
		$this->nginx->up();

//		$this->maria->query("DROP DATABASE IF EXISTS " . $this->getName());
//		$this->maria->query("CREATE DATABASE " . $this->getName());

		$htdocs = $this->conf->getHtdocs($this->getName());
		$fl = $htdocs . DIRECTORY_SEPARATOR . "constants.php";

		$constants = file_get_contents($fl);
		
		$constants = preg_replace(",define\('DB_USER'.+,", "define('DB_USER', '$db_user');", $constants);
		$constants = preg_replace(",define\('DB_PASSWORD'.+,", "define('DB_PASSWORD', '$db_pass');", $constants);
		$constants = preg_replace(",define\('DB_NAME'.+,", "define('DB_NAME', '" . $this->getName() . "');", $constants);
		$constants = preg_replace(",define\('DB_HOST'.+,", "define('DB_HOST', '$db_host:$db_port');", $constants);
		file_put_contents($fl, $constants);

		// work around <https://github.com/Microsoft/php-sdk-binary-tools/issues/51>
		$fl = $htdocs . DIRECTORY_SEPARATOR . "class.php";
		$class = file_get_contents($fl);
		$class = preg_replace(",function Student,", "function __construct", $class);
		$class = preg_replace(",function Faculty,", "function __construct", $class);
		file_put_contents($fl, $class);

		// patch <https://github.com/intel/php_pgo_training_scripts/pull/4>
		$fl = $htdocs . DIRECTORY_SEPARATOR . "standard_calls.php";
		$standard_calls = file_get_contents($fl);
		$standard_calls = preg_replace(",parse_str\(\\\$var1\),", "parse_str(\$var1, \$dummy)", $standard_calls);
		file_put_contents($fl, $standard_calls);

		//$php->exec($cmd, NULL, $env);
		/* TODO check status or switch to cli. */
		$out = file_get_contents("http://$http_host:$http_port/init.php");
		echo $out, PHP_EOL;

		$this->nginx->down(true);
		$this->maria->down(true);

	}