Skip to content

Commit

Permalink
module view scaffolding command
Browse files Browse the repository at this point in the history
  • Loading branch information
norbybaru committed Mar 18, 2024
1 parent 1c6bb71 commit a30f1ac
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 13 deletions.
20 changes: 18 additions & 2 deletions src/Console/Commands/ModuleMakeTestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ class ModuleMakeTestCommand extends ModuleMakerCommand
{name : The name of the test}
{--module= : Name of module migration should belong to}
{--u|unit : Create a unit test}
{--p|pest : Create a Pest test}';
{--p|pest : Create a Pest test}
{--view : Create a view test}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate resource for a module';
protected $description = 'Generate test resource for a module';

/**
* The type of class being generated.
Expand All @@ -41,11 +42,26 @@ public function handle()
$testType = 'Feature';
$prefix = 'test';
$type = '';

if ($this->option('unit')) {
$type = 'unit.';
$testType = 'Unit';
}

if ($this->option('view')) {
$filename = collect(explode('/', $filename))
->map(fn ($name) => ucwords($name))
->join('/');

$filename = "View/{$filename}Test";
$type = 'view.';
$testType = 'Feature';

if ($this->option('pest')) {
$prefix = 'pest';
}
}

$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$testType.'\\'.$filename);

if ($this->files->exists($path = $this->getPath($name))) {
Expand Down
87 changes: 87 additions & 0 deletions src/Console/Commands/ModuleMakeViewCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace NorbyBaru\Modularize\Console\Commands;

class ModuleMakeViewCommand extends ModuleMakerCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'module:make:view
{name : The name of the view}
{--module= : Name of module migration should belong to}
{--test : Generate an accompanying PHPUnit test for the View}
{--pest : Generate an accompanying Pest test for the View}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate view resource for a module';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'view';

public function handle()
{
$module = $this->getModuleInput();
$filename = $this->getNameInput();
$folder = $this->getFolderPath();

$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$filename);

if ($this->files->exists($path = $this->getPath($name, 'blade.php'))) {
$this->logFileExist($name);

return true;
}

$type = '';

$this->setStubFile("view.{$type}");
$this->makeDirectory($path);
$this->files->put($path, $this->buildClass($name));

$this->logFileCreated($name);

$folder = 'Tests/Feature';
if ($this->option('pest')) {
$this->call(
ModuleMakeTestCommand::class,
[
'name' => ($filename),
'--module' => $module,
'--pest' => true,
'--view' => true,
]
);
$type = 'pest.';
}

if ($this->option('test')) {
$this->call(
ModuleMakeTestCommand::class,
[
'name' => $filename,
'--module' => $module,
'--view' => true,
]
);
$type = 'test.';
}

return true;
}

protected function getFolderPath(): string
{
return 'Views';
}
}
46 changes: 38 additions & 8 deletions src/Console/Commands/ModuleMakerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ abstract class ModuleMakerCommand extends GeneratorCommand
*/
protected $currentStub = __DIR__.'/templates/';

protected ?string $module = null;

