Skip to content

Commit

Permalink
Merge branch '8.x' into 9.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 24, 2024
2 parents ae3a2d0 + 2570008 commit fab3621
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 57 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG-8.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

This changelog references the relevant changes (bug and security fixes) done to `orchestra/workbench`.

## 8.16.0

Released: 2024-12-24

### Added

* Add `Orchestra\Workbench\StubRegistrar::replaceInFile()` method.

### Changes

* Allow to send options to `Orchestra\Workbench\Recipes\Command`.

## 8.15.2

Released: 2024-12-19
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"laravel/tinker": "^2.9",
"nunomaduro/collision": "^8.0",
"orchestra/canvas": "^9.1",
"orchestra/testbench-core": "^9.8.2",
"orchestra/testbench-core": "^9.9.0",
"symfony/polyfill-php83": "^1.31",
"symfony/polyfill-php84": "^1.31",
"symfony/process": "^7.0.3",
Expand Down
27 changes: 8 additions & 19 deletions src/Console/DevToolCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Orchestra\Workbench\Actions\ModifyComposer;
use Orchestra\Workbench\Events\InstallEnded;
use Orchestra\Workbench\Events\InstallStarted;
use Orchestra\Workbench\StubRegistrar;
use Orchestra\Workbench\Workbench;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -90,6 +91,8 @@ protected function prepareWorkbenchDirectories(Filesystem $filesystem, string $w
'--force' => (bool) $this->option('force'),
]);

StubRegistrar::replaceInFile($filesystem, join_paths($workbenchWorkingPath, 'Providers', 'WorkbenchServiceProvider.php'));

$this->prepareWorkbenchDatabaseSchema($filesystem, $workbenchWorkingPath);

if ($this->option('basic') === false) {
Expand Down Expand Up @@ -129,11 +132,15 @@ protected function prepareWorkbenchDatabaseSchema(Filesystem $filesystem, string
'--force' => (bool) $this->option('force'),
]);

StubRegistrar::replaceInFile($filesystem, join_paths($workingPath, 'app', 'Providers', 'WorkbenchServiceProvider.php'));

$this->callSilently('make:user-factory', [
'--preset' => 'workbench',
'--force' => (bool) $this->option('force'),
]);

StubRegistrar::replaceInFile($filesystem, join_paths($workingPath, 'database', 'factories', 'UserFactory.php'));

