Example #1
0
        /// <summary>
        /// Resolves path according to PHP semantics, lookups the file in runtime tables and calls its Main method.
        /// </summary>
        /// <param name="cd">Current script directory. Used for relative path resolution. Can be <c>null</c> to not resolve against current directory.</param>
        /// <param name="path">The relative or absolute path to resolve and include.</param>
        /// <param name="locals">Variables scope for the included script.</param>
        /// <param name="this">Reference to <c>this</c> variable.</param>
        /// <param name="once">Whether to include according to include once semantics.</param>
        /// <param name="throwOnError">Whether to include according to require semantics.</param>
        /// <returns>Inclusion result value.</returns>
        public PhpValue Include(string cd, string path, PhpArray locals, object @this = null, bool once = false, bool throwOnError = false)
        {
            var script = ScriptsMap.SearchForIncludedFile(path, IncludePaths, cd, _scripts.GetScript);  // TODO: _scripts.GetScript => make relative path from absolute

            if (script.IsValid)
            {
                if (once && _scripts.IsIncluded(script.Index))
                {
                    return(PhpValue.Create(true));
                }
                else
                {
                    return(script.MainMethod(this, locals, @this));
                }
            }
            else
            {
                if (throwOnError)
                {
                    throw new ArgumentException($"File '{path}' cannot be included with current configuration.");   // TODO: ErrCode
                }
                else
                {
                    return(PhpValue.Create(false));   // TODO: Warning
                }
            }
        }
Example #2
0
        /// <summary>
        /// Resolves path according to PHP semantics, lookups the file in runtime tables and calls its Main method.
        /// </summary>
        /// <param name="cd">Current script directory. Used for relative path resolution. Can be <c>null</c> to not resolve against current directory.</param>
        /// <param name="path">The relative or absolute path to resolve and include.</param>
        /// <param name="locals">Variables scope for the included script.</param>
        /// <param name="this">Reference to <c>this</c> variable.</param>
        /// <param name="once">Whether to include according to include once semantics.</param>
        /// <param name="throwOnError">Whether to include according to require semantics.</param>
        /// <returns>Inclusion result value.</returns>
        public PhpValue Include(string cd, string path, PhpArray locals, object @this = null, bool once = false, bool throwOnError = false)
        {
            ScriptInfo script;

            path = ScriptsMap.NormalizeSlashes(path);

            if (path.StartsWith(this.RootPath, StringComparison.Ordinal)) // rooted
            {
                script = _scripts.GetScript(path.Substring(this.RootPath.Length + 1));
            }
            else
            {
                script = ScriptsMap.SearchForIncludedFile(path, IncludePaths, cd, _scripts.GetScript);
            }

            if (script.IsValid)
            {
                if (once && _scripts.IsIncluded(script.Index))
                {
                    return(PhpValue.Create(true));
                }
                else
                {
                    return(script.MainMethod(this, locals, @this));
                }
            }
            else
            {
                if (TryIncludeFileContent(path))    // include non-compiled file (we do not allow dynamic compilation)
                {
                    return(PhpValue.Null);
                }
                else
                {
                    var cause = string.Format(Resources.ErrResources.script_not_found, path);

                    PhpException.Throw(
                        throwOnError ? PhpError.Error : PhpError.Notice,
                        Resources.ErrResources.script_inclusion_failed, path, cause, string.Join(";", IncludePaths), cd);

                    if (throwOnError)
                    {
                        throw new ArgumentException(cause);
                    }

                    return(PhpValue.False);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Resolves path according to PHP semantics, lookups the file in runtime tables and calls its Main method.
        /// </summary>
        /// <param name="cd">Current script directory. Used for relative path resolution. Can be <c>null</c> to not resolve against current directory.</param>
        /// <param name="path">The relative or absolute path to resolve and include.</param>
        /// <param name="locals">Variables scope for the included script.</param>
        /// <param name="this">Reference to <c>this</c> variable.</param>
        /// <param name="once">Whether to include according to include once semantics.</param>
        /// <param name="throwOnError">Whether to include according to require semantics.</param>
        /// <returns>Inclusion result value.</returns>
        public PhpValue Include(string cd, string path, PhpArray locals, object @this = null, bool once = false, bool throwOnError = false)
        {
            ScriptInfo script;

            path = path.Replace('/', '\\');                               // normalize slashes
            if (path.StartsWith(this.RootPath, StringComparison.Ordinal)) // rooted
            {
                script = _scripts.GetScript(path.Substring(this.RootPath.Length));
            }
            else
            {
                script = ScriptsMap.SearchForIncludedFile(path, IncludePaths, cd, _scripts.GetScript);
            }
            if (script.IsValid)
            {
                if (once && _scripts.IsIncluded(script.Index))
                {
                    return(PhpValue.Create(true));
                }
                else
                {
                    return(script.MainMethod(this, locals, @this));
                }
            }
            else
            {
                if (throwOnError)
                {
                    throw new ArgumentException($"File '{path}' cannot be included with current configuration.");   // TODO: ErrCode
                }
                else
                {
                    return(PhpValue.Create(false));   // TODO: Warning
                }
            }
        }