Skip to content

Commit

Permalink
Fix failing file mode validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tuupola committed Aug 25, 2020
1 parent 1a4ba89 commit e367318
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/StreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

use InvalidArgumentException;
use RuntimeException;

class StreamFactory implements StreamFactoryInterface
{
/**
Expand All @@ -65,11 +68,23 @@ public function createStream(string $content = ""): StreamInterface
*/
public function createStreamFromFile(string $filename, string $mode = "r"): StreamInterface
{
if ("" === $mode || false === in_array($mode[0], ["r", "w", "a", "x", "c"])) {
throw new InvalidArgumentException(
sprintf("The mode %s is invalid.", $mode)
);
}

if (class_exists(SlimPsr7StreamFactory::class)) {
return (new SlimPsr7StreamFactory)->createStreamFromFile($filename, $mode);
}

$resource = fopen($filename, $mode);
$resource = @fopen($filename, $mode);
if (false === $resource) {
throw new RuntimeException(
sprintf("The file %s cannot be opened.", $filename)
);
}

return $this->createStreamFromResource($resource);
}

Expand Down Expand Up @@ -98,6 +113,6 @@ public function createStreamFromResource($resource): StreamInterface
return new GuzzleStream($resource);
}

throw new \RuntimeException("No PSR-7 implementation available");
throw new RuntimeException("No PSR-7 implementation available");
}
}

0 comments on commit e367318

Please sign in to comment.