Skip to content

Commit

Permalink
wip
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
1 parent c014788 commit 01dbbf7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 43 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG-7.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

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

## 7.16.0
## 7.17.0

Released: 2024-12-24

### Added
### Changes

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

## 7.16.0

* Add `Orchestra\Workbench\StubRegistrar::replaceInFile()` method.
Released: 2024-12-24

### Changes

Expand Down
68 changes: 68 additions & 0 deletions src/Actions/ReplaceNamespaces.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Orchestra\Workbench\Actions;

use Illuminate\Filesystem\Filesystem;
use Orchestra\Workbench\Workbench;

class ReplaceNamespaces
{
/**
* Construct a new action.
*/
public function __construct(
protected Filesystem $filesystem
) {}

/**
* Handle the action.
*
* @param string $filename
*/
public function handle(string $filename): void
{
if (! $this->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,
];

$this->filesystem->put(
$filename,
str_replace(array_keys($keywords), array_values($keywords), $this->filesystem->get($filename))
);
}
}
41 changes: 1 addition & 40 deletions src/StubRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,45 +79,6 @@ function ($file) {
*/
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->put($filename, str_replace(array_keys($keywords), array_values($keywords), $filesystem->get($filename)));
(new Actions\ReplaceNamespaces($filesystem))->handle($filename);
}
}

0 comments on commit 01dbbf7

Please sign in to comment.