/// <summary> /// Initializes the script context for a web request. /// </summary> /// <param name="appContext">Application context.</param> /// <param name="context">HTTP context of the request.</param> /// <returns>A instance of <see cref="ScriptContext"/> to be used by the request.</returns> /// <exception cref="System.Configuration.ConfigurationErrorsException"> /// Web configuration is invalid. The context is not initialized then. /// </exception> internal static ScriptContext /*!*/ InitWebRequest(ApplicationContext /*!*/ appContext, HttpContext /*!*/ context) { Debug.Assert(appContext != null && context != null); // reloads configuration of the current thread from ASP.NET caches or web.config files; // cached configuration is reused; Configuration.Reload(appContext, false); // takes a writable copy of a global configuration (may throw ConfigurationErrorsException): LocalConfiguration config = (LocalConfiguration)Configuration.DefaultLocal.DeepCopy(); // following initialization statements shouldn't throw an exception: // can throw on Integrated Pipeline, events must be attached within HttpApplication.Init() ScriptContext result = new ScriptContext(appContext, config, context.Response.Output, context.Response.OutputStream); result.IsOutputBuffered = config.OutputControl.OutputBuffering; result.ThrowExceptionOnError = true; result.WorkingDirectory = Path.GetDirectoryName(context.Request.PhysicalPath); if (config.OutputControl.ContentType != null) { context.Response.ContentType = config.OutputControl.ContentType; } if (config.OutputControl.CharSet != null) { context.Response.Charset = config.OutputControl.CharSet; } result.AutoGlobals.Initialize(config, context); ScriptContext.CurrentContext = result; Externals.BeginRequest(); return(result); }
/// <summary> /// Initializes the script context for a PHP console application. /// </summary> /// <param name="appContext">Application context.</param> /// <param name="mainScript">The main script's type or a <B>null</B> reference for a pure application.</param> /// <param name="relativeSourcePath">A path to the main script source file.</param> /// <param name="sourceRoot">A source root within which an application has been compiler.</param> /// <returns> /// A new instance of <see cref="ScriptContext"/> with its own copy of local configuration /// to be used by the application. /// </returns> /// <exception cref="System.Configuration.ConfigurationErrorsException"> /// Web configuration is invalid. The context is not initialized then. /// </exception> /// <remarks> /// Use this method if you want to initialize application in the same way the PHP console/Windows /// application is initialized. The returned script context is initialized as follows: /// <list type="bullet"> /// <term>The application's source root is set.</term> /// <term>The main script of the application is defined.</term> /// <term>Output and input streams are set to standard output and input, respectively.</term> /// <term>Current culture it set to <see cref="CultureInfo.InvariantCulture"/>.</term> /// <term>Auto-global variables ($_GET, $_SET, etc.) are initialized.</term> /// <term>Working directory is set tothe current working directory.</term> /// </list> /// </remarks> 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(); return(result); }