Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,23 @@
'replacements' => [
/**
* Define custom replacements for each section.
* You can specify a closure for dynamic values.
* You can now specify a class name that extends
* \Nwidart\Modules\Support\ReplacementKeyCommand for dynamic values.
*
* Example:
*
* 'composer' => [
* 'CUSTOM_KEY' => fn (\Nwidart\Modules\Generators\ModuleGenerator $generator) => $generator->getModule()->getLowerName() . '-module',
* 'CUSTOM_KEY2' => fn () => 'custom text',
* // Map the UPPERCASE token to your command class
* 'CUSTOM_KEY' => \App\Modules\Support\Replacements\CustomKey::class,
* // You can still list built-in tokens by their names
* 'LOWER_NAME',
* 'STUDLY_NAME',
* // ...
* ],
*
* The command class must extend ReplacementKeyCommand and implement handle(): string
* to return the replacement text.
*
* Note: Keys should be in UPPERCASE.
*/
'routes/web' => ['LOWER_NAME', 'STUDLY_NAME', 'PLURAL_LOWER_NAME', 'KEBAB_NAME', 'MODULE_NAMESPACE', 'CONTROLLER_NAMESPACE'],
Expand Down Expand Up @@ -132,6 +137,7 @@
'channels' => ['path' => 'app/Broadcasting', 'generate' => false],
'class' => ['path' => 'app/Classes', 'generate' => false],
'command' => ['path' => 'app/Console', 'generate' => false],
'command_replacements' => ['path' => 'app/Console/Replacements', 'generate' => false],
'component-class' => ['path' => 'app/View/Components', 'generate' => false],
'emails' => ['path' => 'app/Emails', 'generate' => false],
'event' => ['path' => 'app/Events', 'generate' => false],
Expand Down
82 changes: 82 additions & 0 deletions src/Commands/Make/ReplacementMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Nwidart\Modules\Commands\Make;

use Illuminate\Support\Str;
use Nwidart\Modules\Support\Config\GenerateConfigReader;
use Nwidart\Modules\Support\Stub;
use Nwidart\Modules\Traits\ModuleCommandTrait;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class ReplacementMakeCommand extends GeneratorCommand
{
use ModuleCommandTrait;

/**
* The name of the console command.
*/
protected $name = 'module:make-replacement';

/**
* The console command description.
*/
protected $description = 'Create a new replacement key command class for stubs.';

protected $argumentName = 'name';

public function getDestinationFilePath(): string
{
$path = $this->laravel['modules']->getModulePath($this->getModuleName());

$filePath = GenerateConfigReader::read('command_replacements')->getPath() ?? config('modules.paths.app_folder').'Console/Replacements';

return $path.$filePath.'/'.$this->getFileName().'.php';
}

protected function getTemplateContents(): string
{
$module = $this->laravel['modules']->findOrFail($this->getModuleName());

return (new Stub($this->getStubName(), [
'NAMESPACE' => $this->getClassNamespace($module),
'CLASS' => $this->getClassNameWithoutNamespace(),
]))->render();
}

protected function getArguments(): array
{
return [
['name', InputArgument::REQUIRED, 'The name of the enum class.'],
['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
];
}

protected function getOptions(): array
{
return [
['force', 'f', InputOption::VALUE_NONE, 'su.'],
];
}

protected function getFileName(): string
{
return Str::studly($this->argument('name'));
}

private function getClassNameWithoutNamespace(): array|string
{
return class_basename($this->getFileName());
}

public function getDefaultNamespace(): string
{
return config('modules.paths.generator.command_replacements.namespace')
?? ltrim(config('modules.paths.generator.command_replacements.path', 'Console/Replacements'), config('modules.paths.app_folder', ''));
}

protected function getStubName(): string
{
return '/replacement.stub';
}
}
13 changes: 13 additions & 0 deletions src/Commands/stubs/replacement.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace $NAMESPACE$;

use Nwidart\Modules\Support\ReplacementKeyCommand;

class $CLASS$ extends ReplacementKeyCommand
{
public function handle(): string
{
return $this->generator->getModule()->getLowerName();
}
}
5 changes: 3 additions & 2 deletions src/Generators/ModuleGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Nwidart\Modules\FileRepository;
use Nwidart\Modules\Module;
use Nwidart\Modules\Support\Config\GenerateConfigReader;
use Nwidart\Modules\Support\ReplacementKeyCommand;
use Nwidart\Modules\Support\Stub;
use Nwidart\Modules\Traits\PathNamespace;

Expand Down Expand Up @@ -494,8 +495,8 @@ protected function getReplacement($stub): array
}

foreach ($keys as $key => $value) {
if ($value instanceof \Closure) {
$replaces[strtoupper($key)] = $value($this);
if (class_exists($value) && is_subclass_of($value, ReplacementKeyCommand::class)) {
$replaces[strtoupper($key)] = (new $value($this))->handle();
} elseif (method_exists($this, $method = 'get'.ucfirst(Str::studly(strtolower($value))).'Replacement')) {
$replace = $this->$method();

Expand Down
1 change: 1 addition & 0 deletions src/Providers/ConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static function defaultCommands(): Collection
Commands\Make\ResourceMakeCommand::class,
Commands\Make\RouteProviderMakeCommand::class,
Commands\Make\RuleMakeCommand::class,
Commands\Make\ReplacementMakeCommand::class,
Commands\Make\ScopeMakeCommand::class,
Commands\Make\SeedMakeCommand::class,
Commands\Make\ServiceMakeCommand::class,
Expand Down
12 changes: 12 additions & 0 deletions src/Support/ReplacementKeyCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Nwidart\Modules\Support;

use Nwidart\Modules\Generators\ModuleGenerator;

abstract class ReplacementKeyCommand
{
public function __construct(protected ModuleGenerator $generator) {}

abstract public function handle(): string;
}
77 changes: 77 additions & 0 deletions tests/Commands/Make/ReplacementMakeCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Nwidart\Modules\Tests\Commands\Make;

use Nwidart\Modules\Contracts\RepositoryInterface;
use Nwidart\Modules\Tests\BaseTestCase;
use Spatie\Snapshots\MatchesSnapshots;

class ReplacementMakeCommandTest extends BaseTestCase
{
use MatchesSnapshots;

/** @var \Illuminate\Filesystem\Filesystem */
private $finder;

/** @var string */
private $modulePath;

protected function setUp(): void
{
parent::setUp();
$this->finder = $this->app['files'];
$this->createModule();
$this->modulePath = $this->getModuleAppPath();
}

protected function tearDown(): void
{
$this->app[RepositoryInterface::class]->delete('Blog');
parent::tearDown();
}

public function test_it_generates_a_new_replacement_class()
{
$code = $this->artisan('module:make-replacement', ['name' => 'Demo', 'module' => 'Blog']);

$this->assertTrue(is_file($this->modulePath.'/Console/Replacements/Demo.php'));
$this->assertSame(0, $code);
}

public function test_it_generates_a_new_replacement_class_can_override_with_force_option()
{
$this->artisan('module:make-replacement', ['name' => 'Demo', 'module' => 'Blog']);
$code = $this->artisan('module:make-replacement', ['name' => 'Demo', 'module' => 'Blog', '--force' => true]);

$this->assertTrue(is_file($this->modulePath.'/Console/Replacements/Demo.php'));
$this->assertSame(0, $code);
}

public function test_it_generated_correct_file_with_content()
{
$code = $this->artisan('module:make-replacement', ['name' => 'Demo', 'module' => 'Blog']);

$file = $this->finder->get($this->modulePath.'/Console/Replacements/Demo.php');

$this->assertMatchesSnapshot($file);
$this->assertSame(0, $code);
}

public function test_it_can_generate_a_replacement_class_in_sub_namespace_in_correct_folder()
{
$code = $this->artisan('module:make-replacement', ['name' => 'Api\\Demo', 'module' => 'Blog']);

$this->assertTrue(is_file($this->modulePath.'/Console/Replacements/Api/Demo.php'));
$this->assertSame(0, $code);
}

public function test_it_can_generate_a_replacement_class_in_sub_namespace_with_correct_generated_file()
{
$code = $this->artisan('module:make-replacement', ['name' => 'Api\\Demo', 'module' => 'Blog']);

$file = $this->finder->get($this->modulePath.'/Console/Replacements/Api/Demo.php');

$this->assertMatchesSnapshot($file);
$this->assertSame(0, $code);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Modules\Blog\Console\Replacements\Api;

use Nwidart\Modules\Support\ReplacementKeyCommand;

class Demo extends ReplacementKeyCommand
{
public function handle(): string
{
return $this->generator->getModule()->getLowerName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Modules\Blog\Console\Replacements;

use Nwidart\Modules\Support\ReplacementKeyCommand;

class Demo extends ReplacementKeyCommand
{
public function handle(): string
{
return $this->generator->getModule()->getLowerName();
}
}