@param Stream $stream

@return array
188860
private function parseTypeDefinition(Stream $stream)
    {
        $definition = [];

        while ($stream->peek()->equals(Token::TYPE_SYMBOL, ':')) {
            $field = $this->parseIdentifier($stream);

            $stream->expects(Token::TYPE_SYMBOL, ':');

            $type = $this->parseTypeAlias($stream);

            $stream->expects(Token::TYPE_SYMBOL, ';');

            $definition['mapping'][$field] = $type;
        }

        while ($stream->peek()->equals(Token::TYPE_SYMBOL, '=')) {
            $name = $stream->expects(Token::TYPE_IDENTIFIER)->getValue();

            $stream->expects(Token::TYPE_SYMBOL, '=');

            $value = $this->parseValue($stream);

            $stream->expects(Token::TYPE_SYMBOL, ';');

            $definition['options'][$name] = $value;
        }

        return $definition;
    }
1002334
private function parseImport(Stream $stream)
    {
        $stream->expects(Token::TYPE_KEYWORD, 'import');

        if ($stream->current()->equals(Token::TYPE_SYMBOL, '{')) {
            $stream->expects(Token::TYPE_SYMBOL, '{');

            $map = $this->parseImportList($stream);

            $stream->expects(Token::TYPE_SYMBOL, '}');
        } else {
            $stream->expects(Token::TYPE_SYMBOL, '*');

            $map = null;
        }

        $stream->expects(Token::TYPE_KEYWORD, 'from');

        $path = $stream->expects(Token::TYPE_STRING | Token::TYPE_SINGLE_QUOTED)->getValue();

        $stream->expects(Token::TYPE_SYMBOL, ';');

        return [$path, $map];
    }
1265386
public function readStream($path) {
        $mapping = $this->mapper->get($path);

        if (!$mapping) {
            return false;
        }

        try {
            $file = $this->client->get($mapping['fid']);

            if (!$file) {
                return false;
            }

            return [
                'stream' => $file
            ];
        } catch (SeaweedFSException $e) {
            return false;
        }
    }
531343
private function parseValue(Stream $stream)
    {
        if ($stream->current()->is(Token::TYPE_STRING | Token::TYPE_SINGLE_QUOTED)) {
            return new ValueNode($stream->expects(Token::TYPE_STRING | Token::TYPE_SINGLE_QUOTED)->getValue());
        }

        if ($stream->current()->is(Token::TYPE_NUMBER)) {
            $number = $stream->expects(Token::TYPE_NUMBER)->getValue();

            return new ValueNode(strpos($number, '.') === false ? intval($number) : floatval($number));
        }

        if ($stream->current()->is(Token::TYPE_CONSTANT)) {
            $constant = $stream->expects(Token::TYPE_CONSTANT)->getValue();

            switch ($constant) {
                case 'true':
                    return new ValueNode(true);

                case 'false':
                    return new ValueNode(false);

                case 'null':
                    return new ValueNode(null);

                default:
                    throw new ParserException(sprintf('Unexpected constant "%s" found', $constant));
            }
        }

        if ($stream->current()->equals(Token::TYPE_SYMBOL, '[')) {
            $stream->expects(Token::TYPE_SYMBOL, '[');

            if (!$stream->current()->equals(Token::TYPE_SYMBOL, ']')) {
                $array = $this->parseList($stream);
            } else {
                $array = [];
            }

            $stream->expects(Token::TYPE_SYMBOL, ']');

            return new ValueNode($array);
        }

        if ($stream->current()->equals(Token::TYPE_SYMBOL, '{')) {
            $stream->expects(Token::TYPE_SYMBOL, '{');

            if (!$stream->current()->equals(Token::TYPE_SYMBOL, '}')) {
                $hash = $this->parseHashList($stream);
            } else {
                $hash = [];
            }

            $stream->expects(Token::TYPE_SYMBOL, '}');

            return new ValueNode($hash);
        }

        if ($stream->current()->equals(Token::TYPE_KEYWORD, 'new')) {
            return $this->parseClass($stream);
        }

        throw new ParserException(sprintf('Can\'t parse the stream "%s".', $stream->humanize()));
    }
1059855
public function supportsExpr(Stream $stream)
    {
        $token = $stream->current();

        return !$token->is(Token::TYPE_KEYWORD) && ($token->is(Token::TYPE_STRING) || $token->is(Token::TYPE_NUMBER) || $token->is(Token::TYPE_CONSTANT) || $token->is(Token::TYPE_IDENTIFIER) || $token->equals(Token::TYPE_SYMBOL, '{') || $token->equals(Token::TYPE_SYMBOL, '['));
    }
