Skip to content

Commit

Permalink
Improved API for file creation
Browse files Browse the repository at this point in the history
- Added a Module class to help get the details of a module
- Added ability to create migrations in the directory.
  • Loading branch information
coolsam726 committed Apr 6, 2024
1 parent 178cb89 commit 246c540
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Commands/MigrationMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Savannabits\Modular\Commands;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Illuminate\Database\Console\Migrations\BaseCommand;
use Illuminate\Database\Console\Migrations\MigrateMakeCommand;
use Savannabits\Modular\Facades\Modular;
use Symfony\Component\Console\Input\InputArgument;

class MigrationMakeCommand extends Command implements PromptsForMissingInput
{
protected $signature = 'modular:make-migration
{name : The name of the migration}
{module : The module to create the migration for}
{--create= : The table to be created}
{--table= : The table to migrate}
';
protected $description = 'Create a new migration file in a module';

public function handle(): void
{
$this->call('make:migration', [
'name' => $this->argument('name'),
'--create' => $this->option('create'),
'--table' => $this->option('table'),
'--path' => $this->getMigrationPath(),
]);
}

protected function getMigrationPath(): string
{
return Modular::module($this->argument('module'))->migrationsPath(relative: true);
}

protected function promptForMissingArgumentsUsing(): array
{
return [
'name' => ['What should the migration name be?','e.g create_planets_table'],
'module' => ['Enter the module name (It has to be existing)','e.g Blog, BlogPost, blog-post']
];
}
}
5 changes: 5 additions & 0 deletions src/Modular.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public function execCommand(string $command): void
proc_close($process);
}
}

public function module(string $name): Module
{
return new Module($name);
}
}
98 changes: 98 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Savannabits\Modular;

use Illuminate\Support\Str;

class Module
{
private string $name;
private string $title;
private string $studlyName;
private string $namespace;
private string $basePath;

/**
* @throws \Exception
*/
public function __construct(string $name)
{
$this->name = Str::kebab($name);
$this->title = Str::of($name)->kebab()->title()->replace('-', ' ')->toString();
// If the module does not exist, throw an error
if (!is_dir(app()->basePath(config('modular.path') . DIRECTORY_SEPARATOR . $this->name))) {
abort(404,"Module $name does not exist");
}
$this->studlyName = Str::of($name)->studly()->toString();
$this->namespace = config('modular.namespace') . '\\' . $this->studlyName;
$this->basePath = app()->basePath(config('modular.path') . DIRECTORY_SEPARATOR . trim($this->name, DIRECTORY_SEPARATOR));
}

public function path(string $path = '', bool $relative = false): string
{
$basePath = $this->basePath;
if ($relative) {
$basePath = config('modular.path') . DIRECTORY_SEPARATOR . trim($this->name, DIRECTORY_SEPARATOR);
}
return $basePath . ($path ? DIRECTORY_SEPARATOR . trim($path, DIRECTORY_SEPARATOR) : '');
}

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

public function name(): string
{
return $this->name;
}

public function title(): string
{
return $this->title;
}

public function studlyName(): string
{
return $this->studlyName;
}

public function basePath(): string
{
return $this->basePath;
}

public function __toString(): string
{
return $this->title();
}

public function configPath(string $path = '', bool $relative = false): string
{
return $this->path('config'.DIRECTORY_SEPARATOR. ltrim($path,DIRECTORY_SEPARATOR), relative: $relative);
}

public function databasePath(string $path = '', bool $relative = false): string
{
return $this->path('database'.DIRECTORY_SEPARATOR. trim($path,DIRECTORY_SEPARATOR), relative: $relative);
}

public function migrationsPath(string $path = '', bool $relative = false): string
{
return $this->databasePath('migrations'.DIRECTORY_SEPARATOR. trim($path,DIRECTORY_SEPARATOR),relative: $relative);
}
public function seedsPath(string $path = '', bool $relative = false): string
{
return $this->databasePath('seeds'.DIRECTORY_SEPARATOR. trim($path,DIRECTORY_SEPARATOR), relative: $relative);
}

public function factoriesPath(string $path = '', bool $relative = false): string
{
return $this->databasePath('factories'.DIRECTORY_SEPARATOR. trim($path,DIRECTORY_SEPARATOR), $relative);
}

public function srcPath(string $path = '', bool $relative = false): string
{
return $this->path('src'.DIRECTORY_SEPARATOR. trim($path,DIRECTORY_SEPARATOR), $relative);
}
}

0 comments on commit 246c540

Please sign in to comment.