/**
* Get the stub file for the generator.
*
Expand All @@ -26,11 +28,11 @@ protected function getStub()

public function getModuleInput(): string
{
if (! $module = $this->option('module')) {
$module = $this->ask('What is the name of the module?');
if (! $this->module = $this->option('module')) {
$this->module = $this->ask('What is the name of the module?');
}

return $module;
return $this->module;
}

/**
Expand All @@ -46,6 +48,7 @@ protected function buildClass($name)
$stub = $this->files->get($this->getStub());

return $this->replaceName($stub, $this->getNameInput())
->replaceModuleName($stub, $this->module)
->replaceNamespace($stub, $name)
->replaceClass($stub, $name);
}
Expand Down Expand Up @@ -119,15 +122,42 @@ protected function replaceName(&$stub, $name)
{
$title = $name;

$stub = str_replace(search: 'SampleTitle', replace: strtolower($name), subject: $stub);
$stub = str_replace(search: 'SampleViewTitle', replace: strtolower(Str::snake($title, '-')), subject: $stub);
$stub = str_replace(search: 'SampleUCtitle', replace: ucfirst(Str::studly($name)), subject: $stub);
$stub = str_replace(
search: [
'SampleTitle',
'SampleViewTitle',
'SampleUCtitle',
'{{viewFile}}',
],
replace: [
strtolower($name),
strtolower(Str::snake($title, '-')),
ucfirst(Str::studly($name)),
strtolower(Str::snake(str_replace(search: '/', replace: '.', subject: $title), '-')),
],
subject: $stub
);

$stub = $this->removePrefixFromRoutes($stub);

return $this;
}

protected function replaceModuleName(&$stub, $name)
{
$stub = str_replace(
search: [
'{{ moduleName }}',
'{{moduleName}}',
'moduleName',
],
replace: strtolower($name),
subject: $stub
);

return $this;
}

/**
* Remove prefix from routes when there its not a module group
*
Expand Down Expand Up @@ -317,11 +347,11 @@ protected function logFileExist(string $path)
* @param string $name
* @return string
*/
protected function getPath($name)
protected function getPath($name, string $fileExtension = 'php')
{
$name = Str::replaceFirst($this->rootNamespace(), '', $name);

return $this->getModuleRootPath().'/'.str_replace('\\', '/', $name).'.php';
return $this->getModuleRootPath().'/'.str_replace('\\', '/', $name).".{$fileExtension}";
}

protected function getModuleRootPath(): string
Expand Down
9 changes: 9 additions & 0 deletions src/Console/Commands/templates/pest.view.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

it('can render', function () {
$contents = $this->view('{{moduleName}}::{{viewFile}}', [
//
]);

$contents->assertSee('');
});
20 changes: 20 additions & 0 deletions src/Console/Commands/templates/test.view.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace {{ namespace }};

use Tests\TestCase;

class {{ class }} extends TestCase
{
/**
* A basic view test example.
*/
public function test_it_can_render(): void
{
$contents = $this->view('{{moduleName}}::{{viewFile}}', [
//
]);

$contents->assertSee('');
}
}
26 changes: 26 additions & 0 deletions src/Console/Commands/templates/view-component.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace {{ namespace }};

use Closure;
use Illuminate\View\Component;
use Illuminate\Contracts\View\View;

class {{ class }} extends Component
{
/**
* Create a new component instance.
*/
public function __construct()
{
//
}

/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
return {{ view }};
}
}
8 changes: 6 additions & 2 deletions src/Console/Commands/templates/view.sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
<?php
echo trans('SampleViewTitle::example.welcome');
<div>
<!-- To Render translation, create a Lang/{locale}/messages.php file inside module directory -->
<!-- Replace {module} with module name -->

{{-- {{ __('{module}::messages.welcome') }} --}}
</div>
4 changes: 3 additions & 1 deletion src/ModularizeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use NorbyBaru\Modularize\Console\Commands\ModuleMakeRequestCommand;
use NorbyBaru\Modularize\Console\Commands\ModuleMakeResourceCommand;
use NorbyBaru\Modularize\Console\Commands\ModuleMakeTestCommand;
use NorbyBaru\Modularize\Console\Commands\ModuleMakeViewCommand;

class ModularizeServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -206,7 +207,7 @@ private function autoloadViews(string $moduleRootPath, string $module): void

private function autoloadTranslations(string $moduleRootPath, string $module): void
{
$path = "{$moduleRootPath}/{$module}/Translations";
$path = "{$moduleRootPath}/{$module}/Lang";

if ($this->files->isDirectory(directory: $path)) {
$this->loadTranslationsFrom(
Expand Down Expand Up @@ -246,6 +247,7 @@ protected function registerMakeCommand()
ModuleMakeResourceCommand::class,
ModuleMakeRequestCommand::class,
ModuleMakeTestCommand::class,
ModuleMakeViewCommand::class,
]);
}
}

0 comments on commit a30f1ac

Please sign in to comment.