示例#1
0
        static void ConfigureOptions(PhpOptions options, PhpRequestOptions oldoptions)
        {
            if (oldoptions == null)
            {
                return;
            }

            if (oldoptions.StringEncoding != null)
            {
                options.StringEncoding = oldoptions.StringEncoding;
            }

            if (oldoptions.RootPath != null)
            {
                options.RootPath = oldoptions.RootPath;
            }

            if (oldoptions.ScriptAssembliesName != null)
            {
                foreach (var ass in oldoptions.ScriptAssembliesName)
                {
                    options.ScriptAssemblyCollection.Add(Assembly.Load(new AssemblyName(ass)));
                }
            }

            if (oldoptions.BeforeRequest != null)
            {
                options.RequestStart += oldoptions.BeforeRequest;
            }
        }
示例#2
0
        public PhpHandlerMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, PhpRequestOptions options)
        {
            if (hostingEnv == null)
            {
                throw new ArgumentNullException(nameof(hostingEnv));
            }

            _next    = next ?? throw new ArgumentNullException(nameof(next));
            _options = options;

            // determine Root Path:
            _rootPath = hostingEnv.GetDefaultRootPath();

            if (!string.IsNullOrEmpty(options.RootPath))
            {
                _rootPath = Path.GetFullPath(Path.Combine(_rootPath, options.RootPath));  // use the root path option, relative to the ASP.NET Core Web Root
            }

            _rootPath = NormalizeRootPath(_rootPath);

            //

            // TODO: pass hostingEnv.ContentRootFileProvider to the Context for file system functions

            // sideload script assemblies
            LoadScriptAssemblies(options);
        }
示例#3
0
        /// <summary>
        /// Loads and reflects assemblies containing compiled PHP scripts.
        /// </summary>
        static void LoadScriptAssemblies(PhpRequestOptions options)
        {
            if (options.ScriptAssembliesName != null)
            {
                foreach (var assname in options.ScriptAssembliesName)
                {
                    Context.AddScriptReference(Assembly.Load(new AssemblyName(assname)));
                }
            }
            else
            {
                var PeachpieRuntime = typeof(Context).Assembly.GetName().Name; // "Peachpie.Runtime"

                // reads dependencies from DependencyContext
                foreach (var lib in DependencyContext.Default.RuntimeLibraries)
                {
                    if (lib.Type != "package" && lib.Type != "project")
                    {
                        continue;
                    }

                    // process assembly if it has a dependency to runtime
                    var dependencies = lib.Dependencies;
                    for (int i = 0; i < dependencies.Count; i++)
                    {
                        if (dependencies[i].Name == PeachpieRuntime)
                        {
                            Context.AddScriptReference(Assembly.Load(new AssemblyName(lib.Name)));
                            break;
                        }
                    }
                }
            }
        }
示例#4
0
        /// <summary>
        /// Loads and reflects assemblies containing compiled PHP scripts.
        /// </summary>
        static void LoadScriptAssemblies(PhpRequestOptions options)
        {
            if (options.ScriptAssembliesName != null)
            {
                foreach (var assname in options.ScriptAssembliesName)
                {
                    Context.AddScriptReference(Assembly.Load(new AssemblyName(assname)));
                }
            }
            else
            {
                var PeachpieRuntime = typeof(Context).Assembly.GetName().Name; // "Peachpie.Runtime"

                // reads dependencies from DependencyContext
                foreach (var lib in DependencyContext.Default.RuntimeLibraries)
                {
                    if (lib.Type != "package" && lib.Type != "project")
                    {
                        continue;
                    }

                    if (lib.Name.StartsWith("Peachpie.", StringComparison.Ordinal))
                    {
                        continue;
                    }

                    // process assembly if it has a dependency to runtime
                    var dependencies = lib.Dependencies;
                    for (int i = 0; i < dependencies.Count; i++)
                    {
                        if (dependencies[i].Name == PeachpieRuntime)
                        {
                            try
                            {
                                // assuming DLL is deployed with the executable,
                                // and contained lib is the same name as package:
                                Context.AddScriptReference(Assembly.Load(new AssemblyName(lib.Name)));
                            }
                            catch
                            {
                                //
                            }
                            break;
                        }
                    }
                }
            }
        }
示例#5
0
 /// <summary>
 /// Loads and reflects assemblies containing compiled PHP scripts.
 /// </summary>
 static void LoadScriptAssemblies(PhpRequestOptions options)
 {
     if (options.ScriptAssembliesName != null)
     {
         foreach (var assname in options.ScriptAssembliesName.Select(str => new System.Reflection.AssemblyName(str)))
         {
             var ass = System.Reflection.Assembly.Load(assname);
             if (ass != null)
             {
                 Context.AddScriptReference(ass);
             }
             else
             {
                 LogEventSource.Log.ErrorLog($"Assembly '{assname}' couldn't be loaded.");
             }
         }
     }
 }
示例#6
0
        public PhpHandlerMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, IServiceProvider services, PhpRequestOptions oldoptions = null)
        {
            if (hostingEnv == null)
            {
                throw new ArgumentNullException(nameof(hostingEnv));
            }

            _next     = next ?? throw new ArgumentNullException(nameof(next));
            _rootPath = hostingEnv.GetDefaultRootPath();
            _options  = new PhpOptions(Context.DefaultPhpConfigurationService.Instance)
            {
                RootPath = _rootPath,
            };

            // configure global options:
            ConfigureOptions(_options, oldoptions);
            ConfigureOptions(_options, services);

            // determine resulting root Path:
            if (_options.RootPath != default && _options.RootPath != _rootPath)
            {
                // use the root path option, relative to the ASP.NET Core Web Root
                _rootPath = Path.GetFullPath(Path.Combine(_rootPath, _options.RootPath));
            }

            // normalize slashes
            _rootPath = NormalizeRootPath(_rootPath);

            // TODO: pass hostingEnv.ContentRootFileProvider to the Context for file system functions

            // sideload script assemblies
            LoadScriptAssemblies(_options);
        }
示例#7
0
 /// <summary>
 /// Installs request handler to compiled PHP scripts.
 /// </summary>
 public static IApplicationBuilder UsePhp(this IApplicationBuilder builder, PhpRequestOptions options = null)
 {
     return(builder.UseMiddleware <PhpHandlerMiddleware>(options ?? new PhpRequestOptions()));
 }