Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit ce1811f

Browse files
committed
Merge branch 'hotfix/67'
Close #67
2 parents c3adcc3 + da8b201 commit ce1811f

File tree

3 files changed

+92
-38
lines changed

3 files changed

+92
-38
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#67](https://github.com/zendframework/zend-diactoros/pull/67) ensures that
22+
the `Stream` class only accepts `stream` resources, not any resource.
2223

2324
## 1.1.1 - 2015-06-25
2425

src/Stream.php

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,7 @@ class Stream implements StreamInterface
3535
*/
3636
public function __construct($stream, $mode = 'r')
3737
{
38-
$this->stream = $stream;
39-
40-
if (is_resource($stream)) {
41-
$this->resource = $stream;
42-
} elseif (is_string($stream)) {
43-
set_error_handler(function ($errno, $errstr) {
44-
throw new InvalidArgumentException(
45-
'Invalid file provided for stream; must be a valid path with valid permissions'
46-
);
47-
}, E_WARNING);
48-
$this->resource = fopen($stream, $mode);
49-
restore_error_handler();
50-
} else {
51-
throw new InvalidArgumentException(
52-
'Invalid stream provided; must be a string stream identifier or resource'
53-
);
54-
}
38+
$this->setStream($stream, $mode);
5539
}
5640

5741
/**
@@ -105,26 +89,7 @@ public function detach()
10589
*/
10690
public function attach($resource, $mode = 'r')
10791
{
108-
$error = null;
109-
if (! is_resource($resource) && is_string($resource)) {
110-
set_error_handler(function ($e) use (&$error) {
111-
$error = $e;
112-
}, E_WARNING);
113-
$resource = fopen($resource, $mode);
114-
restore_error_handler();
115-
}
116-
117-
if ($error) {
118-
throw new InvalidArgumentException('Invalid stream reference provided');
119-
}
120-
121-
if (! is_resource($resource)) {
122-
throw new InvalidArgumentException(
123-
'Invalid stream provided; must be a string stream identifier or resource'
124-
);
125-
}
126-
127-
$this->resource = $resource;
92+
$this->setStream($resource, $mode);
12893
}
12994

13095
/**
@@ -323,4 +288,41 @@ public function getMetadata($key = null)
323288

324289
return $metadata[$key];
325290
}
291+
292+
/**
293+
* Set the internal stream resource.
294+
*
295+
* @param string|resource $stream String stream target or stream resource.
296+
* @param string $mode Resource mode for stream target.
297+
* @throws InvalidArgumentException for invalid streams or resources.
298+
*/
299+
private function setStream($stream, $mode = 'r')
300+
{
301+
$error = null;
302+
$resource = $stream;
303+
304+
if (is_string($stream)) {
305+
set_error_handler(function ($e) use (&$error) {
306+
$error = $e;
307+
}, E_WARNING);
308+
$resource = fopen($stream, $mode);
309+
restore_error_handler();
310+
}
311+
312+
if ($error) {
313+
throw new InvalidArgumentException('Invalid stream reference provided');
314+
}
315+
316+
if (! is_resource($resource) || 'stream' !== get_resource_type($resource)) {
317+
throw new InvalidArgumentException(
318+
'Invalid stream provided; must be a string stream identifier or stream resource'
319+
);
320+
}
321+
322+
if ($stream !== $resource) {
323+
$this->stream = $stream;
324+
}
325+
326+
$this->resource = $resource;
327+
}
326328
}

test/StreamTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,4 +539,55 @@ public function testGetSizeReturnsStreamSize()
539539
$stream = new Stream($resource);
540540
$this->assertEquals($expected['size'], $stream->getSize());
541541
}
542+
543+
/**
544+
* @group 67
545+
*/
546+
public function testRaisesExceptionOnConstructionForNonStreamResources()
547+
{
548+
$resource = $this->getResourceFor67();
549+
if (false === $resource) {
550+
$this->markTestSkipped('No acceptable resource available to test ' . __METHOD__);
551+
}
552+
553+
$this->setExpectedException('InvalidArgumentException', 'stream');
554+
new Stream($resource);
555+
}
556+
557+
/**
558+
* @group 67
559+
*/
560+
public function testRaisesExceptionOnAttachForNonStreamResources()
561+
{
562+
$resource = $this->getResourceFor67();
563+
if (false === $resource) {
564+
$this->markTestSkipped('No acceptable resource available to test ' . __METHOD__);
565+
}
566+
567+
$stream = new Stream(__FILE__);
568+
569+
$this->setExpectedException('InvalidArgumentException', 'stream');
570+
$stream->attach($resource);
571+
}
572+
573+
public function getResourceFor67()
574+
{
575+
if (function_exists('curl_init')) {
576+
return curl_init();
577+
}
578+
579+
if (function_exists('shmop_open')) {
580+
return shmop_open(ftok(__FILE__, 't'), 'c', 0644, 100);
581+
}
582+
583+
if (function_exists('gmp_init')) {
584+
return gmp_init(1);
585+
}
586+
587+
if (function_exists('imagecreate')) {
588+
return imagecreate(200, 200);
589+
}
590+
591+
return false;
592+
}
542593
}

0 commit comments

Comments
 (0)