-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Module command for view and view component
- Loading branch information
Showing
6 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) { | ||
$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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/Console/Commands/templates/view-component.inline.sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters