Open a stream

@param string $uri
@return resource
@throws \RuntimeException if stream failed to open
497344
protected function openStream(string $uri)
    {
        $scheme = parse_url($uri, PHP_URL_SCHEME);

        if (!isset($scheme) || $scheme === '') {
            throw new \InvalidArgumentException("Invalid URI '$uri'; scheme missing");
        }

        $reporting = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
        try {
            $stream = fopen($uri, 'w'); // Just in case stream wrapper throws an exception
        } finally {
            error_reporting($reporting);
        }

        if (!is_resource($stream)) {
            ['message' => $message] = error_get_last() ?? ['message' => "Failed to open '$uri'"];
            throw new \RuntimeException($message);
        }

        return $stream;
    }
61460
public function resolve($resource): Stream
    {
        // detect best way to load and process configuration content
        if (is_array($resource)) {
            return new DataStream($resource, new ArrayProcessor());
        } elseif (is_callable($resource)) {
            return new DataStream($resource, new CallableProcessor());
        } elseif ($resource instanceof Config) {
            return new DataStream($resource, new ConfigProcessor());
        } elseif ($resource instanceof Collection) {
            return new DataStream($resource, new CollectionProcessor());
        } elseif ($resource instanceof Stream) {
            return $resource;
        } elseif ($resource instanceof stdClass) {
            return new DataStream($resource, new StdClassProcessor());
        } elseif (is_string($resource)) {
            $filesHandlers = $this->filesHandlers;
            if (null === $filesHandlers) {
                $filesHandlers = new FilesHandlers(null);
            }
            return new FileStream($resource, $filesHandlers);
        }

        throw new UnknownResourceException($resource);
    }
1370859
public static function create($resource = '', array $options = []): StreamInterface
    {
        switch (gettype($resource)) {
            case 'string':
                $stream = fopen('php://temp', 'r+');
                if ($resource !== '') {
                    fwrite($stream, $resource);
                    fseek($stream, 0);
                }
                return new Stream($stream, $options);
            case 'resource':
                return new Stream($resource, $options);
            case 'object':
                if ($resource instanceof StreamInterface) {
                    return $resource;
                } elseif ($resource instanceof \Traversable) {
                    return new IteratorStream($resource, $options);
                } elseif (method_exists($resource, '__toString')) {
                    return static::createStream((string)$resource, $options);
                }
                break;
            case 'NULL':
                return new Stream('php://temp', 'r+');
        }

        throw new InvalidStreamException(
            'Invalid resource type: ' . gettype($resource)
        );
    }
1320217
public function get($uri)
    {
        $options = array(
            CURLOPT_URL => $this->domain.'/'.ltrim($uri, '/'),
            CURLOPT_FOLLOWLOCATION => 1,
            CURLOPT_RETURNTRANSFER => 1,
        );

        return $this->exec($options);
    }
871537
public function read($length = null)
    {
        $this->assertReadable();
        if (null == $length) {
            $length = $this->bufferSize;
        }
        $ret = fread($this->stream, $length);
        if (false === $ret) {
            throw new RuntimeException('Cannot read stream');
        }
        
        return $ret;
    }
1252486
public function open($uri, $mode='r+', $lock=false)
    {

        if (is_resource($uri)) {
            return new ResourceStream($uri, $lock);
        }

        if (!Type::isStringLike($uri) && !$uri instanceof UrlContract) {
            throw new TypeException('$uri has to be string or ' . Type::of($uri));
        }

        $uri = $uri instanceof UrlContract ? $uri : new Url($uri);

        if ($uri->scheme == 'http' || $uri->scheme == 'https' && class_exists(HttpFileStream::class)) {
            return new HttpFileStream($uri, $mode, $lock);
        }

        return new FileStream($uri, $mode, $lock);
    }
545309
public static function call($uri)
    {
        return new \OtherCode\Rest\Rest(new \OtherCode\Rest\Core\Configuration(array(
            'url' => $uri
        )));
    }
823926
private function createUri($uri)
    {
        if ($this->hasBaseUri() && (stripos($uri, $baseUri = (string) $this->getBaseUri()) === false)) {
            return $baseUri.$uri;
        }

        return $uri;
    }
139664
public function resolveReader(ResourceUri $resource)
    {
        if (! array_key_exists($resource->getProtocol(), $this->protocolFactoryIndexes)) {
            throw new \RuntimeException('Unsupported protocol: ' . $resource->getProtocol() . '( ' . $resource  . ')');
        }

        /** @var int $factoryIndex */
        $factoryIndex = $this->protocolFactoryIndexes[$resource->getProtocol()];
        /** @var ResourceReaderFactory $factory */
        $factory = $this->factories[$factoryIndex];

        return $factory->createReaderFor($resource);
    }
282829
public static function createFromString(string $uri): self
    {
        if (!\is_string($uri) && !\method_exists($uri, '__toString')) {
            throw new \InvalidArgumentException('Uri must be a string');
        }

        $parts    = \parse_url($uri);
        $scheme   = $parts['scheme'] ?? '';
        $user     = $parts['user'] ?? '';
        $pass     = $parts['pass'] ?? '';
        $host     = $parts['host'] ?? '';
        $port     = $parts['port'] ?? null;
        $path     = $parts['path'] ?? '';
        $query    = $parts['query'] ?? '';
        $fragment = $parts['fragment'] ?? '';

        return new static($scheme, $host, $port, $path, $query, $fragment, $user, $pass);
    }
677577
public function open(string $uri, string $content)
    {
        if (isset($this->documents[$uri])) {
            $document = $this->documents[$uri];
            $document->updateContent($content);
        } else {
            $document = $this->create($uri, $content);
            $this->documents[$uri] = $document;
        }
        return $document;
    }
685098
protected function seePageIs($uri)
    {
        $this->assertPageLoaded($uri = $this->prepareUrlForRequest($uri));

        $this->assertEquals(
            $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
        );

        return $this;
    }
1024034
public static function createFromResource($resource): Stream
    {
        if (!\is_resource($resource))
        {
            throw new \InvalidArgumentException('Stream must be a resource');
        }

        $obj           = new self();
        $obj->stream   = $resource;
        $meta          = stream_get_meta_data($obj->stream);
        $obj->seekable = $meta['seekable'];
        $obj->readable = isset(self::$readWriteHash['read'][$meta['mode']]);
        $obj->writable = isset(self::$readWriteHash['write'][$meta['mode']]);
        $obj->uri      = $obj->getMetadata('uri');

        return $obj;
    }
739050
public function writeStream($uri, $resource, array $config = [])
    {
        return $this->mount_manager->writeStream($uri, $resource, $config);
    }
1272008
public static function releaseUri($uri)
    {
        if ($uri instanceof StreamInterface) {
            $uri = 'spiral://' . spl_object_hash($uri);
        }

        unset(self::$uris[$uri]);
    }
784094
public function setBaseUrl($uri)
    {
        if (is_string($uri)) {
            $this->baseUrl = Uri::createFromString($uri);
        } else {
            $this->baseUrl = $uri;
        }
        return $this;
    }
1091092
public function parse($uri)
    {
        parent::parse($uri);

        if (empty($this->path)) {
            $this->path = '/';
        }

        return $this;
    }