Skip to content

Commit 86dbc7b

Browse files
authored
[7.x] Add Orchestra\Testbench\transform_realpath_to_relative function (#276)
* wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> --------- Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent cdcc614 commit 86dbc7b

11 files changed

+113
-83
lines changed

src/Foundation/Console/Actions/Action.php

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,27 @@
22

33
namespace Orchestra\Testbench\Foundation\Console\Actions;
44

5-
use function Orchestra\Testbench\package_path;
5+
use function Orchestra\Testbench\transform_realpath_to_relative;
66

77
/**
88
* @api
99
*/
1010
abstract class Action
1111
{
12-
/**
13-
* Working path for the action.
14-
*
15-
* @var string|null
16-
*/
17-
public $workingPath;
18-
1912
/**
2013
* Normalise file location.
2114
*
2215
* @param string $path
2316
* @return string
17+
*
18+
* @deprecated
19+
*
20+
* @codeCoverageIgnore
2421
*/
2522
protected function pathLocation(string $path): string
2623
{
27-
$packagePath = package_path();
28-
29-
if (! \is_null($this->workingPath)) {
30-
$path = str_replace(rtrim($this->workingPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR, '', $path);
31-
32-
$prefix = match ($this->workingPath) {
33-
base_path() => '@laravel',
34-
$packagePath => '.',
35-
default => '@'
36-
};
37-
38-
return implode('/', [$prefix, ltrim($path, '/')]);
39-
}
40-
41-
if (str_starts_with($path, $packagePath)) {
42-
return \sprintf('./%s', ltrim(str_replace($packagePath, '', $path), '/'));
43-
}
44-
45-
return $path;
24+
return transform_realpath_to_relative(
25+
$path, property_exists($this, 'workingPath') ? $this->workingPath : null
26+
);
4627
}
4728
}

src/Foundation/Console/Actions/DeleteDirectories.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Illuminate\Filesystem\Filesystem;
77
use Illuminate\Support\LazyCollection;
88

9+
use function Orchestra\Testbench\transform_realpath_to_relative;
10+
911
/**
1012
* @api
1113
*/
@@ -21,10 +23,8 @@ class DeleteDirectories extends Action
2123
public function __construct(
2224
public Filesystem $filesystem,
2325
public ?ComponentsFactory $components = null,
24-
?string $workingPath = null
25-
) {
26-
$this->workingPath = $workingPath;
27-
}
26+
public ?string $workingPath = null
27+
) {}
2828

2929
/**
3030
* Handle the action.
@@ -38,7 +38,7 @@ public function handle(iterable $directories): void
3838
->each(function ($directory) {
3939
if (! $this->filesystem->isDirectory($directory)) {
4040
$this->components?->twoColumnDetail(
41-
\sprintf('Directory [%s] doesn\'t exists', $this->pathLocation($directory)),
41+
\sprintf('Directory [%s] doesn\'t exists', transform_realpath_to_relative($directory, $this->workingPath)),
4242
'<fg=yellow;options=bold>SKIPPED</>'
4343
);
4444

@@ -48,7 +48,7 @@ public function handle(iterable $directories): void
4848
$this->filesystem->deleteDirectory($directory);
4949

5050
$this->components?->task(
51-
\sprintf('Directory [%s] has been deleted', $this->pathLocation($directory))
51+
\sprintf('Directory [%s] has been deleted', transform_realpath_to_relative($directory, $this->workingPath))
5252
);
5353
});
5454
}

src/Foundation/Console/Actions/DeleteFiles.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Illuminate\Filesystem\Filesystem;
77
use Illuminate\Support\LazyCollection;
88

9+
use function Orchestra\Testbench\transform_realpath_to_relative;
10+
911
/**
1012
* @api
1113
*/
@@ -21,10 +23,8 @@ class DeleteFiles extends Action
2123
public function __construct(
2224
public Filesystem $filesystem,
2325
public ?ComponentsFactory $components = null,
24-
?string $workingPath = null
25-
) {
26-
$this->workingPath = $workingPath;
27-
}
26+
public ?string $workingPath = null
27+
) {}
2828

