protected function registerStreamHandlers()
    {
        $handlers = $this->getConfig()->get('stream_handlers', null, array());
        if (!is_array($handlers)) {
            throw new ConfigException('Invalid stream_handlers configuration', E_USER_ERROR);
        }

        $defaultStreamContext = array();
        foreach ($handlers as $protocol => $handler) {
            if (isset($handler['class'])) {
                try {
                    $flags = isset($handler['local']) ? !empty($handler['local']) : STREAM_IS_URL;
                    if (isset($handler['register_callback']) && is_callable($handler['register_callback'])) {
                        $registered = $handler['register_callback']($protocol, $handler);
                    } else {
                        if (in_array($protocol, stream_get_wrappers())) {
                            stream_wrapper_unregister($protocol);
                        }
                        $registered = stream_register_wrapper($protocol, $handler['class'], $flags);
                    }
                    if (isset($handler['options']) && is_array($handler['options'])) {
                        $defaultStreamContext[$protocol] = $handler['options'];
                    }
                } catch (\Exception $e) {
                    throw new ConfigException("Error registering stream_handler {$handler['class']} ({$protocol}://)", E_USER_ERROR, $e);
                }
                if (!$registered) {
                    throw new ConfigException("Could not register stream_handler {$handler['class']} ({$protocol}://)", E_USER_ERROR);
                }
            } else {
                throw new ConfigException("Invalid stream_handler configuration for protocol {$protocol}", E_USER_ERROR);
            }
        }

        $this->streamContext = stream_context_set_default($defaultStreamContext);
    }