Skip to content

Commit

Permalink
drop support for tomato-php and dbl
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Mar 28, 2024
1 parent acf130a commit d537586
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 98 deletions.
2 changes: 1 addition & 1 deletion resources/views/plugins/list.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</div>
<div class="flex justifiy-between gap-1 my-4 px-4 border-t border-gray-100 pt-4">
<div class="flex justifiy-start w-full">
@if(class_exists("Doctrine\DBAL\DriverManager") && (bool)config('tomato-plugins.allow_generator'))
@if((bool)config('tomato-plugins.allow_generator'))
<x-tomato-admin-button type="icon" href="{{route('admin.tables.index', ['module' => $item->module_name])}}" >
<x-tomato-admin-tooltip :text="__('Tables')">
<x-heroicon-s-server class="w-5 h-5" />
Expand Down
154 changes: 154 additions & 0 deletions src/Console/TomatoPluginsCrudGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace TomatoPHP\TomatoPlugins\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Nwidart\Modules\Facades\Module;
use TomatoPHP\ConsoleHelpers\Traits\RunCommand;
use TomatoPHP\TomatoPlugins\Services\CRUDGenerator;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\search;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;
use function Laravel\Prompts\error;
use function Laravel\Prompts\warning;
use function Laravel\Prompts\suggest;



class TomatoPluginsCrudGenerator extends Command
{
use RunCommand;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tomato:generate
{table=0}
{module=0}
{--api}
{--builder}
';

/**
* The console command description.
*
* @var string
*/
protected $description = 'create a new CRUD for the application by tomato';


/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$tables = collect(\DB::select('SHOW TABLES'))->map(function ($item){
return $item->{'Tables_in_'.config('database.connections.mysql.database')};
})->toArray();

$tableName = $this->argument('table') && $this->argument('table') != "0" ? $this->argument('table') : search(
label: 'Please input your table name you went to create CRUD?',
options: fn (string $value) => strlen($value) > 0
? collect($tables)->filter(function ($item, $key) use ($value){
return Str::contains($item, $value) ? (string)$item : null;
})->toArray()
: [],
placeholder: "ex: users",
scroll: 10
);

if(is_numeric($tableName)){
$tableName = $tables[$tableName];
}
else {
$tableName = $tableName;
}

//Check if user need to use HMVC
$isModule = ($this->argument('module') && $this->argument('module') != "0") ?: confirm('Do you went to use HMVC module?');
$moduleName = false;
if ($isModule){
if (class_exists(\Nwidart\Modules\Facades\Module::class)){
$modules = \Nwidart\Modules\Facades\Module::toCollection()->map(function ($item){
return $item->getName();
});
$moduleName = ($this->argument('module') && $this->argument('module') != "0") ? $this->argument('module') : suggest(
label:'Please input your module name?',
placeholder:'Translations',
options: fn (string $value) => strlen($value) > 0
? collect($modules)->filter(function ($item, $key) use ($value){
return Str::contains($item, $value) ? $item : null;
})->toArray()
: [],
validate: fn (string $value) => match (true) {
strlen($value) < 1 => "Sorry this filed is required!",
default => null
},
scroll: 10
);
$check = \Nwidart\Modules\Facades\Module::find($moduleName);
if (!$check) {
$createIt = confirm('Module not found! do you when to create it?');
$createIt ? $this->artisanCommand(["module:make", $moduleName]) : $moduleName = null;
\Laravel\Prompts\info('We Generate It please re-run the command again');
exit();
}
}
else {
$installItem = confirm('Sorry nwidart/laravel-modules not installed please install it first. do you when to install it?');
if($installItem){
$this->requireComposerPackages(["nwidart/laravel-modules"]);
\Laravel\Prompts\info('Add This line to composer.json psr-4 autoload');
\Laravel\Prompts\info('"Modules\\" : "Modules/"');
\Laravel\Prompts\info('now run');
\Laravel\Prompts\info('composer dump-autoload');
\Laravel\Prompts\info('Install success please run the command again');
exit();
}
}
}

$generateAPI = ($this->option('api') && $this->option('api') != "0") ? $this->option('api') : confirm(
label: 'Do you went to generate api routes?',
);

$generateForm = ($this->option('builder') && $this->option('builder') != "0") ? $this->option('builder') : confirm(
label: 'Do you went to use form class builder?',
);

//Generate CRUD Service
try {
\Laravel\Prompts\spin(fn()=> (new CRUDGenerator(
tableName:$tableName,
moduleName:$moduleName,
apiRoutes: $generateAPI,
isBuilder: $generateForm,
migration: false,
models: true,
request: true,
tables: true,
routes: true,
controllers: true,
views: true,
json: true,
menu: true,
))->generate(), 'Generating ...');
} catch (\Exception $e) {
\Laravel\Prompts\error($e);
}

\Laravel\Prompts\info('🍅 Thanks for using Tomato Plugins & TomatoPHP framework');
\Laravel\Prompts\info('💼 Join support server on discord https://discord.gg/VZc8nBJ3ZU');
\Laravel\Prompts\info('📄 You can check docs here https://docs.tomatophp.com');
\Laravel\Prompts\info('⭐ please gave us a start on any repo if you like it https://github.com/tomatophp');
\Laravel\Prompts\info('🤝 sponser us here https://github.com/sponsors/3x1io');
}
}

