Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: Generate Models #6

Merged
merged 3 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions src/Commands/ModelMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Savannabits\Modular\Commands;

use Illuminate\Foundation\Console\ModelMakeCommand as BaseModelMakeCommand;
use Illuminate\Support\Str;
use Savannabits\Modular\Facades\Modular;
use Savannabits\Modular\Module;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;

#[AsCommand(name: 'modular:make-model')]
class ModelMakeCommand extends BaseModelMakeCommand
{
protected $name = 'modular:make-model';

protected $description = 'Create a new Eloquent model class in a modular package';

protected function getArguments(): array
{
return array_merge(parent::getArguments(), [
['module', InputArgument::REQUIRED, 'The name of the module in which this should be installed'],
]);
}

public function getModule(): Module
{
return Modular::module($this->argument('module'));
}

protected function getDefaultNamespace($rootNamespace): string
{
return $rootNamespace.'\\Models';
}

protected function rootNamespace(): string
{
return $this->getModule()->getRootNamespace();
}

protected function getPath($name): string
{
$name = Str::replaceFirst($this->rootNamespace(), '', $name);

return $this->getModule()->srcPath(str_replace('\\', '/', $name).'.php');
}

protected function createFactory(): void
{
$factory = Str::studly($this->argument('name'));

$this->call('make:factory', [
'name' => "{$factory}Factory",
'--model' => $this->qualifyClass($this->getNameInput()),
]);
}

/**
* Create a migration file for the model.
*/
protected function createMigration(): void
{
$table = Str::snake(Str::pluralStudly(class_basename($this->argument('name'))));

if ($this->option('pivot')) {
$table = Str::singular($table);
}

$this->call('modular:make-migration', [
'name' => "create_{$table}_table",
'module' => $this->getModule()->name(),
'--create' => $table,
]);
}

/**
* Create a seeder file for the model.
*/
protected function createSeeder(): void
{
$seeder = Str::studly(class_basename($this->argument('name')));

$this->call('make:seeder', [
'name' => "{$seeder}Seeder",
]);
}

/**
* Create a controller for the model.
*/
protected function createController(): void
{
$controller = Str::studly(class_basename($this->argument('name')));

$modelName = $this->qualifyClass($this->getNameInput());

$this->call('make:controller', array_filter([
'name' => "{$controller}Controller",
'--model' => $this->option('resource') || $this->option('api') ? $modelName : null,
'--api' => $this->option('api'),
'--requests' => $this->option('requests') || $this->option('all'),
'--test' => $this->option('test'),
'--pest' => $this->option('pest'),
]));
}

/**
* Create a policy file for the model.
*/
protected function createPolicy(): void
{
$policy = Str::studly(class_basename($this->argument('name')));

$this->call('make:policy', [
'name' => "{$policy}Policy",
'--model' => $this->qualifyClass($this->getNameInput()),
]);
}
}
5 changes: 5 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public function path(string $path = '', bool $relative = false): string
return $basePath.($path ? DIRECTORY_SEPARATOR.trim($path, DIRECTORY_SEPARATOR) : '');
}

public function getRootNamespace(): string
{
return $this->namespace.'\\';
}

public function makeNamespace(string $relativeNamespace = ''): string
{
return $this->namespace.($relativeNamespace ? '\\'.ltrim($relativeNamespace, '\\') : '');
Expand Down