Exemple #1
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);
        }
Exemple #2
0
        static void ConfigureOptions(PhpOptions options, IServiceProvider services)
        {
            foreach (var configservice in services.GetServices <IConfigureOptions <IPhpOptions> >())
            {
                configservice.Configure(options);
            }

            foreach (var configservice in services.GetServices <IPostConfigureOptions <IPhpOptions> >())
            {
                configservice.PostConfigure(Options.DefaultName, options);
            }

            //
            if (options.Session?.AutoStart == true)
            {
                options.RequestStart += ctx =>
                {
                    var httpctx = ctx.HttpPhpContext;
                    if (httpctx != null)
                    {
                        httpctx.SessionHandler?.StartSession(ctx, httpctx);
                    }
                };
            }
        }
Exemple #3
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;
            }
        }
Exemple #4
0
        public PhpHandlerMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, IServiceProvider services, PhpHandlerConfiguration configuration)
        {
            if (hostingEnv == null)
            {
                throw new ArgumentNullException(nameof(hostingEnv));
            }

            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

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

            if (_prefix.Value == "/")
            {
                _prefix = PathString.Empty;
            }

            // legacy options
            ConfigureOptions(_options, configuration.LegacyOptions);

            // sideload script assemblies
            LoadScriptAssemblies(_options);

            // global options
            ConfigureOptions(_options, services);

            // local options
            if (configuration.ConfigureContext != null)
            {
                _options.RequestStart += configuration.ConfigureContext;
            }

            // determine application's 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));
            }

            if (configuration.RootPath != null && configuration.RootPath != _rootPath)
            {
                _rootPath = Path.GetFullPath(Path.Combine(_rootPath, configuration.RootPath));
            }

            // normalize slashes
            _rootPath = NormalizeRootPath(_rootPath);

            // setup logger
            ConfigureLogger(_options, services.GetService <ILoggerFactory>());

            // TODO: pass hostingEnv.ContentRootFileProvider to the Context for file system functions
        }
Exemple #5
0
        /// <summary>
        /// Loads and reflects assemblies containing compiled PHP scripts.
        /// </summary>
        static void LoadScriptAssemblies(PhpOptions options)
        {
            if (options.ScriptAssemblyCollection.Count != 0)
            {
                foreach (var assembly in options.ScriptAssemblyCollection)
                {
                    Context.AddScriptReference(assembly);
                }
            }
            else if (TryLoadDependencyContext(out var dcontext))
            {
                // import libraries that has "Peachpie.App" as a dependency
                var runtimelibs = new HashSet <string>(StringComparer.OrdinalIgnoreCase)
                {
                    "Peachpie.App",
                    typeof(Context).Assembly.GetName().Name, // "Peachpie.Runtime"
                };

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

                    if (lib.Name == "Peachpie.App")
                    {
                        continue;
                    }

                    // process assembly if it has a dependency to runtime
                    var dependencies = lib.Dependencies;
                    for (int i = 0; i < dependencies.Count; i++)
                    {
                        if (runtimelibs.Contains(dependencies[i].Name))
                        {
                            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;
                        }
                    }
                }
            }
            else if (TryGetEntryAssembly(out var entryass))
            {
                Context.AddScriptReference(entryass);
            }
        }
Exemple #6
0
        /// <summary>
        /// Loads and reflects assemblies containing compiled PHP scripts.
        /// </summary>
        static void LoadScriptAssemblies(PhpOptions options)
        {
            if (options.ScriptAssemblyCollection.Count != 0)
            {
                foreach (var assembly in options.ScriptAssemblyCollection)
                {
                    Context.AddScriptReference(assembly);
                }
            }
            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;
                        }
                    }
                }
            }
        }