Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Move Illuminate\Foundation\Application::joinPaths() to Illuminate\Filesystem\join_paths() #49433

Merged
merged 5 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Console/MigrationGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;

use function Illuminate\Filesystem\join_paths;

abstract class MigrationGeneratorCommand extends Command
{
/**
Expand Down Expand Up @@ -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;
}
}
5 changes: 4 additions & 1 deletion src/Illuminate/Filesystem/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
"autoload": {
"psr-4": {
"Illuminate\\Filesystem\\": ""
}
},
"files": [
"functions.php"
]
},
"extra": {
"branch-alias": {
Expand Down
25 changes: 25 additions & 0 deletions src/Illuminate/Filesystem/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Illuminate\Filesystem;

if (! function_exists('Illuminate\Filesystem\join_paths')) {
/**
* Join the given paths together.
*
* @param string|null $basePath
* @param string ...$paths
* @return string
*/
function join_paths($basePath, string ...$paths)
{
foreach ($paths as $index => $path) {
if (empty($path)) {
unset($paths[$index]);
} else {
$paths[$index] = DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR);
}
}

return $basePath.implode('', $paths);
}
}
4 changes: 3 additions & 1 deletion src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;

use function Illuminate\Filesystem\join_paths;

class Application extends Container implements ApplicationContract, CachesConfiguration, CachesRoutes, HttpKernelInterface
{
use Macroable;
Expand Down Expand Up @@ -586,7 +588,7 @@ public function viewPath($path = '')
*/
public function joinPaths($basePath, $path = '')
{
return $basePath.($path != '' ? DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR) : '');
return join_paths($basePath, $path);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/Filesystem/JoinPathsHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Illuminate\Tests\Filesystem;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;
use PHPUnit\Framework\TestCase;

use function Illuminate\Filesystem\join_paths;

class JoinPathsHelperTest extends TestCase
{
#[RequiresOperatingSystem('Linux|DAR')]
#[DataProvider('unixDataProvider')]
public function testItCanMergePathsForUnix(string $expected, string $given)
{
$this->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')];
}
}