Skip to content

Commit

Permalink
Adjust UploadedFile - getStream
Browse files Browse the repository at this point in the history
  • Loading branch information
terrylinooo committed Jul 15, 2020
1 parent 53f4b31 commit 83af410
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/Psr7/Response.php
Expand Up @@ -211,7 +211,7 @@ public function getReasonPhrase(): string
*/
protected function assertStatus($code)
{
if (! is_integer($code)) {
if (!is_integer($code)) {
throw new InvalidArgumentException(
sprintf(
'Status code should be an integer value, but "%s" provided.',
Expand All @@ -220,7 +220,7 @@ protected function assertStatus($code)
);
}

if (! ($code > 100 && $code < 599)) {
if (!($code > 100 && $code < 599)) {
throw new InvalidArgumentException(
sprintf(
'Status code should be in a range of 100-599, but "%s" provided.',
Expand All @@ -245,7 +245,7 @@ protected function assertReasonPhrase($reasonPhrase)
return;
}

if (! is_string($reasonPhrase)) {
if (!is_string($reasonPhrase)) {
throw new InvalidArgumentException(
sprintf(
'Reason phrase must be a string, but "%s" provided.',
Expand Down
4 changes: 2 additions & 2 deletions src/Psr7/ServerRequest.php
Expand Up @@ -124,7 +124,7 @@ public function __construct(
// the UploadedFile instance(s) as the $filesParams is given.
$this->uploadedFiles = [];

if (! empty($filesParams)) {
if (!empty($filesParams)) {
$this->uploadedFiles = UploadedFileHelper::uploadedFileSpecsConvert(
UploadedFileHelper::uploadedFileParse($filesParams)
);
Expand Down Expand Up @@ -280,7 +280,7 @@ protected function assertUploadedFiles(array $values): void
foreach ($values as $value) {
if (is_array($value)) {
$this->assertUploadedFiles($value);
} elseif (! ($value instanceof UploadedFileInterface)) {
} elseif (!($value instanceof UploadedFileInterface)) {
throw new InvalidArgumentException(
'Invalid PSR-7 array structure for handling UploadedFile.'
);
Expand Down
12 changes: 6 additions & 6 deletions src/Psr7/Stream.php
Expand Up @@ -150,7 +150,7 @@ public function close(): void
*/
public function detach()
{
if (! $this->isStream()) {
if (!$this->isStream()) {
return null;
}

Expand All @@ -172,7 +172,7 @@ public function detach()
*/
public function getSize()
{
if (! $this->isStream()) {
if (!$this->isStream()) {
return null;
}

Expand Down Expand Up @@ -226,7 +226,7 @@ public function seek($offset, $whence = SEEK_SET): void
{
$this->assertPropertyStream();

if (! $this->seekable) {
if (!$this->seekable) {
throw new RuntimeException(
'Stream is not seekable.'
);
Expand Down Expand Up @@ -349,7 +349,7 @@ public function getMetadata($key = null)
if ($this->isStream()) {
$this->meta = stream_get_meta_data($this->stream);

if (! $key) {
if (!$key) {
return $this->meta;
}

Expand Down Expand Up @@ -390,7 +390,7 @@ public function __toString(): string
*/
protected function assertStream($stream): void
{
if (! is_resource($stream)) {
if (!is_resource($stream)) {
throw new InvalidArgumentException(
sprintf(
'Stream should be a resource, but "%s" provided.',
Expand All @@ -408,7 +408,7 @@ protected function assertStream($stream): void
*/
protected function assertPropertyStream(): void
{
if (! $this->isStream()) {
if (!$this->isStream()) {
throw new RuntimeException(
'Stream does not exist.'
);
Expand Down
28 changes: 21 additions & 7 deletions src/Psr7/UploadedFile.php
Expand Up @@ -14,6 +14,7 @@

use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use Shieldon\Psr7\Stream;
use InvalidArgumentException;
use RuntimeException;

Expand Down Expand Up @@ -55,6 +56,13 @@ class UploadedFile implements UploadedFileInterface
*/
protected $stream;

/**
* Is file copy to stream when first time calling getStream().
*
* @var bool
*/
protected $isFileToStream = false;

/**
* The file size based on the "size" key in the $_FILES array.
*
Expand Down Expand Up @@ -152,7 +160,15 @@ public function getStream(): StreamInterface
);
}

if (! $this->stream) {
if (!$this->isFileToStream && !$this->stream) {
$resource = @fopen($this->file, 'r');
if (is_resource($resource)) {
$this->stream = new Stream($resource);
}
$this->isFileToStream = true;
}

if (!$this->stream) {
throw new RuntimeException(
'No stream is available or can be created.'
);
Expand All @@ -173,7 +189,7 @@ public function moveTo($targetPath): void
);
}

if (! is_writable(dirname($targetPath))) {
if (!is_writable(dirname($targetPath))) {
// Throw exception if the $targetPath specified is invalid.
throw new RuntimeException(
sprintf(
Expand All @@ -188,7 +204,7 @@ public function moveTo($targetPath): void

if ($this->sapi === 'cli') {

if (! rename($this->file, $targetPath)) {
if (!rename($this->file, $targetPath)) {

// @codeCoverageIgnoreStart

Expand Down Expand Up @@ -217,17 +233,15 @@ public function moveTo($targetPath): void
);
}
}
}

// Is a stream...
if ($this->stream instanceof StreamInterface) {
} elseif ($this->stream instanceof StreamInterface) {
$content = $this->stream->getContents();

file_put_contents($targetPath, $content, LOCK_EX);

// @codeCoverageIgnoreStart

if (! file_exists($targetPath)) {
if (!file_exists($targetPath)) {
// Throw exception on any error during the move operation.
throw new RuntimeException(
sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Psr7/Uri.php
Expand Up @@ -142,7 +142,7 @@ public function getAuthority(): string

$authority .= $this->getHost();

if (! is_null($this->getPort())) {
if (!is_null($this->getPort())) {
$authority .= ':' . $this->getPort();
}

Expand Down

0 comments on commit 83af410

Please sign in to comment.