public static ScriptContext/*!*/ InitApplication(ApplicationContext/*!*/ appContext, Type mainScript,
string relativeSourcePath, string sourceRoot)
{
// loads configuration into the given application context
// (applies only if the config has not been loaded yet by the current thread):
Configuration.Load(appContext);
ApplicationConfiguration app_config = Configuration.Application;
if (mainScript != null)
{
if (relativeSourcePath == null)
throw new ArgumentNullException("relativeSourcePath");
if (sourceRoot == null)
throw new ArgumentNullException("sourceRoot");
// overrides source root configuration if not explicitly specified in config file:
if (!app_config.Compiler.SourceRootSet)
app_config.Compiler.SourceRoot = new FullPath(sourceRoot);
}
// takes a writable copy of a global configuration:
LocalConfiguration config = (LocalConfiguration)Configuration.DefaultLocal.DeepCopy();
// sets invariant culture as a default one:
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
ScriptContext result = new ScriptContext(appContext, config, Console.Out, Console.OpenStandardOutput());
result.IsOutputBuffered = result.config.OutputControl.OutputBuffering;
result.AutoGlobals.Initialize(config, null);
result.WorkingDirectory = Directory.GetCurrentDirectory();
result.ThrowExceptionOnError = true;
result.config.ErrorControl.HtmlMessages = false;
if (mainScript != null)
{
// converts relative path of the script source to full canonical path using source root from the configuration:
PhpSourceFile main_source_file = new PhpSourceFile(
app_config.Compiler.SourceRoot,
new FullPath(relativeSourcePath, app_config.Compiler.SourceRoot)
);
result.DefineMainScript(new ScriptInfo(mainScript), main_source_file);
}
ScriptContext.CurrentContext = result;
Externals.BeginRequest();
result.FinallyDispose += Externals.EndRequest;
//
return result;
}