Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Fix Symfony File::move() (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarampampam authored Jun 10, 2021
1 parent beff13e commit 46e67d7
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].

## UNRELEASED

### Fixed

- Symfony uploaded file moving (`FixSymfonyFileMovingListener` was added for this) [#43]

[#43]:https://github.com/spiral/roadrunner-laravel/issues/43

## v5.0.0

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "spiral/roadrunner-laravel",
"type": "library",
"description": "RoadRunner bridge for Laravel applications",
"description": "RoadRunner: Bridge for Laravel applications",
"keywords": [
"laravel",
"bridge",
Expand Down
36 changes: 36 additions & 0 deletions fixes/fix-symfony-file-moving.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Symfony\Component\HttpFoundation\File {

/**
* ***************************************************************************
* ******* WARNING **** WARNING **** WARNING **** WARNING **** WARNING *******.
* ***************************************************************************
* ******* *******
* ******* THIS FUNCTION OVERLOADING IS NECESSARY MEASURE *******
* ******* https://github.com/spiral/roadrunner-laravel/issues/43 *******
* ******* *******
* ***************************************************************************.
*
* Moves an uploaded file to a new location.
*
* @link https://php.net/manual/en/function.move-uploaded-file.php
*
* @param string $from The filename of the uploaded file
* @param string $to The destination of the moved file
*
* @return bool If filename is a valid file, but cannot be moved for some
* reason, no action will occur, and will return false.
*
* @see \Symfony\Component\HttpFoundation\File\UploadedFile::move
*
* @since 4.0.3
* @since 5.0
* @since 7.0
* @since 8.0
*/
function move_uploaded_file(string $from, string $to): bool
{
return \is_file($from) && \rename($from, $to);
}
}
9 changes: 6 additions & 3 deletions fixes/fix-symfony-file-validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@
* ******* *******
* ***************************************************************************.
*
* @see \Symfony\Component\HttpFoundation\File\UploadedFile::isValid
*
* Tells whether the file was uploaded via HTTP POST.
*
* @link https://php.net/manual/en/function.is-uploaded-file.php
*
* @param string $filename The filename being checked
*
* @return bool always true
*
* @see \Symfony\Component\HttpFoundation\File\UploadedFile::isValid
*
* @since 4.0.3
* @since 5.0
* @since 7.0
* @since 8.0
*/
function is_uploaded_file($filename)
function is_uploaded_file(string $filename): bool
{
return true;
}
Expand Down
1 change: 1 addition & 0 deletions src/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static function beforeLoopStarted(): array
{
return [
Listeners\FixSymfonyFileValidationListener::class,
Listeners\FixSymfonyFileMovingListener::class,
Listeners\WarmInstancesListener::class,
];
}
Expand Down
21 changes: 21 additions & 0 deletions src/Listeners/FixSymfonyFileMovingListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunnerLaravel\Listeners;

/**
* @link https://github.com/spiral/roadrunner-laravel/issues/43
*/
class FixSymfonyFileMovingListener implements ListenerInterface
{
/**
* {@inheritdoc}
*/
public function handle($event): void
{
if (!\function_exists('\\Symfony\\Component\\HttpFoundation\\File\\move_uploaded_file')) {
require __DIR__ . '/../../fixes/fix-symfony-file-moving.php';
}
}
}
49 changes: 49 additions & 0 deletions tests/Unit/Listeners/FixSymfonyFileMovingListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners;

use Illuminate\Support\Str;
use Spiral\RoadRunnerLaravel\Listeners\FixSymfonyFileMovingListener;

/**
* @covers \Spiral\RoadRunnerLaravel\Listeners\FixSymfonyFileMovingListener
*/
class FixSymfonyFileMovingListenerTest extends AbstractListenerTestCase
{
/**
* {@inheritdoc}
*/
public function testHandle(): void
{
$function_location = '\\Symfony\\Component\\HttpFoundation\\File\\move_uploaded_file';

$this->assertFalse(\function_exists($function_location));
$this->assertFalse(\is_uploaded_file('foo'));

$this->listenerFactory()->handle(new \stdClass());

$this->assertTrue(\function_exists($function_location));

$tmp_dir = $this->createTemporaryDirectory();
$old_file_path = $tmp_dir . DIRECTORY_SEPARATOR . Str::random();
$new_file_path = $tmp_dir . DIRECTORY_SEPARATOR . Str::random();
\file_put_contents($old_file_path, '');
$this->assertFileExists($old_file_path);
$this->assertTrue($function_location($old_file_path, $new_file_path));
$this->assertFileExists($new_file_path);
$this->assertFileNotExists($old_file_path);
$rnd_file_path1 = $tmp_dir . DIRECTORY_SEPARATOR . Str::random();
$rnd_file_path2 = $tmp_dir . DIRECTORY_SEPARATOR . Str::random();
$this->assertFalse($function_location($rnd_file_path1, $rnd_file_path2));
}

/**
* @return FixSymfonyFileMovingListener
*/
protected function listenerFactory(): FixSymfonyFileMovingListener
{
return new FixSymfonyFileMovingListener();
}
}

0 comments on commit 46e67d7

Please sign in to comment.