337919
public function readZigzag(Stream $stream)
    {
        $number = $this->readVarint($stream);
        $zigzag = ($number >> 1) ^ (-($number & 1));

        return $zigzag;
    }
45304
public function read($path)
    {
        if ($response = $this->readStream($path)) {
            return ['contents' => stream_get_contents($response['stream']), 'path' => $response['path']];
        }

        return false;
    }
147823
public function query(array $stream, array $filters = [])
    {
        $qb = QueryBuilder::create((array)$stream);

        foreach ($filters as $key => $value) {
            $qb->addCriterion($key, $value);
        }

        $qb->sortedBy('occurred_on', 'ASC');

        return $qb->getResults();
    }
137566
public function getStreams($filter = null)
	{
		if ($filter !== null)
		{
			if ( ! is_callable($filter))
			{
				$filter = function (StreamInterface $stream) use ($filter) {
					return (bool) ($filter & $stream->getType());
				};
			}
			
			$streams = clone $this->streams;
			
			foreach ($streams as $index => $stream)
			{
				if ($filter($stream) === false)
				{
					$streams->offsetUnset($index);
				}
			}
			
			return $streams;
		}
		
		return $this->streams;
	}
316660
public function parseExpr(Stream $stream)
    {
        if ($stream->current()->is(Token::TYPE_STRING | Token::TYPE_SINGLE_QUOTED)) {
            return new ValueFragmentNode($stream->expects(Token::TYPE_STRING | Token::TYPE_SINGLE_QUOTED)->getValue(), 'varchar');
        }

        if ($stream->current()->is(Token::TYPE_STRING | Token::TYPE_DOUBLE_QUOTED)) {
            return new FragmentNode($stream->expects(Token::TYPE_STRING | Token::TYPE_DOUBLE_QUOTED)->getValue());
        }

        if ($stream->current()->is(Token::TYPE_NUMBER)) {
            $value = $stream->expects(Token::TYPE_NUMBER)->getValue();

            return new ValueFragmentNode($value, strpos($value, '.') === false ? 'int8' : 'decimal');
        }

        if ($stream->current()->is(Token::TYPE_CONSTANT)) {
            $value = strtolower($stream->expects(Token::TYPE_CONSTANT)->getValue());

            return new ValueFragmentNode($value, in_array($value, ['true', 'false']) ? 'bool' : null);
        }

        if ($stream->current()->is(Token::TYPE_IDENTIFIER) && $stream->peek()->equals(Token::TYPE_SYMBOL, '(')) {
            $func = $stream->expects(Token::TYPE_IDENTIFIER)->getValue();
            $args = [];

            $stream->expects(Token::TYPE_SYMBOL, '(');

            if (!$stream->current()->equals(Token::TYPE_SYMBOL, ')')) {
                do {
                    $args[] = $this->parseExpr($stream);
                } while ($stream->current()->equals(Token::TYPE_SYMBOL, ',') && $stream->expects(Token::TYPE_SYMBOL, ','));
            }

            $func = new FuncNode($func, $args);

            $stream->expects(Token::TYPE_SYMBOL, ')');

            return $func;
        }

        if ($stream->current()->is(Token::TYPE_IDENTIFIER)) {
            $path = [$stream->expects(Token::TYPE_IDENTIFIER)->getValue()];

            while ($stream->hasNext() && $stream->current()->equals(Token::TYPE_SYMBOL, '.')) {
                $stream->expects(Token::TYPE_SYMBOL, '.');

                $path[] = $stream->expects(Token::TYPE_IDENTIFIER)->getValue();
            }

            return new ReferenceNode($path);
        }

        if ($stream->current()->equals(Token::TYPE_SYMBOL, '{')) {
            $stream->expects(Token::TYPE_SYMBOL, '{');

            $hash = [];

            if (!$stream->current()->equals(Token::TYPE_SYMBOL, '}')) {
                do {
                    $key = $stream->expects(Token::TYPE_IDENTIFIER)->getValue();

                    $stream->expects(Token::TYPE_SYMBOL, ':');

                    $value = $this->parseExpr($stream);

                    $hash[$key] = $value;
                } while ($stream->current()->equals(Token::TYPE_SYMBOL, ',') && $stream->expects(Token::TYPE_SYMBOL, ','));
            }

            $stream->expects(Token::TYPE_SYMBOL, '}');

            return new RowFragmentNode($hash);
        }

        if ($stream->current()->equals(Token::TYPE_SYMBOL, '[')) {
            $stream->expects(Token::TYPE_SYMBOL, '[');

            $array = [];

            if (!$stream->current()->equals(Token::TYPE_SYMBOL, ']')) {
                do {
                    $array[] = $this->parseExpr($stream);
                } while ($stream->current()->equals(Token::TYPE_SYMBOL, ',') && $stream->expects(Token::TYPE_SYMBOL, ','));
            }

            $stream->expects(Token::TYPE_SYMBOL, ']');

            return new SetFragmentNode($array);
        }

        throw new \Exception(sprintf(
            'Fails to parse stream "%s". Unexpected token %s at line %d.',
            $stream->humanize(),
            Token::humanize($stream->current()->getType()),
            $stream->current()->getLine()
        ));
    }
