public static function wrap(Doozr_Request_File_Stream_Interface $stream)
    {
        static $cache   = [];
        static $replace = ['_', '\\'];

        $handle      = $stream->getHandle();
        $calledClass = get_called_class();
        $protocol    = str_replace($replace, '-', $calledClass);

        // Register the stream wrapper decorator if needed. Cached - registering + checking wrappers is expensive.
        if (false === isset($cache[$protocol])) {
            self::registerProtocol($protocol, $calledClass);
            $cache[$protocol] = true;
        }

        $wrapper = fopen(
            $protocol.'://',
            stream_get_meta_data($handle)['mode'],
            null,
            stream_context_create(
                [DOOZR_NAMESPACE_FLAT => ['stream' => $handle]]
            )
        );

        if (false === $wrapper) {
            throw new RuntimeException(
                sprintf('Unable to wrap the stream with custom protocol: %s.', $protocol)
            );
        }

        return $wrapper;
    }