Skip to content

Commit

Permalink
updated the make command to make it more inline with the package
Browse files Browse the repository at this point in the history
  • Loading branch information
neerajsohal committed Jun 14, 2024
1 parent dde6b78 commit 7c0b9f1
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 38 deletions.
147 changes: 112 additions & 35 deletions src/Console/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}


/**
* 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)), '\\');
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?php

namespace DummyNamespace;
namespace {{ namespace }};

use Livewire\Component;
use Usernotnull\Toast\Concerns\WireToast;

class DummyClass extends Component {
class {{ className }} extends Component {

use WireToast;

public function mount() {

}

public function render() {
return view('livewire.dashboard.index');
return view('livewire.{{ className }}');
}
}
File renamed without changes.

0 comments on commit 7c0b9f1

Please sign in to comment.