Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Felice Ostuni committed Jun 14, 2024
1 parent 8191f74 commit 5c0139c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 34 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,24 @@ Rapyd Admin enhances Laravel by offering essential admin features with modular a

## Generators

Rapyd has some commands to generate components, views, modules (bundled components & views isolated in a folder) via artisan command line:
Rapyd has some commands to generate models, components, modules (bundled components & views isolated in a folder) via artisan command line:


### Models

generate a model (via command line)
```bash
php artisan rpd:make:model {ModelName}

# example
php artisan rpd:make:model Article
```

### Livewire components
```bash
php artisan rpd:make {ComponentName} {Model}

# example
php artisan rpd:make UserTable User
```

Expand All @@ -73,12 +87,14 @@ laravel/
```


## Modules
## Modules & Generators

example of out of the box module structure you can use after installing rapyd-admin.

```bash
php artisan rpd:make {ComponentsName} {Model} --module={module}

# example
php artisan rpd:make Articles Article --module=Blog
```
- Will create `Blog` folder in you app/Modules directory.
Expand Down
41 changes: 27 additions & 14 deletions src/Commands/RapydMakeBaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Touhidurabir\StubGenerator\Facades\StubGenerator;
use Zofe\Rapyd\Breadcrumbs\BreadcrumbsMiddleware;
use Zofe\Rapyd\Breadcrumbs\Manager;
use ReflectionClass;

class RapydMakeBaseCommand extends Command
{
Expand Down Expand Up @@ -62,35 +63,47 @@ protected function createModuleConfig()
}
}


protected function createModel($model)
{
$module = $this->option('module');
$modelClass = $this->getModelNamespace(true, false);

if (!$modelClass) {

$this->call('rpd:make:model', ['model' => $model, '--module' => $module]);

$this->
$this->call('migrate');
}
}

protected function getComponentName(): string
{
return Str::studly($this->argument('component'));
}
protected function getModelNamespace($full = false): string
protected function getModelNamespace($full = false, $ignore_existence = true): string
{
$model = $this->argument('model');
$module = $this->option('module');
$table = $this->option('table');

//cerco prima il model nel modulo
$namespace = namespace_module('App\\Models', $module);
if (! class_exists($namespace."\\".$model)) {

// $this->warn($namespace."\\".$model." doesn't exists as model");
$namespace = namespace_module('App\\Models');
//se non lo trovo cerco nel namespace principale
if(! class_exists($namespace."\\".$model)) {
$this->warn($namespace."\\".$model." doesn't exists as model");

if($table) {
//todo generare un model eloquent data la tabella
}
if ($ignore_existence) {
return $full ? $namespace . "\\" . $model : $namespace;
}

exit;
if (class_exists($namespace."\\".$model)) {
return $full ? $namespace . "\\" . $model : $namespace;
} else {
$namespace = 'App\\Models';
if (class_exists($namespace."\\".$model)) {
return $full ? $namespace . "\\" . $model : $namespace;
}
}

return $full ? $namespace."\\".$model : $namespace;
return false;
}

protected function getModelName(): string
Expand Down
4 changes: 1 addition & 3 deletions src/Commands/RapydMakeEditCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ public function handle()
$component_name = Str::snake($componentName);

$this->createModuleConfig();
// if(count($this->breadcrumbs->generate('home')) < 1) {
// $this->call('rpd:make:layout');
// }
$this->createModel($model);

$this->comment('generate '.$component.' for model '.$model);

Expand Down
37 changes: 29 additions & 8 deletions src/Commands/RapydMakeModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Zofe\Rapyd\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

Expand All @@ -22,12 +23,12 @@ public function handle()

$fields = [];
$fieldTypes = [
'string', 'integer', 'boolean', 'text', 'date',
'datetime', 'float', 'double', 'decimal', 'binary',
'enum', 'json', 'longText', 'mediumText', 'time',
'timestamp',
'string', 'text', 'integer', 'boolean', 'date',
'datetime', 'float', 'decimal', 'json', 'timestamp',
];

$this->comment('No Model ' . $modelName.' found, start creation...');

while (true) {
$fieldName = $this->ask('Field Name? (empty to end)');
if (empty($fieldName)) {
Expand All @@ -41,10 +42,11 @@ public function handle()
$fields[] = compact('fieldName', 'fieldType');
}

$this->call('make:model', ['name' => $modelName]);

$migrationName = 'create_' . Str::snake(Str::plural($modelName)) . '_table';
$this->call('make:migration', ['name' => $migrationName]);

Artisan::call('make:model', ['name' => $modelName, '--quiet' => true]);
Artisan::call('make:migration', ['name' => $migrationName, '--quiet' => true]);


$migrationFile = $this->getMigrationFile($migrationName);
if ($migrationFile) {
Expand All @@ -53,8 +55,27 @@ public function handle()
File::put($migrationFile, $migrationContent);
}

$this->info('Model and Migration created successfully');
if($this->module) {
$migrationName = basename($migrationFile);
$migration_from = base_path("database/migrations/$migrationName");
$migration_to = base_path(path_module("app/Database/Migrations/{$migrationName}", $this->module));
$model_from = base_path("app/Models/{$modelName}.php");
$model_to = base_path(path_module("app/Models/{$modelName}.php", $this->module));

File::ensureDirectoryExists(dirname($migration_to), 0755, true);
File::ensureDirectoryExists(dirname($model_to), 0755, true);

File::move($migration_from, $migration_to);
File::move($model_from, $model_to);

$content = File::get($model_to);
$updatedContent = str_replace('namespace App\\Models;', "namespace App\\Modules\\{$this->module}\\Models;", $content);
File::put($model_to, $updatedContent);

}

$this->info('Model and Migration created successfully, you can run `php artisan migrate` to create the table.');
exit;
}


Expand Down
5 changes: 1 addition & 4 deletions src/Commands/RapydMakeTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ public function handle()
$component = $this->getComponentName();
$model = $this->getModelName();

// if(count($this->breadcrumbs->generate('home')) < 1) {
// $this->call('rpd:make:layout');
// }

$this->createModuleConfig();
$this->createModel($model);

$this->comment('generate '.$component.' for model '.$model);

Expand Down
4 changes: 1 addition & 3 deletions src/Commands/RapydMakeViewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public function handle()
$component_name = Str::snake($componentName);

$this->createModuleConfig();
// if(count($this->breadcrumbs->generate('home')) < 1) {
// $this->call('rpd:make:layout');
// }
$this->createModel($model);

$this->comment('generate '.$component.' for model '.$model);

Expand Down

0 comments on commit 5c0139c

Please sign in to comment.