28 changes: 10 additions & 18 deletions src/Services/CRUDGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace TomatoPHP\TomatoPlugins\Services;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -36,6 +33,7 @@ class CRUDGenerator
{
private string $modelName;
private string $stubPath;
private array $cols=[];

//Handler
use HandleStub;
Expand All @@ -47,7 +45,6 @@ class CRUDGenerator
use GenerateMigrations;
use GenerateCols;
use GenerateModel;
use GenerateCasts;
use GenerateTable;
use GenerateRules;
use GenerateController;
Expand Down Expand Up @@ -75,7 +72,7 @@ class CRUDGenerator
* @throws Exception
*/
public function __construct(
private Table $table,
private ?Table $table = null,
private string | null $tableName = null,
private string | bool | null $moduleName = null,
private bool $isBuilder = false,
Expand All @@ -92,20 +89,16 @@ public function __construct(
private bool $json = false,
private bool $menu = false,
){
$this->tableName = $this->table->name;
$this->moduleName = $this->table->module;

$connectionParams = [
'dbname' => config('database.connections.mysql.database'),
'user' => config('database.connections.mysql.username'),
'password' => config('database.connections.mysql.password'),
'host' => config('database.connections.mysql.host'),
'driver' => 'pdo_mysql',
];

$this->connection = DriverManager::getConnection($connectionParams);
if(!$this->tableName){
$this->tableName = $this->table->name;

}
if(!$this->moduleName){
$this->moduleName = $this->table->module;
}
$this->modelName = Str::ucfirst(Str::singular(Str::camel($this->tableName)));
$this->stubPath = base_path('vendor/tomatophp/tomato-plugins/stubs') . "/";
$this->cols = $this->getCols();
}

/**
Expand All @@ -121,7 +114,6 @@ public function generate(): bool
sleep(3);
if($this->models){
$this->generateModel();
$this->generateCasts();
}
if($this->tables){
$this->generateTable();
Expand Down
12 changes: 0 additions & 12 deletions src/Services/Concerns/GenerateCasts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@

trait GenerateCasts
{
private function generateCasts()
{
$this->injectString(
$this->moduleName ? module_path($this->moduleName) ."/App/Models/{$this->modelName}.php" : app_path("Models/{$this->modelName}.php"),
'protected $fillable =',
$this->stubPath . "casts.stub",
[
"casts" => $this->getCasts(),
]
);
}

private function getCasts()
{
$casts = [];
Expand Down
Loading

0 comments on commit d537586

Please sign in to comment.