Skip to content

Commit

Permalink
Module command for view and view component
Browse files Browse the repository at this point in the history
  • Loading branch information
norbybaru committed Jun 15, 2024
1 parent a30f1ac commit f199a3c
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 4 deletions.
110 changes: 110 additions & 0 deletions src/Console/Commands/ModuleMakeComponentCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace NorbyBaru\Modularize\Console\Commands;

use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputOption;

class ModuleMakeComponentCommand extends ModuleMakerCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'module:make:component
{name : The name of the component}
{--module= : Name of module controller should belong to}
{--inline : Create a component that renders an inline view}
{--view: Create an anonymous component with only a view}
{--test : Generate an accompanying PHPUnit test for the Component}
{--pest : Generate an accompanying Pest test for the Component}';

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

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

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the component already exists'],
['inline', null, InputOption::VALUE_NONE, 'Create a component that renders an inline view'],
['view', null, InputOption::VALUE_NONE, 'Create an anonymous component with only a view'],
];
}

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

$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$filename);
$path = $this->getPath($name);
if ($this->files->exists($path) && ! $this->option('force')) {

Check failure on line 60 in src/Console/Commands/ModuleMakeComponentCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Command "module:make:component" does not have option "force".
$this->logFileExist($name);

return true;
}

$type = '';

if ($this->option('inline')) {
$type = 'inline.';
}

$this->setStubFile("view-component.{$type}");
$this->makeDirectory($path);

$this->files->put($path, $this->buildClass($name));

if (! $this->option('inline')) {
$this->call(
ModuleMakeViewCommand::class,
[
'name' => 'Components/'.Str::snake($filename, '-'),
'--module' => $module,
'--quiet' => true,
]
);
}

$this->logFileCreated($name);

return true;
}

protected function buildClass($name): string
{
if ($this->option('inline')) {
return str_replace(
['{{view}}', '{{ view }}'],
"<<<'blade'\n<div>\n <!-- ".Inspiring::quotes()->random()." -->\n</div>\nblade",
parent::buildClass($name)
);
}

return parent::buildClass($name);
}

protected function getFolderPath(): string
{
return 'Components';
}
}
2 changes: 1 addition & 1 deletion src/Console/Commands/ModuleMakeViewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function handle()
$this->call(
ModuleMakeTestCommand::class,
[
'name' => ($filename),
'name' => $filename,
'--module' => $module,
'--pest' => true,
'--view' => true,
Expand Down
8 changes: 6 additions & 2 deletions src/Console/Commands/ModuleMakerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,16 @@ protected function setStubFile(string $file): void

protected function logFileCreated(string $path)
{
$this->components->info(sprintf('%s [%s] created successfully.', $this->type, $path));
if (! $this->hasOption('quiet') || ! $this->option('quiet')) {

Check failure on line 336 in src/Console/Commands/ModuleMakerCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Negated boolean expression is always false.
$this->components->info(sprintf('%s [%s] created successfully.', $this->type, $path));
}
}

protected function logFileExist(string $path)
{
$this->components->error(sprintf('%s [%s] already exist.', $this->type, $path));
if (! $this->hasOption('quiet') || ! $this->option('quiet')) {

Check failure on line 343 in src/Console/Commands/ModuleMakerCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Negated boolean expression is always false.
$this->components->error(sprintf('%s [%s] already exist.', $this->type, $path));
}
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/Console/Commands/templates/view-component.inline.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}};
}
}
2 changes: 1 addition & 1 deletion src/Console/Commands/templates/view-component.sample
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ class {{ class }} extends Component
*/
public function render(): View|Closure|string
{
return {{ view }};
return view('{{moduleName}}::components.{{viewFile}}');
}
}
13 changes: 13 additions & 0 deletions src/ModularizeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Illuminate\Contracts\Foundation\CachesRoutes;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use NorbyBaru\Modularize\Console\Commands\ModuleCommand;
use NorbyBaru\Modularize\Console\Commands\ModuleMakeComponentCommand;
use NorbyBaru\Modularize\Console\Commands\ModuleMakeControllerCommand;
use NorbyBaru\Modularize\Console\Commands\ModuleMakeEventCommand;
use NorbyBaru\Modularize\Console\Commands\ModuleMakeJobCommand;
Expand Down Expand Up @@ -74,8 +76,10 @@ public function boot()
$this->autoloadHelper($moduleRootPath, $module);
$this->autoloadViews($moduleRootPath, $module);
$this->autoloadTranslations($moduleRootPath, $module);
$this->autoloadViewComponents($module);
}
}

}

/**
Expand Down Expand Up @@ -205,6 +209,14 @@ private function autoloadViews(string $moduleRootPath, string $module): void
}
}

private function autoloadViewComponents(string $module): void
{
Blade::componentNamespace(
"Modules\\{$module}\\Components",
$this->getModuleNamespace(name: $module)
);
}

private function autoloadTranslations(string $moduleRootPath, string $module): void
{
$path = "{$moduleRootPath}/{$module}/Lang";
Expand Down Expand Up @@ -234,6 +246,7 @@ protected function registerMakeCommand()
{
$this->commands([
ModuleCommand::class,
ModuleMakeComponentCommand::class,
ModuleMakeControllerCommand::class,
ModuleMakeEventCommand::class,
ModuleMakeJobCommand::class,
Expand Down

0 comments on commit f199a3c

Please sign in to comment.