2929
/**
3030
* Handle the action.
@@ -39,7 +39,7 @@ public function handle(iterable $files): void
3939
->each(function ($file) {
4040
if (! $this->filesystem->exists($file)) {
4141
$this->components?->twoColumnDetail(
42-
\sprintf('File [%s] doesn\'t exists', $this->pathLocation($file)),
42+
\sprintf('File [%s] doesn\'t exists', transform_realpath_to_relative($file, $this->workingPath)),
4343
'<fg=yellow;options=bold>SKIPPED</>'
4444
);
4545

@@ -49,7 +49,7 @@ public function handle(iterable $files): void
4949
$this->filesystem->delete($file);
5050

5151
$this->components?->task(
52-
\sprintf('File [%s] has been deleted', $this->pathLocation($file))
52+
\sprintf('File [%s] has been deleted', transform_realpath_to_relative($file, $this->workingPath))
5353
);
5454
});
5555
}

src/Foundation/Console/Actions/EnsureDirectoryExists.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\LazyCollection;
88

99
use function Orchestra\Testbench\join_paths;
10+
use function Orchestra\Testbench\transform_realpath_to_relative;
1011

1112
/**
1213
* @api
@@ -23,10 +24,8 @@ class EnsureDirectoryExists extends Action
2324
public function __construct(
2425
public Filesystem $filesystem,
2526
public ?ComponentsFactory $components = null,
26-
?string $workingPath = null
27-
) {
28-
$this->workingPath = $workingPath;
29-
}
27+
public ?string $workingPath = null
28+
) {}
3029

3130
/**
3231
* Handle the action.
@@ -40,7 +39,7 @@ public function handle(iterable $directories): void
4039
->each(function ($directory) {
4140
if ($this->filesystem->isDirectory($directory)) {
4241
$this->components?->twoColumnDetail(
43-
\sprintf('Directory [%s] already exists', $this->pathLocation($directory)),
42+
\sprintf('Directory [%s] already exists', transform_realpath_to_relative($directory, $this->workingPath)),
4443
'<fg=yellow;options=bold>SKIPPED</>'
4544
);
4645

@@ -50,7 +49,7 @@ public function handle(iterable $directories): void
5049
$this->filesystem->ensureDirectoryExists($directory, 0755, true);
5150
$this->filesystem->copy((string) realpath(join_paths(__DIR__, 'stubs', '.gitkeep')), join_paths($directory, '.gitkeep'));
5251

53-
$this->components?->task(\sprintf('Prepare [%s] directory', $this->pathLocation($directory)));
52+
$this->components?->task(\sprintf('Prepare [%s] directory', transform_realpath_to_relative($directory, $this->workingPath)));
5453
});
5554
}
5655
}

src/Foundation/Console/Actions/GeneratesFile.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Illuminate\Console\View\Components\Factory as ComponentsFactory;
66
use Illuminate\Filesystem\Filesystem;
77

8+
use function Orchestra\Testbench\transform_realpath_to_relative;
9+
810
/**
911
* @api
1012
*/
@@ -22,10 +24,8 @@ public function __construct(
2224
public Filesystem $filesystem,
2325
public ?ComponentsFactory $components = null,
2426
public bool $force = false,
25-
?string $workingPath = null
26-
) {
27-
$this->workingPath = $workingPath;
28-
}
27+
public ?string $workingPath = null
28+
) {}
2929

