Pchp.Library.Streams.StreamWrapper.GetWrapperInternal C# (CSharp) Method

GetWrapperInternal() static private method

Search the lists of registered StreamWrappers to find the appropriate wrapper for a given scheme. When the scheme is empty, the FileStreamWrapper is returned.
static private GetWrapperInternal ( Context ctx, string scheme ) : StreamWrapper
ctx Pchp.Core.Context Current runtime context.
scheme string The scheme portion of an URL.
return StreamWrapper
        internal static StreamWrapper GetWrapperInternal(Context ctx, string scheme)
        {
            StreamWrapper result;

            // Note: FileStreamWrapper is returned both for "file" and for "".
            if (string.IsNullOrEmpty(scheme))
            {
                scheme = FileStreamWrapper.scheme;
            }

            // First search the system wrappers (always at least an empty Hashtable)
            if (!SystemStreamWrappers.TryGetValue(scheme, out result))
            {

                // Then look if the wrapper is implemented but not instantiated
                switch (scheme)
                {
                    case FileStreamWrapper.scheme:
                        return (StreamWrapper)(SystemStreamWrappers[scheme] = new FileStreamWrapper());
                    //case HttpStreamWrapper.scheme:
                    //    return (StreamWrapper)(SystemStreamWrappers[scheme] = new HttpStreamWrapper());
                    //case InputOutputStreamWrapper.scheme:
                    //    return (StreamWrapper)(SystemStreamWrappers[scheme] = new InputOutputStreamWrapper());
                }

                //// Next search the user wrappers (if present)
                //if (UserWrappers != null)
                //{
                //    UserWrappers.TryGetValue(scheme, out result);
                //}
            }

            //
            return result;  // can be null
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Registers a user-wrapper specified by the name of a defining user-class.
        /// </summary>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="protocol">The schema to be associated with the given wrapper.</param>
        /// <param name="classname">Name of the user class implementing the wrapper functions.</param>
        /// <param name="flags">Should be set to STREAM_IS_URL if protocol is a URL protocol. Default is 0, local stream.</param>
        /// <returns>False in case of failure (ex. schema already occupied).</returns>
        public static bool stream_wrapper_register(Context ctx, string protocol, string classname, StreamWrapperRegisterFlags flags = StreamWrapperRegisterFlags.Default)
        {
            // check if the scheme is already registered:
            if (string.IsNullOrEmpty(protocol) || StreamWrapper.GetWrapperInternal(ctx, protocol) == null)
            {
                // TODO: Warning?
                return(false);
            }

            var wrapperClass = ctx.GetDeclaredTypeOrThrow(classname, true);

            if (wrapperClass == null)
            {
                return(false);
            }

            // EX: [stream_wrapper_register]: create the user wrapper
            var wrapper = new UserStreamWrapper(ctx, protocol, wrapperClass, flags == StreamWrapperRegisterFlags.IsUrl);

            return(StreamWrapper.RegisterUserWrapper(ctx, protocol, wrapper));
        }