1093684
public function readSFixed64(Stream $stream)
    {
        $bytes = $stream->read(8);

        list(, $lo0, $lo1, $hi0, $hi1) = unpack('v*', $bytes);

        return ($hi1 << 16 | $hi0) << 32 | ($lo1 << 16 | $lo0);
    }
795184
public function receive(ProtoStream $stream = null)
    {
        $socket = $stream ?: $this->getStream();
        $length = $this->receiveLengthHeader($socket);
        $code   = $this->receiveMessageCode($socket);
        $body   = ($length > 1)
            ? $this->receiveMessageBody($socket, $length - 1)
            : null;

        return [$code, $body];
    }
222049
public function append($stream)
    {
        $streams = func_get_args();

        array_unshift($streams, $this);

        $generator = function () use ($streams) {
            foreach ($streams as $stream) {
                foreach ($stream as $element) {
                    yield $element;
                }
            }
        };

        return new IteratorStream($generator());
    }
160546
public function hasStream($stream)
    {
        $isInputStream = $stream == $this->getInputStream();
        $isOutputStream = $stream == $this->getOutputStream();
        $isErrStream = $stream == $this->getErrorStream();

        return $isInputStream || $isOutputStream || $isErrStream;
    }
510626
public function initFromStream($stream)
    {
        $stream = Factory::stream($stream);

        try {
            $offset = $stream->tell();
        } catch (\RuntimeException $e) {
            $offset = 0;
        }

        $shouldAndCanSeek = $offset !== 0 && $stream->isSeekable();

        if ($shouldAndCanSeek) {
            $stream->rewind();
        }

        try {
            $data = $stream->getContents();
        } catch (\RuntimeException $e) {
            $data = null;
        }

        if ($shouldAndCanSeek) {
            $stream->seek($offset);
        }

        if ($data) {
            return $this->initFromBinary($data);
        }

        throw new Exception\NotReadableException(
            "Unable to init from given stream"
        );
    }
377366
public static function getExtBySignature($stream)
    {
        $prefix = strval(bin2hex(mb_strcut($stream, 0, 10)));

        foreach (self::$signatures as $signature => $extension) {
            if (0 === strpos($prefix, strval($signature))) {
                return $extension;
            }
        }

        return '';
    }
1180137
public function toStreamContextArray(): array
    {
        $options = [
            'crypto_method' => $this->toStreamCryptoMethod(),
            'peer_name' => $this->peerName,
            'verify_peer' => $this->verifyPeer,
            'verify_peer_name' => $this->verifyPeer,
            'verify_depth' => $this->verifyDepth,
            'ciphers' => $this->ciphers ?? \OPENSSL_DEFAULT_STREAM_CIPHERS,
            'honor_cipher_order' => true,
            'single_dh_use' => true,
            'no_ticket' => true,
            'capture_peer_cert' => $this->capturePeer,
            'capture_peer_chain' => $this->capturePeer,
        ];

        if ($this->defaultCertificate !== null) {
            $options['local_cert'] = $this->defaultCertificate->getCertFile();

            if ($this->defaultCertificate->getCertFile() !== $this->defaultCertificate->getKeyFile()) {
                $options['local_pk'] = $this->defaultCertificate->getKeyFile();
            }
        }

        if ($this->certificates) {
            $options['SNI_server_certs'] = \array_map(static function (Certificate $certificate) {
                if ($certificate->getCertFile() === $certificate->getKeyFile()) {
                    return $certificate->getCertFile();
                }

                return [
                    'local_cert' => $certificate->getCertFile(),
                    'local_pk' => $certificate->getKeyFile(),
                ];
            }, $this->certificates);
        }

        if ($this->caFile !== null) {
            $options['cafile'] = $this->caFile;
        }

        if ($this->caPath !== null) {
            $options['capath'] = $this->caPath;
        }

        if (\OPENSSL_VERSION_NUMBER >= 0x10100000) {
            $options['security_level'] = $this->securityLevel;
        }

        return ['ssl' => $options];
    }