8
8
use Phplrt \Contracts \Source \ReadableInterface ;
9
9
use Phplrt \Contracts \Source \SourceExceptionInterface ;
10
10
use Phplrt \Contracts \Source \SourceFactoryInterface ;
11
+ use Psr \Http \Message \StreamInterface ;
11
12
12
13
trait SourceFactoryTrait
13
14
{
@@ -34,8 +35,12 @@ public static function getSourceFactory(): SourceFactoryInterface
34
35
*
35
36
* @psalm-suppress NoValue : Allow any value
36
37
*/
37
- public static function new (mixed $ source ): ReadableInterface
38
+ public static function new ($ source ): ReadableInterface
38
39
{
40
+ if ($ source instanceof StreamInterface) {
41
+ return static ::fromPsrStream ($ source );
42
+ }
43
+
39
44
$ factory = self ::getSourceFactory ();
40
45
41
46
return $ factory ->create ($ source );
@@ -46,6 +51,7 @@ public static function new(mixed $source): ReadableInterface
46
51
* @param non-empty-string|null $pathname
47
52
*
48
53
* @return ($pathname is null ? ReadableInterface : FileInterface)
54
+ * @throws SourceExceptionInterface
49
55
*/
50
56
public static function empty (?string $ pathname = null ): ReadableInterface
51
57
{
@@ -57,6 +63,7 @@ public static function empty(?string $pathname = null): ReadableInterface
57
63
* @param non-empty-string|null $pathname
58
64
*
59
65
* @return ($pathname is null ? ReadableInterface : FileInterface)
66
+ * @throws SourceExceptionInterface
60
67
*/
61
68
public static function fromSources (string $ sources , ?string $ pathname = null ): ReadableInterface
62
69
{
@@ -97,14 +104,32 @@ public static function fromPathname(string $pathname): FileInterface
97
104
return new File ($ pathname );
98
105
}
99
106
107
+ /**
108
+ * @param non-empty-string|null $pathname
109
+ *
110
+ * @return ($pathname is null ? ReadableInterface : FileInterface)
111
+ * @throws SourceExceptionInterface
112
+ *
113
+ * @deprecated since phplrt 3.4 and will be removed in 4.0, use {@see fromResource()} instead.
114
+ */
115
+ public static function fromPsrStream (StreamInterface $ stream , ?string $ pathname = null ): ReadableInterface
116
+ {
117
+ trigger_deprecation ('phplrt/source ' , '3.4 ' , <<<'MSG'
118
+ Using "%s::fromPsrStream($stream)" with %s argument is deprecated,
119
+ use "%1$s::fromResource($stream->detach())" instead.
120
+ MSG, static ::class, \get_class ($ stream ));
121
+
122
+ return static ::fromResource ($ stream ->detach (), $ pathname );
123
+ }
124
+
100
125
/**
101
126
* @param resource $resource
102
127
* @param non-empty-string|null $pathname
103
128
*
104
129
* @return ($pathname is null ? ReadableInterface : FileInterface)
105
130
* @throws SourceExceptionInterface
106
131
*/
107
- public static function fromResource (mixed $ resource , ?string $ pathname = null ): ReadableInterface
132
+ public static function fromResource ($ resource , ?string $ pathname = null ): ReadableInterface
108
133
{
109
134
$ factory = static ::getSourceFactory ();
110
135
0 commit comments