(new GeneratesFile(
filesystem: $filesystem,
components: $this->components,
Expand All @@ -143,25 +150,7 @@ protected function prepareWorkbenchDatabaseSchema(Filesystem $filesystem, string
join_paths($workingPath, 'database', 'seeders', 'DatabaseSeeder.php')
);

$workbenchSeederNamespacePrefix = rtrim(Workbench::detectNamespace('database/seeders') ?? 'Workbench\Database\Seeders\\', '\\');

$filesystem->replaceInFile([
'{{WorkbenchSeederNamespace}}',
'{{ WorkbenchSeederNamespace }}',
'Workbench\Database\Seeders',
], [
$workbenchSeederNamespacePrefix,
$workbenchSeederNamespacePrefix,
$workbenchSeederNamespacePrefix,
], join_paths($workingPath, 'database', 'seeders', 'DatabaseSeeder.php'));

if ($filesystem->exists(join_paths($workingPath, 'database', 'factories', 'UserFactory.php'))) {
$filesystem->replaceInFile([
'use Orchestra\Testbench\Factories\UserFactory;',
], [
\sprintf('use %sUserFactory;', Workbench::detectNamespace('database/factories') ?? 'Workbench\Database\Factories\\'),
], join_paths($workingPath, 'database', 'seeders', 'DatabaseSeeder.php'));
}
StubRegistrar::replaceInFile($filesystem, join_paths($workingPath, 'database', 'seeders', 'DatabaseSeeder.php'));
}

/**
Expand Down
39 changes: 2 additions & 37 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Orchestra\Testbench\Foundation\Console\Actions\GeneratesFile;
use Orchestra\Workbench\StubRegistrar;
use Orchestra\Workbench\Workbench;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -120,43 +121,7 @@ protected function copyTestbenchConfigurationFile(Filesystem $filesystem, string
force: (bool) $this->option('force'),
))->handle($from, $to);

$workbenchAppNamespacePrefix = rtrim(Workbench::detectNamespace('app') ?? 'Workbench\App\\', '\\');
$workbenchSeederNamespacePrefix = rtrim(Workbench::detectNamespace('database/seeders') ?? 'Workbench\Database\Seeders\\', '\\');

$serviceProvider = \sprintf('%s\Providers\WorkbenchServiceProvider', $workbenchAppNamespacePrefix);
$databaseSeeder = \sprintf('%s\DatabaseSeeder', $workbenchSeederNamespacePrefix);

$filesystem->replaceInFile(
[
'{{WorkbenchAppNamespace}}',
'{{ WorkbenchAppNamespace }}',
'{{WorkbenchSeederNamespace}}',
'{{ WorkbenchSeederNamespace }}',

'{{WorkbenchServiceProvider}}',
'{{ WorkbenchServiceProvider }}',
'Workbench\App\Providers\WorkbenchServiceProvider',

'{{WorkbenchDatabaseSeeder}}',
'{{ WorkbenchDatabaseSeeder }}',
'Workbench\Database\Seeders\DatabaseSeeder',
],
[
$workbenchAppNamespacePrefix,
$workbenchAppNamespacePrefix,
$workbenchSeederNamespacePrefix,
$workbenchSeederNamespacePrefix,

$serviceProvider,
$serviceProvider,
$serviceProvider,

$databaseSeeder,
$databaseSeeder,
$databaseSeeder,
],
$to
);
StubRegistrar::replaceInFile($filesystem, $to);
}

/**
Expand Down
48 changes: 48 additions & 0 deletions src/StubRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Orchestra\Workbench;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;

use function Orchestra\Testbench\join_paths;
Expand Down Expand Up @@ -72,4 +73,51 @@ function ($file) {
}
);
}

/**
* Replace stub namespaces.
*/
public static function replaceInFile(Filesystem $filesystem, string $filename): void
{
if (! $filesystem->isFile($filename)) {
return;
}

$workbenchAppNamespacePrefix = rtrim(Workbench::detectNamespace('app') ?? 'Workbench\App\\', '\\');
$workbenchFactoriesNamespacePrefix = rtrim(Workbench::detectNamespace('database/factories') ?? 'Workbench\Database\Factories\\', '\\');
$workbenchSeederNamespacePrefix = rtrim(Workbench::detectNamespace('database/seeders') ?? 'Workbench\Database\Seeders\\', '\\');

$serviceProvider = \sprintf('%s\Providers\WorkbenchServiceProvider', $workbenchAppNamespacePrefix);
$databaseSeeder = \sprintf('%s\DatabaseSeeder', $workbenchSeederNamespacePrefix);
$userModel = \sprintf('%s\Models\User', $workbenchAppNamespacePrefix);
$userFactory = \sprintf('%s\UserFactory', $workbenchFactoriesNamespacePrefix);

$keywords = [
'Workbench\App' => $workbenchAppNamespacePrefix,
'Workbench\Database\Factories' => $workbenchFactoriesNamespacePrefix,
'Workbench\Database\Seeders' => $workbenchSeederNamespacePrefix,

'{{WorkbenchAppNamespace}}' => $workbenchAppNamespacePrefix,
'{{ WorkbenchAppNamespace }}' => $workbenchAppNamespacePrefix,
'{{WorkbenchFactoryNamespace}}' => $workbenchFactoriesNamespacePrefix,
'{{ WorkbenchFactoryNamespace }}' => $workbenchFactoriesNamespacePrefix,
'{{WorkbenchSeederNamespace}}' => $workbenchSeederNamespacePrefix,
'{{ WorkbenchSeederNamespace }}' => $workbenchSeederNamespacePrefix,

'{{WorkbenchServiceProvider}}' => $serviceProvider,
'{{ WorkbenchServiceProvider }}' => $serviceProvider,

'{{WorkbenchDatabaseSeeder}}' => $databaseSeeder,
'{{ WorkbenchDatabaseSeeder }}' => $databaseSeeder,

'{{WorkbenchUserModel}}' => $userModel,
'{{ WorkbenchUserModel }}' => $userModel,

'{{WorkbenchUserFactory}}' => $userFactory,
'{{ WorkbenchUserFactory }}' => $userFactory,
'Orchestra\Testbench\Factories\UserFactory' => $userFactory,
];

$filesystem->replaceInFile(array_keys($keywords), array_values($keywords), $filename);
}
}

0 comments on commit fab3621

Please sign in to comment.