3030
/**
3131
* Handle the action.
@@ -42,7 +42,7 @@ public function handle($from, $to): void
4242

4343
if (! $this->filesystem->exists($from)) {
4444
$this->components?->twoColumnDetail(
45-
\sprintf('Source file [%s] doesn\'t exists', $this->pathLocation($from)),
45+
\sprintf('Source file [%s] doesn\'t exists', transform_realpath_to_relative($from, $this->workingPath)),
4646
'<fg=yellow;options=bold>SKIPPED</>'
4747
);
4848

@@ -51,7 +51,7 @@ public function handle($from, $to): void
5151

5252
if (! $this->force && $this->filesystem->exists($to)) {
5353
$this->components?->twoColumnDetail(
54-
\sprintf('File [%s] already exists', $this->pathLocation($to)),
54+
\sprintf('File [%s] already exists', transform_realpath_to_relative($to, $this->workingPath)),
5555
'<fg=yellow;options=bold>SKIPPED</>'
5656
);
5757

@@ -67,7 +67,7 @@ public function handle($from, $to): void
6767
}
6868

6969
$this->components?->task(
70-
\sprintf('File [%s] generated', $this->pathLocation($to))
70+
\sprintf('File [%s] generated', transform_realpath_to_relative($to, $this->workingPath))
7171
);
7272
}
7373
}

src/Foundation/Console/CreateSqliteDbCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public function handle(Filesystem $filesystem)
4747
filesystem: $filesystem,
4848
components: $this->components,
4949
force: $force,
50-
workingPath: $workingPath,
5150
))->handle($from, $to);
5251

5352
return Command::SUCCESS;

src/Foundation/Console/DropSqliteDbCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public function handle(Filesystem $filesystem)
3535
(new Actions\DeleteFiles(
3636
filesystem: $filesystem,
3737
components: $this->components,
38-
workingPath: $workingPath,
3938
))->handle(
4039
match ($this->option('all')) {
4140
true => [...$filesystem->glob(join_paths($databasePath, '*.sqlite'))],

src/Foundation/Console/VendorPublishCommand.php

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Foundation\Console\VendorPublishCommand as Command;
66

7-
use function Orchestra\Testbench\package_path;
7+
use function Orchestra\Testbench\transform_realpath_to_relative;
88

99
/**
1010
* @codeCoverageIgnore
@@ -15,37 +15,19 @@ class VendorPublishCommand extends Command
1515
#[\Override]
1616
protected function status($from, $to, $type)
1717
{
18-
$laravelPath = base_path();
19-
$packagePath = package_path();
20-
21-
$pathLocation = function ($path) use ($laravelPath, $packagePath) {
22-
$path = (string) realpath($path);
23-
18+
$format = function ($path) use ($type) {
2419
return match (true) {
25-
str_starts_with($path, $laravelPath) => str_replace("{$laravelPath}/", '@laravel/', $path),
26-
str_starts_with($path, $packagePath) => str_replace("{$packagePath}/", './', $path),
27-
default => $path,
20+
$type === 'directory' && is_link($path) => $path,
21+
$this->files->exists($path) => $path,
22+
default => (string) realpath($path),
2823
};
2924
};
3025

31-
$fromLocation = $pathLocation($from);
32-
$toLocation = $pathLocation($to);
33-
34-
if (
35-
$type === 'directory' &&
36-
$fromLocation === $toLocation &&
37-
is_link($to)
38-
) {
39-
$this->components->task('Synced directory');
40-
41-
return;
42-
}
43-
4426
$this->components->task(\sprintf(
4527
'Copying %s [%s] to [%s]',
4628
$type,
47-
$fromLocation,
48-
$toLocation,
29+
transform_realpath_to_relative($format($from)),
30+
transform_realpath_to_relative($format($to)),
4931
));
5032
}
5133
}

src/functions.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,36 @@ function refresh_router_lookups(Router $router): void
219219
$router->getRoutes()->refreshNameLookups();
220220
}
221221

222+
/**
223+
* Transform realpath to alias path.
224+
*
225+
* @api
226+
*
227+
* @param string $path
228+
* @param string|null $workingPath
229+
* @return string
230+
*/
231+
function transform_realpath_to_relative(string $path, ?string $workingPath = null, string $prefix = ''): string
232+
{
233+
$separator = DIRECTORY_SEPARATOR;
234+
235+
if (! \is_null($workingPath)) {
236+
return str_replace(rtrim($workingPath, $separator).$separator, $prefix.$separator, $path);
237+
}
238+
239+
$laravelPath = base_path();
240+
$workbenchPath = workbench_path();
241+
$packagePath = package_path();
242+
243+
return match (true) {
244+
str_starts_with($path, $laravelPath) => str_replace($laravelPath.$separator, '@laravel'.$separator, $path),
245+
str_starts_with($path, $workbenchPath) => str_replace($workbenchPath.$separator, '@workbench'.$separator, $path),
246+
str_starts_with($path, $packagePath) => str_replace($packagePath.$separator, '.'.$separator, $path),
247+
! empty($prefix) => implode($separator, [$prefix, ltrim($path, $separator)]),
248+
default => $path,
249+
};
250+
}
251+
222252
/**
223253
* Transform relative path.
224254
*
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Orchestra\Testbench\Tests\Helpers;
4+
5+
use Orchestra\Testbench\TestCase;
6+
7+
use function Orchestra\Testbench\default_skeleton_path;
8+
use function Orchestra\Testbench\join_paths;
9+
use function Orchestra\Testbench\package_path;
10+
use function Orchestra\Testbench\transform_realpath_to_relative;
11+
12+
class TransformRealpathToRelativeTest extends TestCase
13+
{
14+
/** @test */
15+
public function it_can_use_transform_realpath_to_relative()
16+
{
17+
$this->assertSame('Testbench.php', transform_realpath_to_relative('Testbench.php'));
18+
19+
$this->assertSame(
20+
join_paths('.', 'src', 'TestCase.php'),
21+
transform_realpath_to_relative(package_path('src', 'TestCase.php'))
22+
);
23+
24+
$this->assertSame(
25+
join_paths('@laravel', 'composer.json'),
26+
transform_realpath_to_relative(default_skeleton_path('composer.json'))
27+
);
28+
29+
$this->assertSame(
30+
join_paths('@workbench', 'app', 'Providers', 'WorkbenchServiceProvider.php'),
31+
transform_realpath_to_relative(package_path('workbench', 'app', 'Providers', 'WorkbenchServiceProvider.php'))
32+
);
33+
}
34+
35+
/** @test */
36+
public function it_can_use_transform_realpath_to_relative_using_custom_working_path()
37+
{
38+
$this->assertSame(
39+
join_paths('@tests', 'Helpers', 'TransformRealpathToRelativeTest.php'),
40+
transform_realpath_to_relative(__FILE__, package_path('tests'), '@tests')
41+
);
42+
}
43+
}

tests/Helpers/TransformRelativePathTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ class TransformRelativePathTest extends TestCase
1111
/** @test */
1212
public function it_can_use_transform_relative_path()
1313
{
14-
$this->assertSame(
15-
realpath(__DIR__.DIRECTORY_SEPARATOR.'TransformRelativePathTest.php'),
16-
transform_relative_path('./TransformRelativePathTest.php', realpath(__DIR__))
17-
);
14+
$this->assertSame(__FILE__, transform_relative_path('./TransformRelativePathTest.php', __DIR__));
1815
}
1916
}

0 commit comments

Comments
 (0)