Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 26... Línea 26...
26
    public function __construct(
26
    public function __construct(
27
        StreamInterface $stream,
27
        StreamInterface $stream,
28
        StructureShape $shape,
28
        StructureShape $shape,
29
        AbstractParser $parser
29
        AbstractParser $parser
30
    ) {
30
    ) {
31
        $this->decodingIterator = new DecodingEventStreamIterator($stream);
31
        $this->decodingIterator = $this->chooseDecodingIterator($stream);
32
        $this->shape = $shape;
32
        $this->shape = $shape;
33
        $this->parser = $parser;
33
        $this->parser = $parser;
34
    }
34
    }
Línea -... Línea 35...
-
 
35
 
-
 
36
    /**
-
 
37
     * This method choose a decoding iterator implementation based on if the stream
-
 
38
     * is seekable or not.
-
 
39
     *
-
 
40
     * @param $stream
-
 
41
     *
-
 
42
     * @return Iterator
-
 
43
     */
-
 
44
    private function chooseDecodingIterator($stream)
-
 
45
    {
-
 
46
        if ($stream->isSeekable()) {
-
 
47
            return new DecodingEventStreamIterator($stream);
-
 
48
        } else {
-
 
49
            return new NonSeekableStreamDecodingEventStreamIterator($stream);
-
 
50
        }
-
 
51
    }
-
 
52
 
-
 
53
    /**
-
 
54
     * @return mixed
35
 
55
     */
36
    #[\ReturnTypeWillChange]
56
    #[\ReturnTypeWillChange]
37
    public function current()
57
    public function current()
