From 65e65d335c6c045b5bd65ced06aad818803c44d1 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Tue, 19 Dec 2023 20:43:55 +0800 Subject: [PATCH 1/5] Move `Illuminate\Foundation\Application::joinPaths()` to `Illuminate\Filesystem\join_paths()` Signed-off-by: Mior Muhammad Zaki --- composer.json | 1 + .../Console/MigrationGeneratorCommand.php | 4 +++- src/Illuminate/Filesystem/composer.json | 5 ++++- src/Illuminate/Filesystem/functions.php | 17 +++++++++++++++++ src/Illuminate/Foundation/Application.php | 4 +++- 5 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 src/Illuminate/Filesystem/functions.php diff --git a/composer.json b/composer.json index 9e7705b73db9..9c4e1c019bbe 100644 --- a/composer.json +++ b/composer.json @@ -125,6 +125,7 @@ "files": [ "src/Illuminate/Collections/helpers.php", "src/Illuminate/Events/functions.php", + "src/Illuminate/Filesystem/functions.php", "src/Illuminate/Foundation/helpers.php", "src/Illuminate/Support/helpers.php" ], diff --git a/src/Illuminate/Console/MigrationGeneratorCommand.php b/src/Illuminate/Console/MigrationGeneratorCommand.php index a3a3e002b675..35e28f0dc68b 100644 --- a/src/Illuminate/Console/MigrationGeneratorCommand.php +++ b/src/Illuminate/Console/MigrationGeneratorCommand.php @@ -5,6 +5,8 @@ use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Composer; +use function Illuminate\Filesystem\join_paths; + abstract class MigrationGeneratorCommand extends Command { /** @@ -114,7 +116,7 @@ protected function replaceMigrationPlaceholders($path, $table) protected function migrationExists($table) { return count($this->files->glob( - $this->laravel->joinPaths($this->laravel->databasePath('migrations'), '*_*_*_*_create_'.$table.'_table.php') + join_paths($this->laravel->databasePath('migrations'), '*_*_*_*_create_'.$table.'_table.php') )) !== 0; } } diff --git a/src/Illuminate/Filesystem/composer.json b/src/Illuminate/Filesystem/composer.json index 66925c92b553..2af15697bb47 100644 --- a/src/Illuminate/Filesystem/composer.json +++ b/src/Illuminate/Filesystem/composer.json @@ -24,7 +24,10 @@ "autoload": { "psr-4": { "Illuminate\\Filesystem\\": "" - } + }, + "files": [ + "functions.php" + ] }, "extra": { "branch-alias": { diff --git a/src/Illuminate/Filesystem/functions.php b/src/Illuminate/Filesystem/functions.php new file mode 100644 index 000000000000..13510c454413 --- /dev/null +++ b/src/Illuminate/Filesystem/functions.php @@ -0,0 +1,17 @@ + Date: Wed, 20 Dec 2023 06:28:08 +0800 Subject: [PATCH 2/5] wip Signed-off-by: Mior Muhammad Zaki --- src/Illuminate/Filesystem/functions.php | 8 +++-- tests/Filesystem/JoinPathsHelperTest.php | 38 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tests/Filesystem/JoinPathsHelperTest.php diff --git a/src/Illuminate/Filesystem/functions.php b/src/Illuminate/Filesystem/functions.php index 13510c454413..db4bbdff86a5 100644 --- a/src/Illuminate/Filesystem/functions.php +++ b/src/Illuminate/Filesystem/functions.php @@ -7,11 +7,13 @@ * Join the given paths together. * * @param string $basePath - * @param string $path + * @param string $paths * @return string */ - function join_paths($basePath, $path = '') + function join_paths(string $basePath, string ...$paths) { - return $basePath.($path != '' ? DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR) : ''); + return $basePath.collect($paths)->reject(fn ($path) => empty($path)) + ->transform(fn ($path) => DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR)) + ->join(''); } } diff --git a/tests/Filesystem/JoinPathsHelperTest.php b/tests/Filesystem/JoinPathsHelperTest.php new file mode 100644 index 000000000000..2a4d7cf2e089 --- /dev/null +++ b/tests/Filesystem/JoinPathsHelperTest.php @@ -0,0 +1,38 @@ +assertSame($expected, $given); + } + + public static function unixDataProvider() + { + yield ['app/Http/Kernel.php', join_paths('app', 'Http', 'Kernel.php')]; + yield ['app/Http/Kernel.php', join_paths('app', '', 'Http', 'Kernel.php')]; + } + + #[RequiresOperatingSystem('Windows')] + #[DataProvider('windowsDataProvider')] + public function testItCanMergePathsForWindows(string $expected, string $given) + { + $this->assertSame($expected, $given); + } + + public static function windowsDataProvider() + { + yield ['app\Http\Kernel.php', join_paths('app', 'Http', 'Kernel.php')]; + yield ['app\Http\Kernel.php', join_paths('app', '', 'Http', 'Kernel.php')]; + } +} From 103d2a9cc656d45bf7e1ad814a98a87eb9a43acb Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 20 Dec 2023 06:31:34 +0800 Subject: [PATCH 3/5] wip Signed-off-by: Mior Muhammad Zaki --- src/Illuminate/Filesystem/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Filesystem/functions.php b/src/Illuminate/Filesystem/functions.php index db4bbdff86a5..0214aef5f87c 100644 --- a/src/Illuminate/Filesystem/functions.php +++ b/src/Illuminate/Filesystem/functions.php @@ -7,7 +7,7 @@ * Join the given paths together. * * @param string $basePath - * @param string $paths + * @param string ...$paths * @return string */ function join_paths(string $basePath, string ...$paths) From fbb5e4fe64a7a6b14eb2e3179d39b7b21f90b010 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 20 Dec 2023 06:35:17 +0800 Subject: [PATCH 4/5] wip Signed-off-by: Mior Muhammad Zaki --- src/Illuminate/Filesystem/functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Filesystem/functions.php b/src/Illuminate/Filesystem/functions.php index 0214aef5f87c..c707a6c1657d 100644 --- a/src/Illuminate/Filesystem/functions.php +++ b/src/Illuminate/Filesystem/functions.php @@ -6,11 +6,11 @@ /** * Join the given paths together. * - * @param string $basePath + * @param string|null $basePath * @param string ...$paths * @return string */ - function join_paths(string $basePath, string ...$paths) + function join_paths($basePath, string ...$paths) { return $basePath.collect($paths)->reject(fn ($path) => empty($path)) ->transform(fn ($path) => DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR)) From 04b8357a86cce47a7e953178fb07f56e518afd47 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 20 Dec 2023 08:45:46 -0600 Subject: [PATCH 5/5] formatting --- src/Illuminate/Filesystem/functions.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Filesystem/functions.php b/src/Illuminate/Filesystem/functions.php index c707a6c1657d..47a26fd5eb38 100644 --- a/src/Illuminate/Filesystem/functions.php +++ b/src/Illuminate/Filesystem/functions.php @@ -12,8 +12,14 @@ */ function join_paths($basePath, string ...$paths) { - return $basePath.collect($paths)->reject(fn ($path) => empty($path)) - ->transform(fn ($path) => DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR)) - ->join(''); + foreach ($paths as $index => $path) { + if (empty($path)) { + unset($paths[$index]); + } else { + $paths[$index] = DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR); + } + } + + return $basePath.implode('', $paths); } }