diff --git a/src/Console/MakeCommand.php b/src/Console/MakeCommand.php index fb82a86..5baad7b 100644 --- a/src/Console/MakeCommand.php +++ b/src/Console/MakeCommand.php @@ -4,55 +4,96 @@ use Illuminate\Console\Command; use Illuminate\Console\GeneratorCommand; -use RuntimeException; -use Symfony\Component\Process\PhpExecutableFinder; -use Symfony\Component\Process\Process; +use Illuminate\Contracts\Filesystem\FileNotFoundException; +use Illuminate\Support\Str; class MakeCommand extends GeneratorCommand { + + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'electrik:name {name} {--without-model=true} {--model=}'; + + /** + * The console command description. + * + * @var string + */ + protected $description = 'Create a new Electrik component with optional model'; + + /** + * The type of class being generated. + * + * @var string + */ + protected $type = 'Electrik component'; - protected $name = 'electrik:make'; - - protected $description = 'Create a new Electrik Component'; protected $stubsPath = __DIR__ . '/../../stubs/'; - protected function getStub() - { - return $this->stubsPath . 'controllers/controller.php.stub'; - } + protected function configure() { + + $this->setAliases([ + 'make:electrik', + ]); - protected function getDefaultNamespace($rootNamespace) - { - return $rootNamespace . '\Livewire'; + parent::configure(); } - public function handle() - { + + /** + * Execute the console command. + * + * @return bool|null + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + public function handle(): null|bool|FileNotFoundException { + + $name = $this->getNameInput(); + $name = str_replace('\\', '/', $name); + $modelName = $this->option('model') ?: Str::studly(class_basename($name)); + + // Create Livewire component parent::handle(); - $this->makeController(); - $this->makeView(); + $this->createComponentController($name); + $this->createView($name); + $this->info("Livewire component $name created successfully."); + + // Create Model if not excluded + if (!$this->option('without-model')) { + $this->call('make:model', ['name' => $modelName]); + $this->info("Model $modelName created successfully."); + } + + return Command::SUCCESS; + } + + /** + * Get the stub file for the generator. + * + * @return string + */ + protected function getStub(): string { + return $this->stubsPath . 'components/component.php.stub'; + } + + /** + * Get the default namespace for the class. + * + * @param string $rootNamespace + * @return string + */ + protected function getDefaultNamespace($rootNamespace): string { + return $rootNamespace . '\Livewire'; } - - protected function makeView() { - - if (!is_dir(dirname(resource_path().'/views/livewire/'.strtolower(str_replace("\\","/",$this->getNameInput()).'.blade.php')))) { - // dir doesn't exist, make it - mkdir(dirname(resource_path().'/views/livewire/'.strtolower(str_replace("\\","/",$this->getNameInput()).'.blade.php')), 0755, true); - } - - - file_put_contents( - resource_path().'/views/livewire/'.strtolower(str_replace("\\","/",$this->getNameInput()).'.blade.php'), - file_get_contents($this->stubsPath.'/views/view.blade.php.stub') - ); - } - protected function makeController() - { + protected function createComponentController($componentName) { // Get the fully qualified class name (FQN) - $class = $this->qualifyClass($this->getNameInput()); + $class = $this->qualifyClass($componentName); // get the destination path, based on the default namespace $path = $this->getPath($class); @@ -61,6 +102,42 @@ protected function makeController() // Update the file content with additional data (regular expressions) + $content = str_replace( + ['{{ namespace }}', '{{ className }}'], + [$class, class_basename($componentName)], + $content + ); + + file_put_contents($path, $content); } -} \ No newline at end of file + + + /** + * Create a view file for the component. + * + * @param string $name + * @return void + */ + protected function createView($name): void { + $viewPath = resource_path('views/livewire/' . str_replace('\\', '/', $name) . '.blade.php'); + $stub = file_get_contents($this->stubsPath . 'resources/views/view.blade.php.stub'); + + if (!file_exists($dir = dirname($viewPath))) { + mkdir($dir, 0777, true); + } + + file_put_contents($viewPath, $stub); + } + + /** + * Get the full namespace for a given class, without the class name. + * + * @param $class + * @return string + */ + protected function qualifyNamespace($class): string { + return trim(implode('\\', array_slice(explode('\\', $class), 0, -1)), '\\'); + } + +} diff --git a/stubs/controllers/controller.php.stub b/stubs/components/component.php.stub similarity index 57% rename from stubs/controllers/controller.php.stub rename to stubs/components/component.php.stub index f346a67..8483b9e 100644 --- a/stubs/controllers/controller.php.stub +++ b/stubs/components/component.php.stub @@ -1,18 +1,19 @@