38
    {
58
    {
39
        return $this->parseEvent($this->decodingIterator->current());
59
        return $this->parseEvent($this->decodingIterator->current());
Línea -... Línea 60...
-
 
60
    }
-
 
61
 
-
 
62
    /**
40
    }
63
     * @return mixed
41
 
64
     */
42
    #[\ReturnTypeWillChange]
65
    #[\ReturnTypeWillChange]
43
    public function key()
66
    public function key()
44
    {
67
    {
Línea -... Línea 68...
-
 
68
        return $this->decodingIterator->key();
-
 
69
    }
-
 
70
 
45
        return $this->decodingIterator->key();
71
    /**
46
    }
72
     * @return void
47
 
73
     */
48
    #[\ReturnTypeWillChange]
74
    #[\ReturnTypeWillChange]
49
    public function next()
75
    public function next()
Línea -... Línea 76...
-
 
76
    {
-
 
77
        $this->decodingIterator->next();
-
 
78
    }
50
    {
79
 
51
        $this->decodingIterator->next();
80
    /**
52
    }
81
     * @return void
53
 
82
     */
54
    #[\ReturnTypeWillChange]
83
    #[\ReturnTypeWillChange]
Línea -... Línea 84...
-
 
84
    public function rewind()
-
 
85
    {
-
 
86
        $this->decodingIterator->rewind();
55
    public function rewind()
87
    }
56
    {
88
 
57
        $this->decodingIterator->rewind();
89
    /**
58
    }
90
     * @return bool
59
 
91
     */
Línea 67... Línea 99...
67
    {
99
    {
68
        if (!empty($event['headers'][':message-type'])) {
100
        if (!empty($event['headers'][':message-type'])) {
69
            if ($event['headers'][':message-type'] === 'error') {
101
            if ($event['headers'][':message-type'] === 'error') {
70
                return $this->parseError($event);
102
                return $this->parseError($event);
71
            }
103
            }
-
 
104
 
72
            if ($event['headers'][':message-type'] !== 'event') {
105
            if ($event['headers'][':message-type'] !== 'event') {
73
                throw new ParserException('Failed to parse unknown message type.');
106
                throw new ParserException('Failed to parse unknown message type.');
74
            }
107
            }
75
        }
108
        }
Línea 76... Línea 109...
76
 
109
 
-
 
110
        $eventType = $event['headers'][':event-type'] ?? null;
77
        if (empty($event['headers'][':event-type'])) {
111
        if (empty($eventType)) {
78
            throw new ParserException('Failed to parse without event type.');
112
            throw new ParserException('Failed to parse without event type.');
79
        }
-
 
Línea -... Línea 113...
-
 
113
        }
-
 
114
 
-
 
115
        $eventPayload = $event['payload'];
-
 
116
        if ($eventType === 'initial-response') {
-
 
117
            return $this->parseInitialResponseEvent($eventPayload);
-
 
118
        }
-
 
119
 
-
 
120
        $eventShape = $this->shape->getMember($eventType);
-
 
121
 
-
 
122
        return [
-
 
123
            $eventType => array_merge(
-
 
124
                $this->parseEventHeaders($event['headers'], $eventShape),
-
 
125
                $this->parseEventPayload($eventPayload, $eventShape)
-
 
126
            )
-
 
127
        ];
-
 
128
    }
-
 
129
 
-
 
130
    /**
-
 
131
     * @param $headers
-
 
132
     * @param $eventShape
-
 
133
     *
-
 
134
     * @return array
-
 
135
     */
80
        $eventShape = $this->shape->getMember($event['headers'][':event-type']);
136
    private function parseEventHeaders($headers, $eventShape): array
81
 
137
    {
82
        $parsedEvent = [];
138
        $parsedHeaders = [];
-
 
139
        foreach ($eventShape->getMembers() as $memberName => $memberProps) {
-
 
140
            if (isset($memberProps['eventheader'])) {
-
 
141
                $parsedHeaders[$memberName] = $headers[$memberName];
-
 
142
            }
-
 
143
        }
-
 
144
 
-
 
145
        return $parsedHeaders;
-
 
146
    }
-
 
147
 
-
 
148
    /**
-
 
149
     * @param $payload
-
 
150
     * @param $eventShape
-
 
151
     *
-
 
152
     * @return array
-
 
153
     */
-
 
154
    private function parseEventPayload($payload, $eventShape): array
-
 
155
    {
83
        foreach ($eventShape['members'] as $shape => $details) {
156
        $parsedPayload = [];
-
 
157
        foreach ($eventShape->getMembers() as $memberName => $memberProps) {
84
            if (!empty($details['eventpayload'])) {
158
            $memberShape = $eventShape->getMember($memberName);
85
                $payloadShape = $eventShape->getMember($shape);
159
            if (isset($memberProps['eventpayload'])) {
86
                if ($payloadShape['type'] === 'blob') {
160
                if ($memberShape->getType() === 'blob') {
87
                    $parsedEvent[$shape] = $event['payload'];
161
                    $parsedPayload[$memberName] = $payload;
88
                } else {
162
                } else {
89
                    $parsedEvent[$shape] = $this->parser->parseMemberFromStream(
163
                    $parsedPayload[$memberName] = $this->parser->parseMemberFromStream(
90
                        $event['payload'],
164
                        $payload,
91
                        $payloadShape,
165
                        $memberShape,
92
                        null
166
                        null
-
 
167
                    );
93
                    );
168
                }
94
                }
-
 
95
            } else {
169
 
96
                $parsedEvent[$shape] = $event['headers'][$shape];
170
                break;
Línea -... Línea 171...
-
 
171
            }
97
            }
172
        }
-
 
173
 
98
        }
174
        if (empty($parsedPayload) && !empty($payload->getContents())) {
-
 
175
            /**
-
 
176
             * If we did not find a member with an eventpayload trait, then we should deserialize the payload
99
 
177
             * using the event's shape.
-
 
178
             */
-
 
179
            $parsedPayload = $this->parser->parseMemberFromStream($payload, $eventShape, null);
100
        return [
180
        }
Línea 101... Línea 181...
101
            $event['headers'][':event-type'] => $parsedEvent
181
 
102
        ];
182
        return $parsedPayload;
103
    }
183
    }
104
 
184
 
105
    private function parseError(array $event)
185
    private function parseError(array $event)
106
    {
186
    {
107
        throw new EventStreamDataException(
187
        throw new EventStreamDataException(
108
            $event['headers'][':error-code'],
-
 
109
            $event['headers'][':error-message']
188
            $event['headers'][':error-code'],
-
 
189
            $event['headers'][':error-message']
-
 
190
        );
-
 
191
    }
-
 
192
 
-
 
193
    private function parseInitialResponseEvent($payload): array
-
 
194
    {