forked from hoaproject/Stream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStream.php
More file actions
699 lines (615 loc) · 17.8 KB
/
Stream.php
File metadata and controls
699 lines (615 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
<?php
/**
* Hoa
*
*
* @license
*
* New BSD License
*
* Copyright © 2007-2017, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Hoa nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
namespace Hoa\Stream;
use Hoa\Consistency;
use Hoa\Event;
use Hoa\Protocol;
/**
* Class \Hoa\Stream.
*
* Static register for all streams (files, sockets etc.).
*
* @copyright Copyright © 2007-2017 Hoa community
* @license New BSD License
*/
abstract class Stream implements IStream\Stream, Event\Listenable
{
use Event\Listens;
/**
* Name index in the stream bucket.
*
* @const int
*/
const NAME = 0;
/**
* Handler index in the stream bucket.
*
* @const int
*/
const HANDLER = 1;
/**
* Resource index in the stream bucket.
*
* @const int
*/
const RESOURCE = 2;
/**
* Context index in the stream bucket.
*
* @const int
*/
const CONTEXT = 3;
/**
* Default buffer size.
*
* @const int
*/
const DEFAULT_BUFFER_SIZE = 8192;
/**
* Current stream bucket.
*
* @var array
*/
protected $_bucket = [];
/**
* Static stream register.
*
* @var array
*/
private static $_register = [];
/**
* Buffer size (default is 8Ko).
*
* @var bool
*/
protected $_bufferSize = self::DEFAULT_BUFFER_SIZE;
/**
* Original stream name, given to the stream constructor.
*
* @var string
*/
protected $_streamName = null;
/**
* Context name.
*
* @var string
*/
protected $_context = null;
/**
* Whether the opening has been deferred.
*
* @var bool
*/
protected $_hasBeenDeferred = false;
/**
* Whether this stream is already opened by another handler.
*
* @var bool
*/
protected $_borrowing = false;
/**
* Set the current stream.
* If not exists in the register, try to call the
* `$this->_open()` method. Please, see the `self::_getStream()` method.
*
* @param string $streamName Stream name (e.g. path or URL).
* @param string $context Context ID (please, see the
* `Hoa\Stream\Context` class).
* @param bool $wait Differ opening or not.
*/
public function __construct($streamName, $context = null, $wait = false)
{
$this->_streamName = $streamName;
$this->_context = $context;
$this->_hasBeenDeferred = $wait;
$this->setListener(
new Event\Listener(
$this,
[
'authrequire',
'authresult',
'complete',
'connect',
'failure',
'mimetype',
'progress',
'redirect',
'resolve',
'size'
]
)
);
if (true === $wait) {
return;
}
$this->open();
return;
}
/**
* Get a stream in the register.
* If the stream does not exist, try to open it by calling the
* $handler->_open() method.
*
* @param string $streamName Stream name.
* @param \Hoa\Stream $handler Stream handler.
* @param string $context Context ID (please, see the
* \Hoa\Stream\Context class).
* @return array
* @throws \Hoa\Stream\Exception
*/
private static function &_getStream(
$streamName,
Stream $handler,
$context = null
) {
$name = md5($streamName);
if (null !== $context) {
if (false === Context::contextExists($context)) {
throw new Exception(
'Context %s was not previously declared, cannot retrieve ' .
'this context.',
0,
$context
);
}
$context = Context::getInstance($context);
}
if (!isset(self::$_register[$name])) {
self::$_register[$name] = [
self::NAME => $streamName,
self::HANDLER => $handler,
self::RESOURCE => $handler->_open($streamName, $context),
self::CONTEXT => $context
];
Event::register(
'hoa://Event/Stream/' . $streamName,
$handler
);
// Add :open-ready?
Event::register(
'hoa://Event/Stream/' . $streamName . ':close-before',
$handler
);
} else {
$handler->_borrowing = true;
}
if (null === self::$_register[$name][self::RESOURCE]) {
self::$_register[$name][self::RESOURCE]
= $handler->_open($streamName, $context);
}
return self::$_register[$name];
}
/**
* Open the stream and return the associated resource.
* Note: This method is protected, but do not forget that it could be
* overloaded into a public context.
*
* @param string $streamName Stream name (e.g. path or URL).
* @param \Hoa\Stream\Context $context Context.
* @return resource
* @throws \Hoa\Exception\Exception
*/
abstract protected function &_open($streamName, Context $context = null);
/**
* Close the current stream.
* Note: this method is protected, but do not forget that it could be
* overloaded into a public context.
*
* @return bool
*/
abstract protected function _close();
/**
* Open the stream.
*
* @return \Hoa\Stream
* @throws \Hoa\Stream\Exception
*/
final public function open()
{
$context = $this->_context;
if (true === $this->hasBeenDeferred()) {
if (null === $context) {
$handle = Context::getInstance(uniqid());
$handle->setParameters([
'notification' => [$this, '_notify']
]);
$context = $handle->getId();
} elseif (true === Context::contextExists($context)) {
$handle = Context::getInstance($context);
$parameters = $handle->getParameters();
if (!isset($parameters['notification'])) {
$handle->setParameters([
'notification' => [$this, '_notify']
]);
}
}
}
$this->_bufferSize = self::DEFAULT_BUFFER_SIZE;
$this->_bucket = self::_getStream(
$this->_streamName,
$this,
$context
);
return $this;
}
/**
* Close the current stream.
*
* @return void
*/
final public function close()
{
$streamName = $this->getStreamName();
$name = md5($streamName);
if (!isset(self::$_register[$name])) {
return;
}
Event::notify(
'hoa://Event/Stream/' . $streamName . ':close-before',
$this,
new Event\Bucket()
);
if (false === $this->_close()) {
return;
}
unset(self::$_register[$name]);
$this->_bucket[self::HANDLER] = null;
Event::unregister(
'hoa://Event/Stream/' . $streamName
);
Event::unregister(
'hoa://Event/Stream/' . $streamName . ':close-before'
);
return;
}
/**
* Get the current stream name.
*
* @return string
*/
public function getStreamName()
{
if (empty($this->_bucket)) {
return null;
}
return $this->_bucket[self::NAME];
}
/**
* Get the current stream.
*
* @return resource
*/
public function getStream()
{
if (empty($this->_bucket)) {
return null;
}
return $this->_bucket[self::RESOURCE];
}
/**
* Get the current stream context.
*
* @return \Hoa\Stream\Context
*/
public function getStreamContext()
{
if (empty($this->_bucket)) {
return null;
}
return $this->_bucket[self::CONTEXT];
}
/**
* Get stream handler according to its name.
*
* @param string $streamName Stream name.
* @return \Hoa\Stream
*/
public static function getStreamHandler($streamName)
{
$name = md5($streamName);
if (!isset(self::$_register[$name])) {
return null;
}
return self::$_register[$name][self::HANDLER];
}
/**
* Set the current stream. Useful to manage a stack of streams (e.g. socket
* and select). Notice that it could be unsafe to use this method without
* taking time to think about it two minutes. Resource of type “Unknown” is
* considered as valid.
*
* @return resource
* @throws \Hoa\Stream\Exception
*/
public function _setStream($stream)
{
if (false === is_resource($stream) &&
('resource' !== gettype($stream) ||
'Unknown' !== get_resource_type($stream))) {
throw new Exception(
'Try to change the stream resource with an invalid one; ' .
'given %s.',
1,
gettype($stream)
);
}
$old = $this->_bucket[self::RESOURCE];
$this->_bucket[self::RESOURCE] = $stream;
return $old;
}
/**
* Check if the stream is opened.
*
* @return bool
*/
public function isOpened()
{
return is_resource($this->getStream());
}
/**
* Set the timeout period.
*
* @param int $seconds Timeout period in seconds.
* @param int $microseconds Timeout period in microseconds.
* @return bool
*/
public function setStreamTimeout($seconds, $microseconds = 0)
{
return stream_set_timeout($this->getStream(), $seconds, $microseconds);
}
/**
* Whether the opening of the stream has been deferred
*/
protected function hasBeenDeferred()
{
return $this->_hasBeenDeferred;
}
/**
* Check whether the connection has timed out or not.
* This is basically a shortcut of `getStreamMetaData` + the `timed_out`
* index, but the resulting code is more readable.
*
* @return bool
*/
public function hasTimedOut()
{
$metaData = $this->getStreamMetaData();
return true === $metaData['timed_out'];
}
/**
* Set blocking/non-blocking mode.
*
* @param bool $mode Blocking mode.
* @return bool
*/
public function setStreamBlocking($mode)
{
return stream_set_blocking($this->getStream(), (int) $mode);
}
/**
* Set stream buffer.
* Output using fwrite() (or similar function) is normally buffered at 8 Ko.
* This means that if there are two processes wanting to write to the same
* output stream, each is paused after 8 Ko of data to allow the other to
* write.
*
* @param int $buffer Number of bytes to buffer. If zero, write
* operations are unbuffered. This ensures that
* all writes are completed before other
* processes are allowed to write to that output
* stream.
* @return bool
*/
public function setStreamBuffer($buffer)
{
// Zero means success.
$out = 0 === stream_set_write_buffer($this->getStream(), $buffer);
if (true === $out) {
$this->_bufferSize = $buffer;
}
return $out;
}
/**
* Disable stream buffering.
* Alias of $this->setBuffer(0).
*
* @return bool
*/
public function disableStreamBuffer()
{
return $this->setStreamBuffer(0);
}
/**
* Get stream buffer size.
*
* @return int
*/
public function getStreamBufferSize()
{
return $this->_bufferSize;
}
/**
* Get stream wrapper name.
*
* @return string
*/
public function getStreamWrapperName()
{
if (false === $pos = strpos($this->getStreamName(), '://')) {
return 'file';
}
return substr($this->getStreamName(), 0, $pos);
}
/**
* Get stream meta data.
*
* @return array
*/
public function getStreamMetaData()
{
return stream_get_meta_data($this->getStream());
}
/**
* Whether this stream is already opened by another handler.
*
* @return bool
*/
public function isBorrowing()
{
return $this->_borrowing;
}
/**
* Notification callback.
*
* @param int $ncode Notification code. Please, see
* STREAM_NOTIFY_* constants.
* @param int $severity Severity. Please, see
* STREAM_NOTIFY_SEVERITY_* constants.
* @param string $message Message.
* @param int $code Message code.
* @param int $transferred If applicable, the number of transferred
* bytes.
* @param int $max If applicable, the number of max bytes.
* @return void
*/
public function _notify(
$ncode,
$severity,
$message,
$code,
$transferred,
$max
) {
static $_map = [
STREAM_NOTIFY_AUTH_REQUIRED => 'authrequire',
STREAM_NOTIFY_AUTH_RESULT => 'authresult',
STREAM_NOTIFY_COMPLETED => 'complete',
STREAM_NOTIFY_CONNECT => 'connect',
STREAM_NOTIFY_FAILURE => 'failure',
STREAM_NOTIFY_MIME_TYPE_IS => 'mimetype',
STREAM_NOTIFY_PROGRESS => 'progress',
STREAM_NOTIFY_REDIRECTED => 'redirect',
STREAM_NOTIFY_RESOLVE => 'resolve',
STREAM_NOTIFY_FILE_SIZE_IS => 'size'
];
$this->getListener()->fire($_map[$ncode], new Event\Bucket([
'code' => $code,
'severity' => $severity,
'message' => $message,
'transferred' => $transferred,
'max' => $max
]));
return;
}
/**
* Call the $handler->close() method on each stream in the static stream
* register.
* This method does not check the return value of $handler->close(). Thus,
* if a stream is persistent, the $handler->close() should do anything. It
* is a very generic method.
*
* @return void
*/
final public static function _Hoa_Stream()
{
foreach (self::$_register as $entry) {
$entry[self::HANDLER]->close();
}
return;
}
/**
* Transform object to string.
*
* @return string
*/
public function __toString()
{
return $this->getStreamName();
}
/**
* Close the stream when destructing.
*
* @return void
*/
public function __destruct()
{
if (false === $this->isOpened()) {
return;
}
$this->close();
return;
}
}
/**
* Class \Hoa\Stream\_Protocol.
*
* The `hoa://Library/Stream` node.
*
* @copyright Copyright © 2007-2017 Hoa community
* @license New BSD License
*/
class _Protocol extends Protocol\Node
{
/**
* Component's name.
*
* @var string
*/
protected $_name = 'Stream';
/**
* ID of the component.
*
* @param string $id ID of the component.
* @return mixed
*/
public function reachId($id)
{
return Stream::getStreamHandler($id);
}
}
/**
* Flex entity.
*/
Consistency::flexEntity('Hoa\Stream\Stream');
/**
* Shutdown method.
*/
Consistency::registerShutdownFunction(xcallable('Hoa\Stream\Stream::_Hoa_Stream'));
/**
* Add the `hoa://Library/Stream` node. Should be use to reach/get an entry
* in the stream register.
*/
$protocol = Protocol::getInstance();
$protocol['Library'][] = new _Protocol();