Skip to content

Commit

Permalink
Merge branch '7.x' into 8.x
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Dec 12, 2024
2 parents 9fd8eef + 6dfb707 commit 276b1ff
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 84 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"mockery/mockery": "^1.5.1",
"phpstan/phpstan": "^2.0",
"phpunit/phpunit": "^10.1",
"spatie/laravel-ray": "^1.32.4",
"spatie/laravel-ray": "^1.39",
"symfony/process": "^6.2",
"symfony/yaml": "^6.2",
"vlucas/phpdotenv": "^5.4.1"
Expand Down
35 changes: 8 additions & 27 deletions src/Foundation/Console/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,27 @@

namespace Orchestra\Testbench\Foundation\Console\Actions;

use function Orchestra\Testbench\package_path;
use function Orchestra\Testbench\transform_realpath_to_relative;

/**
* @api
*/
abstract class Action
{
/**
* Working path for the action.
*
* @var string|null
*/
public $workingPath;

/**
* Normalise file location.
*
* @param string $path
* @return string
*
* @deprecated
*
* @codeCoverageIgnore
*/
protected function pathLocation(string $path): string
{
$packagePath = package_path();

if (! \is_null($this->workingPath)) {
$path = str_replace(rtrim($this->workingPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR, '', $path);

$prefix = match ($this->workingPath) {
app()->basePath() => '@laravel',
$packagePath => '.',
default => '@'
};

return implode('/', [$prefix, ltrim($path, '/')]);
}

if (str_starts_with($path, $packagePath)) {
return \sprintf('./%s', ltrim(str_replace($packagePath, '', $path), '/'));
}

return $path;
return transform_realpath_to_relative(
$path, property_exists($this, 'workingPath') ? $this->workingPath : null
);
}
}
11 changes: 5 additions & 6 deletions src/Foundation/Console/Actions/DeleteDirectories.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\LazyCollection;

use function Laravel\Prompts\confirm;
use function Orchestra\Testbench\transform_realpath_to_relative;

/**
* @api
Expand All @@ -24,11 +25,9 @@ class DeleteDirectories extends Action
public function __construct(
public Filesystem $filesystem,
public ?ComponentsFactory $components = null,
?string $workingPath = null,
public ?string $workingPath = null,
public bool $confirmation = false
) {
$this->workingPath = $workingPath;
}
) {}

/**
* Handle the action.
Expand All @@ -40,7 +39,7 @@ public function handle(iterable $directories): void
{
LazyCollection::make($directories)
->each(function ($directory) {
$location = $this->pathLocation($directory);
$location = transform_realpath_to_relative($directory, $this->workingPath);

if (! $this->filesystem->isDirectory($directory)) {
$this->components?->twoColumnDetail(
Expand All @@ -58,7 +57,7 @@ public function handle(iterable $directories): void
$this->filesystem->deleteDirectory($directory);

$this->components?->task(
\sprintf('Directory [%s] has been deleted', $this->pathLocation($directory))
\sprintf('Directory [%s] has been deleted', $location)
);
});
}
Expand Down
9 changes: 4 additions & 5 deletions src/Foundation/Console/Actions/DeleteFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\LazyCollection;

use function Laravel\Prompts\confirm;
use function Orchestra\Testbench\transform_realpath_to_relative;

/**
* @api
Expand All @@ -24,11 +25,9 @@ class DeleteFiles extends Action
public function __construct(
public Filesystem $filesystem,
public ?ComponentsFactory $components = null,
?string $workingPath = null,
public ?string $workingPath = null,
public bool $confirmation = false
) {
$this->workingPath = $workingPath;
}
) {}

/**
* Handle the action.
Expand All @@ -41,7 +40,7 @@ public function handle(iterable $files): void
LazyCollection::make($files)
->reject(static fn ($file) => str_ends_with($file, '.gitkeep') || str_ends_with($file, '.gitignore'))
->each(function ($file) {
$location = $this->pathLocation($file);
$location = transform_realpath_to_relative($file, $this->workingPath);

if (! $this->filesystem->exists($file)) {
$this->components?->twoColumnDetail(
Expand Down
9 changes: 4 additions & 5 deletions src/Foundation/Console/Actions/EnsureDirectoryExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use function Laravel\Prompts\confirm;
use function Orchestra\Testbench\join_paths;
use function Orchestra\Testbench\transform_realpath_to_relative;

/**
* @api
Expand All @@ -25,11 +26,9 @@ class EnsureDirectoryExists extends Action
public function __construct(
public Filesystem $filesystem,
public ?ComponentsFactory $components = null,
?string $workingPath = null,
public ?string $workingPath = null,
public bool $confirmation = false
) {
$this->workingPath = $workingPath;
}
) {}

/**
* Handle the action.
Expand All @@ -41,7 +40,7 @@ public function handle(iterable $directories): void
{
LazyCollection::make($directories)
->each(function ($directory) {
$location = $this->pathLocation($directory);
$location = transform_realpath_to_relative($directory, $this->workingPath);

if ($this->filesystem->isDirectory($directory)) {
$this->components?->twoColumnDetail(
Expand Down
11 changes: 5 additions & 6 deletions src/Foundation/Console/Actions/GeneratesFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use function Laravel\Prompts\confirm;
use function Orchestra\Testbench\join_paths;
use function Orchestra\Testbench\transform_realpath_to_relative;

/**
* @api
Expand All @@ -26,11 +27,9 @@ public function __construct(
public Filesystem $filesystem,
public ?ComponentsFactory $components = null,
public bool $force = false,
?string $workingPath = null,
public ?string $workingPath = null,
public bool $confirmation = false
) {
$this->workingPath = $workingPath;
}
) {}

/**
* Handle the action.
Expand All @@ -47,14 +46,14 @@ public function handle($from, $to): void

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

return;
}

$location = $this->pathLocation($to);
$location = transform_realpath_to_relative($to, $this->workingPath);

if (! $this->force && $this->filesystem->exists($to)) {
$this->components?->twoColumnDetail(
Expand Down
1 change: 0 additions & 1 deletion src/Foundation/Console/CreateSqliteDbCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function handle(Filesystem $filesystem)
filesystem: $filesystem,
components: $this->components,
force: $force,
workingPath: $this->laravel->basePath(),
))->handle($from, $to);

return Command::SUCCESS;
Expand Down
1 change: 0 additions & 1 deletion src/Foundation/Console/DropSqliteDbCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function handle(Filesystem $filesystem)
(new Actions\DeleteFiles(
filesystem: $filesystem,
components: $this->components,
workingPath: $this->laravel->basePath(),
))->handle(
match ($this->option('all')) {
true => [...$filesystem->glob(join_paths($databasePath, '*.sqlite'))],
Expand Down
2 changes: 1 addition & 1 deletion src/Foundation/Console/SyncSkeletonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function configure()
{
parent::configure();

TerminatingConsole::purge();
TerminatingConsole::flush();
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Foundation/Console/TerminatingConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Illuminate\Support\Collection;

/**
* @internal
*/
final class TerminatingConsole
{
/**
Expand Down Expand Up @@ -50,15 +53,15 @@ public static function handle(): void
\call_user_func($callback);
});

self::purge();
self::flush();
}

/**
* Purge terminating console callbacks.
*
* @return void
*/
public static function purge(): void
public static function flush(): void
{
self::$beforeTerminatingCallbacks = [];
}
Expand Down
32 changes: 7 additions & 25 deletions src/Foundation/Console/VendorPublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Foundation\Console\VendorPublishCommand as Command;

use function Orchestra\Testbench\package_path;
use function Orchestra\Testbench\transform_realpath_to_relative;

/**
* @codeCoverageIgnore
Expand All @@ -15,37 +15,19 @@ class VendorPublishCommand extends Command
#[\Override]
protected function status($from, $to, $type)
{
$laravelPath = base_path();
$packagePath = package_path();

$pathLocation = function ($path) use ($laravelPath, $packagePath) {
$path = (string) realpath($path);

$format = function ($path) use ($type) {
return match (true) {
str_starts_with($path, $laravelPath) => str_replace("{$laravelPath}/", '@laravel/', $path),
str_starts_with($path, $packagePath) => str_replace("{$packagePath}/", './', $path),
default => $path,
$type === 'directory' && is_link($path) => $path,
$this->files->exists($path) => $path,
default => (string) realpath($path),
};
};

$fromLocation = $pathLocation($from);
$toLocation = $pathLocation($to);

if (
$type === 'directory' &&
$fromLocation === $toLocation &&
is_link($to)
) {
$this->components->task('Synced directory');

return;
}

$this->components->task(\sprintf(
'Copying %s [%s] to [%s]',
$type,
$fromLocation,
$toLocation,
transform_realpath_to_relative($format($from)),
transform_realpath_to_relative($format($to)),
));
}
}
30 changes: 30 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,36 @@ function refresh_router_lookups(Router $router): void
$router->getRoutes()->refreshNameLookups();
}

/**
* Transform realpath to alias path.
*
* @api
*
* @param string $path
* @param string|null $workingPath
* @return string
*/
function transform_realpath_to_relative(string $path, ?string $workingPath = null, string $prefix = ''): string
{
$separator = DIRECTORY_SEPARATOR;

if (! \is_null($workingPath)) {
return str_replace(rtrim($workingPath, $separator).$separator, $prefix.$separator, $path);
}

$laravelPath = base_path();
$workbenchPath = workbench_path();
$packagePath = package_path();

return match (true) {
str_starts_with($path, $laravelPath) => str_replace($laravelPath.$separator, '@laravel'.$separator, $path),
str_starts_with($path, $workbenchPath) => str_replace($workbenchPath.$separator, '@workbench'.$separator, $path),
str_starts_with($path, $packagePath) => str_replace($packagePath.$separator, '.'.$separator, $path),
! empty($prefix) => implode($separator, [$prefix, ltrim($path, $separator)]),
default => $path,
};
}

/**
* Transform relative path.
*
Expand Down
43 changes: 43 additions & 0 deletions tests/Foundation/Console/TerminatingConsoleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Orchestra\Testbench\Tests\Foundation\Console;

use Orchestra\Testbench\Foundation\Console\TerminatingConsole;
use PHPUnit\Framework\TestCase;

class TerminatingConsoleTest extends TestCase
{
/** @test */
public function it_can_handle_terminating_callbacks_on_terminal()
{
$this->assertFalse(isset($_SERVER['TerminatingConsole.before']));
$this->assertFalse(isset($_SERVER['TerminatingConsole.beforeWhenTrue']));
$this->assertFalse(isset($_SERVER['TerminatingConsole.beforeWhenFalse']));

TerminatingConsole::before(function () {
$_SERVER['TerminatingConsole.before'] = true;
});

TerminatingConsole::beforeWhen(true, function () {
$_SERVER['TerminatingConsole.beforeWhenTrue'] = true;
});

TerminatingConsole::beforeWhen(false, function () {
$_SERVER['TerminatingConsole.beforeWhenFalse'] = true;
});

TerminatingConsole::handle();

$this->assertTrue(isset($_SERVER['TerminatingConsole.before']));
$this->assertTrue(isset($_SERVER['TerminatingConsole.beforeWhenTrue']));
$this->assertFalse(isset($_SERVER['TerminatingConsole.beforeWhenFalse']));

unset(
$_SERVER['TerminatingConsole.before'],
$_SERVER['TerminatingConsole.beforeWhenTrue'],
$_SERVER['TerminatingConsole.beforeWhenFalse'],
);

TerminatingConsole::flush();
}
}
Loading

0 comments on commit 276b1ff

Please sign in to comment.