Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <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;
        }