generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added a Module class to help get the details of a module - Added ability to create migrations in the directory.
- Loading branch information
1 parent
178cb89
commit 246c540
Showing
3 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |