Skip to content

Commit 46e67d7

Browse files
authored
Fix Symfony File::move() (#46)
1 parent beff13e commit 46e67d7

File tree

7 files changed

+122
-4
lines changed

7 files changed

+122
-4
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44

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

7+
## UNRELEASED
8+
9+
### Fixed
10+
11+
- Symfony uploaded file moving (`FixSymfonyFileMovingListener` was added for this) [#43]
12+
13+
[#43]:https://github.com/spiral/roadrunner-laravel/issues/43
14+
715
## v5.0.0
816

917
### Added

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "spiral/roadrunner-laravel",
33
"type": "library",
4-
"description": "RoadRunner bridge for Laravel applications",
4+
"description": "RoadRunner: Bridge for Laravel applications",
55
"keywords": [
66
"laravel",
77
"bridge",

fixes/fix-symfony-file-moving.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpFoundation\File {
4+
5+
/**
6+
* ***************************************************************************
7+
* ******* WARNING **** WARNING **** WARNING **** WARNING **** WARNING *******.
8+
* ***************************************************************************
9+
* ******* *******
10+
* ******* THIS FUNCTION OVERLOADING IS NECESSARY MEASURE *******
11+
* ******* https://github.com/spiral/roadrunner-laravel/issues/43 *******
12+
* ******* *******
13+
* ***************************************************************************.
14+
*
15+
* Moves an uploaded file to a new location.
16+
*
17+
* @link https://php.net/manual/en/function.move-uploaded-file.php
18+
*
19+
* @param string $from The filename of the uploaded file
20+
* @param string $to The destination of the moved file
21+
*
22+
* @return bool If filename is a valid file, but cannot be moved for some
23+
* reason, no action will occur, and will return false.
24+
*
25+
* @see \Symfony\Component\HttpFoundation\File\UploadedFile::move
26+
*
27+
* @since 4.0.3
28+
* @since 5.0
29+
* @since 7.0
30+
* @since 8.0
31+
*/
32+
function move_uploaded_file(string $from, string $to): bool
33+
{
34+
return \is_file($from) && \rename($from, $to);
35+
}
36+
}

fixes/fix-symfony-file-validation.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@
1313
* ******* *******
1414
* ***************************************************************************.
1515
*
16-
* @see \Symfony\Component\HttpFoundation\File\UploadedFile::isValid
17-
*
1816
* Tells whether the file was uploaded via HTTP POST.
17+
*
1918
* @link https://php.net/manual/en/function.is-uploaded-file.php
2019
*
2120
* @param string $filename The filename being checked
2221
*
2322
* @return bool always true
2423
*
24+
* @see \Symfony\Component\HttpFoundation\File\UploadedFile::isValid
25+
*
2526
* @since 4.0.3
2627
* @since 5.0
28+
* @since 7.0
29+
* @since 8.0
2730
*/
28-
function is_uploaded_file($filename)
31+
function is_uploaded_file(string $filename): bool
2932
{
3033
return true;
3134
}

src/Defaults.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static function beforeLoopStarted(): array
1515
{
1616
return [
1717
Listeners\FixSymfonyFileValidationListener::class,
18+
Listeners\FixSymfonyFileMovingListener::class,
1819
Listeners\WarmInstancesListener::class,
1920
];
2021
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunnerLaravel\Listeners;
6+
7+
/**
8+
* @link https://github.com/spiral/roadrunner-laravel/issues/43
9+
*/
10+
class FixSymfonyFileMovingListener implements ListenerInterface
11+
{
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public function handle($event): void
16+
{
17+
if (!\function_exists('\\Symfony\\Component\\HttpFoundation\\File\\move_uploaded_file')) {
18+
require __DIR__ . '/../../fixes/fix-symfony-file-moving.php';
19+
}
20+
}
21+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners;
6+
7+
use Illuminate\Support\Str;
8+
use Spiral\RoadRunnerLaravel\Listeners\FixSymfonyFileMovingListener;
9+
10+
/**
11+
* @covers \Spiral\RoadRunnerLaravel\Listeners\FixSymfonyFileMovingListener
12+
*/
13+
class FixSymfonyFileMovingListenerTest extends AbstractListenerTestCase
14+
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
public function testHandle(): void
19+
{
20+
$function_location = '\\Symfony\\Component\\HttpFoundation\\File\\move_uploaded_file';
21+
22+
$this->assertFalse(\function_exists($function_location));
23+
$this->assertFalse(\is_uploaded_file('foo'));
24+
25+
$this->listenerFactory()->handle(new \stdClass());
26+
27+
$this->assertTrue(\function_exists($function_location));
28+
29+
$tmp_dir = $this->createTemporaryDirectory();
30+
$old_file_path = $tmp_dir . DIRECTORY_SEPARATOR . Str::random();
31+
$new_file_path = $tmp_dir . DIRECTORY_SEPARATOR . Str::random();
32+
\file_put_contents($old_file_path, '');
33+
$this->assertFileExists($old_file_path);
34+
$this->assertTrue($function_location($old_file_path, $new_file_path));
35+
$this->assertFileExists($new_file_path);
36+
$this->assertFileNotExists($old_file_path);
37+
$rnd_file_path1 = $tmp_dir . DIRECTORY_SEPARATOR . Str::random();
38+
$rnd_file_path2 = $tmp_dir . DIRECTORY_SEPARATOR . Str::random();
39+
$this->assertFalse($function_location($rnd_file_path1, $rnd_file_path2));
40+
}
41+
42+
/**
43+
* @return FixSymfonyFileMovingListener
44+
*/
45+
protected function listenerFactory(): FixSymfonyFileMovingListener
46+
{
47+
return new FixSymfonyFileMovingListener();
48+
}
49+
}

0 commit comments

Comments
 (0)