Skip to content

Commit

Permalink
[10.x] Move Illuminate\Foundation\Application::joinPaths() to `Illu…
Browse files Browse the repository at this point in the history
…minate\Filesystem\join_paths()` (#49433)

* Move `Illuminate\Foundation\Application::joinPaths()` to
`Illuminate\Filesystem\join_paths()`

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* formatting

---------

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
crynobone and taylorotwell authored Dec 20, 2023
1 parent 6c5af17 commit 26281bd
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 3 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,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')];
}
}

0 comments on commit 26281bd

Please sign in to comment.