Skip to content

Commit

Permalink
Merge pull request #6 from savannabits/0.x-dev
Browse files Browse the repository at this point in the history
New Feature: Generate Models
  • Loading branch information
coolsam726 authored Apr 6, 2024
2 parents 07296f0 + ace0d13 commit f823a78
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
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

0 comments on commit f823a78

Please sign in to comment.