1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* @package Psr7 |
4
|
|
|
* @category modules |
5
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2016, Nazar Mokrynskyi |
7
|
|
|
* @license MIT License, see license.txt |
8
|
|
|
*/ |
9
|
|
|
namespace cs\modules\Psr7; |
10
|
|
|
use |
11
|
|
|
Exception; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Stream wrapper created in order to be used as virtual stream which actually represents PSR7 Psr\Http\Message\StreamInterface, but in form that can be used |
15
|
|
|
* in `fopen()`, this is all useful in order to avoid copying entire stream from PSR7 representation into regular PHP stream |
16
|
|
|
* |
17
|
|
|
* Usage: `fopen('request-psr7-data://', 'r')` |
18
|
|
|
*/ |
19
|
|
|
class Psr7_data_stream { |
20
|
|
|
/** |
21
|
|
|
* PSR7 request body stream is injected here |
22
|
|
|
* |
23
|
|
|
* @var \Psr\Http\Message\StreamInterface |
24
|
|
|
*/ |
25
|
|
|
public static $stream; |
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $stat = []; |
30
|
|
|
|
31
|
|
|
function stream_open ($path, $mode) { |
32
|
|
|
if ($mode != 'r' && $mode != 'rb') { |
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
$stat = &$this->stat; |
36
|
|
|
$stat[0] = $stat['dev'] = 0; |
37
|
|
|
$stat[1] = $stat['ino'] = 0; |
38
|
|
|
$stat[2] = $stat['mode'] = 0; |
39
|
|
|
$stat[3] = $stat['nlink'] = 0; |
40
|
|
|
$stat[4] = $stat['uid'] = 0; |
41
|
|
|
$stat[5] = $stat['gid'] = 0; |
42
|
|
|
$stat[6] = $stat['rdev'] = 0; |
43
|
|
|
$stat[7] = $stat['size'] = static::$stream->getSize(); |
44
|
|
|
$time = time(); |
45
|
|
|
$stat[8] = $stat['atime'] = $time; |
46
|
|
|
$stat[9] = $stat['mtime'] = $time; |
47
|
|
|
$stat[10] = $stat['ctime'] = $time; |
48
|
|
|
$stat[11] = $stat['blksize'] = -1; |
49
|
|
|
$stat[12] = $stat['blocks'] = -1; |
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
/** |
53
|
|
|
* @param int $length |
54
|
|
|
* |
55
|
|
|
* @return false|string |
56
|
|
|
*/ |
57
|
|
|
function stream_read ($length) { |
58
|
|
|
try { |
59
|
|
|
return static::$stream->read($length); |
60
|
|
|
} catch (Exception $e) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
/** |
65
|
|
|
* @return false|int |
66
|
|
|
*/ |
67
|
|
|
function stream_tell () { |
68
|
|
|
try { |
69
|
|
|
return static::$stream->tell(); |
70
|
|
|
} catch (Exception $e) { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
/** |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
function stream_eof () { |
78
|
|
|
return static::$stream->eof(); |
79
|
|
|
} |
80
|
|
|
/** |
81
|
|
|
* @param int $offset |
82
|
|
|
* @param int $whence |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
function stream_seek ($offset, $whence = SEEK_SET) { |
87
|
|
|
try { |
88
|
|
|
return static::$stream->seek($offset, $whence); |
89
|
|
|
} catch (Exception $e) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
function stream_stat () { |
97
|
|
|
return $this->stat; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
/** |
101
|
|
|
* Stream wrapper for PSR7 request interface |
102
|
|
|
*/ |
103
|
|
|
stream_wrapper_register('request-psr7-data', Psr7_data_stream::class); |
104
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.