From 0503b9d2cd38054a18bc6f0dd438e472d70cda99 Mon Sep 17 00:00:00 2001 From: Miftahurrahmi Date: Mon, 3 Jun 2024 13:21:02 +0700 Subject: [PATCH 01/10] update laravel 11 --- composer.json | 41 +- src/Config/badaso-hidden-tables.php | 6 + src/Controllers/BadasoCRUDController.php | 863 +++++++++++----------- src/Controllers/BadasoTableController.php | 9 +- src/Database/Schema/SchemaManager.php | 16 +- src/Providers/BadasoServiceProvider.php | 4 +- 6 files changed, 475 insertions(+), 464 deletions(-) diff --git a/composer.json b/composer.json index 1513adcf7..f36eaf098 100644 --- a/composer.json +++ b/composer.json @@ -24,30 +24,25 @@ }, "type": "library", "require": { + "laravel-doctrine/orm": "2.1", "psr/http-message": "1.1", - "arcanedev/log-viewer": "8.1.0", - "larapack/doctrine-support": "0.1.9", - "darkaonline/l5-swagger": "8.5", - "symfony/var-exporter": "5.4.21", - "phpstan/phpstan": "1.10.15", - "spatie/laravel-backup": "7.8", - "unisharp/laravel-filemanager": "2.6", - "spatie/laravel-activitylog": "4.7.1", - "php-open-source-saver/jwt-auth": "2.1.0", - "google/apiclient": "2.13.2", - "spatie/flysystem-dropbox": "1.2.3", - "masbug/flysystem-google-drive-ext": "1.3.2", - "laravel/octane": "1.5.5", - "laravel/sanctum": "2.15.1", - "laravel/tinker": "2.8.1", - "laravel/ui": "3.4.6", - "webpatser/laravel-uuid": "4.0.1", - "predis/predis": "2.1.2", - "guzzlehttp/guzzle": "7.5.1", - "zircote/swagger-php": "4.7.10", - "doctrine/dbal": "2.13.9", - "league/flysystem-aws-s3-v3": "1.0.29", - "league/flysystem": "1.1.10" + "arcanedev/log-viewer": "^11.0", + "darkaonline/l5-swagger": "^8.6", + "phpstan/phpstan": "^1.11", + "spatie/laravel-backup": "^8.8", + "unisharp/laravel-filemanager": "^2.9", + "spatie/laravel-activitylog": "^4.8", + "php-open-source-saver/jwt-auth": "^2.3", + "google/apiclient": "^2.16", + "spatie/flysystem-dropbox": "^3.0", + "masbug/flysystem-google-drive-ext": "^2.3", + "laravel/octane": "^2.3", + "laravel/sanctum": "^4.0", + "laravel/ui": "^4.5", + "webpatser/laravel-uuid": "^4.0", + "predis/predis": "^2.2", + "league/flysystem-aws-s3-v3": "^3.28", + "league/flysystem": "^3.28" }, "require-dev": { "phpunit/phpunit": "^9.5", diff --git a/src/Config/badaso-hidden-tables.php b/src/Config/badaso-hidden-tables.php index 43ba1403d..a13d1e67d 100644 --- a/src/Config/badaso-hidden-tables.php +++ b/src/Config/badaso-hidden-tables.php @@ -26,4 +26,10 @@ 'personal_access_tokens', 'users', 'password_resets', + 'cache', + 'cache_locks', + 'jobs', + 'job_batches', + 'password_reset_tokens', + 'sessions' ]; diff --git a/src/Controllers/BadasoCRUDController.php b/src/Controllers/BadasoCRUDController.php index f7bd9250f..cead5e4d3 100644 --- a/src/Controllers/BadasoCRUDController.php +++ b/src/Controllers/BadasoCRUDController.php @@ -46,18 +46,20 @@ public function browse(Request $request) // end init table watch table $protected_tables = Badaso::getProtectedTables(); - $tables = SchemaManager::listTables(); + // $tables = SchemaManager::listTables(); + $tables = Schema::getTables(); $tables_with_crud_data = []; foreach ($tables as $key => $value) { - if (! in_array($key, $protected_tables)) { + if (! in_array($value["name"], $protected_tables)) { // add table watch config - $config_watch_tables->addWatchTable($key); + $config_watch_tables->addWatchTable($value["name"]); + // end add table watch config $table_with_crud_data = []; - $table_with_crud_data['table_name'] = $key; - $table_with_crud_data['crud_data'] = Badaso::model('DataType')::where('name', $key)->first(); + $table_with_crud_data['table_name'] = $value["name"]; + $table_with_crud_data['crud_data'] = Badaso::model('DataType')::where('name', $value["name"])->first(); $tables_with_crud_data[] = $table_with_crud_data; } } @@ -70,229 +72,230 @@ public function browse(Request $request) } } - public function read(Request $request) - { - try { - $request->validate([ - 'table' => 'required|exists:Uasoft\Badaso\Models\DataType,name', - ]); - - $table = $request->input('table', ''); - $data_type = Badaso::model('DataType')::where('name', $table)->first(); - $data_rows = $this->dataRowsTypeReplace($data_type->dataRows); - - $table_fields = SchemaManager::describeTable($table); - $generated_fields = collect($data_rows)->pluck('field')->toArray(); - - foreach ($table_fields as $key => $column) { - $field = $key; - $column = collect($column)->toArray(); - if (! in_array($field, $generated_fields)) { - $data_row['data_type_id'] = $data_type->id; - $data_row['field'] = $key; - $data_row['type'] = DataTypeToComponent::convert($column['type']); - $data_row['displayName'] = Str::studly($field); - $data_row['required'] = $column['notnull'] ? 1 : 0; - $data_row['browse'] = 1; - $data_row['read'] = 1; - $data_row['edit'] = 0; - $data_row['add'] = 0; - $data_row['delete'] = 0; - $data_row['details'] = '{}'; - $data_row['order'] = null; - - $data_rows[] = $data_row; - } - } - - $data_type->data_rows = $data_rows; - - $data['crud'] = collect($data_type)->toArray(); - - return ApiResponse::success($data); - } catch (Exception $e) { - return APIResponse::failed($e); - } - } - - public function readBySlug(Request $request) - { - try { - $request->validate([ - 'slug' => 'required|exists:Uasoft\Badaso\Models\DataType,slug', - ]); - - $slug = $request->input('slug', ''); - $data_type = Badaso::model('DataType')::where('slug', $slug)->first(); - $data_rows = $this->dataRowsTypeReplace($data_type->dataRows); - - $crud_data = $data_type; - $crud_data->data_rows = collect($data_rows)->toArray(); - - $data['crud_data'] = $crud_data; - - return ApiResponse::success($data); - } catch (Exception $e) { - return APIResponse::failed($e); - } - } - - public function edit(Request $request) - { - $this->addTablePolymorphism($request); - DB::beginTransaction(); - - try { - $request->validate([ - 'id' => 'required|exists:Uasoft\Badaso\Models\DataType', - 'name' => [ - 'required', - "unique:Uasoft\Badaso\Models\DataType,name,{$request->id}", - function ($attribute, $value, $fail) { - if (! Schema::hasTable($value)) { - $fail(__('badaso::validation.crud.table_not_found', ['table' => $value])); - } - }, - ], - 'rows' => 'required', - 'rows.*.field' => [ - 'required', - function ($attribute, $value, $fail) use ($request) { - if (! Schema::hasColumn($request->name, $value)) { - $split_attribute = explode('.', $attribute); - $split_attribute[2] = 'relation_type'; - $field_to_relation = join('.', $split_attribute); - if (! $field_to_relation == 'belongs_to_many') { - $request->{$attribute} == $value ? $value : $fail(__('badaso::validation.crud.table_column_not_found', ['table_column' => "$request->name.{$value}"])); - } - } else { - $table_fields = SchemaManager::describeTable($request->name); - $field = collect($table_fields)->where('field', $value)->first(); - $row = collect($request->rows)->where('field', $value)->first(); - if (! $row['add'] && ! $field['autoincrement'] && $field['notnull'] && is_null($field['default'])) { - $fail(__('badaso::validation.crud.table_column_not_have_default_value', ['table_column' => "$request->name.{$value}"])); - } elseif ($row['field'] != 'id' && $field['key'] == 'PRI') { - $fail(__('badaso::validation.crud.id_table_wrong', ['table_column' => "$request->name.{$value}"])); - } - } - }, - ], - 'rows.*.type' => 'required', - 'rows.*.display_name' => 'required', - 'display_name_singular' => 'required', - 'notification.*.event' => ['in:onCreate,onRead,onUpdate,onDelete'], - 'create_soft_delete' => ['required', 'boolean', function ($att, $val, $failed) use ($request) { - if (isset($request->name) && $val) { - if (! Schema::hasColumn($request->name, 'deleted_at')) { - $failed(__('badaso::validation.crud.table_deleted_at_not_exists', [ - 'table_name' => $request->name, - ])); - } - } - }], - ]); - - $table_name = $request->input('name'); - - $data_type = DataType::find($request->input('id')); - - $data_type->name = $table_name; - $data_type->slug = $request->input('slug') ?? Str::slug($table_name); - $data_type->display_name_singular = $request->input('display_name_singular'); - $data_type->display_name_plural = $request->input('display_name_plural') ?? Str::plural($data_type->display_name_singular); - $data_type->icon = $request->input('icon'); - $data_type->model_name = $request->input('model_name'); - $data_type->policy_name = $request->input('policy_name'); - $data_type->order_column = $request->input('order_column'); - $data_type->order_display_column = $request->input('order_display_column'); - $data_type->order_direction = $request->input('order_direction'); - $data_type->description = $request->input('description'); - $data_type->generate_permissions = $request->input('generate_permissions'); - $data_type->server_side = $request->input('server_side'); - $data_type->details = $request->input('details'); - $data_type->controller = $request->input('controller'); - $data_type->notification = json_encode($request->input('notification')); - $data_type->is_soft_delete = $request->input('create_soft_delete'); - $data_type->save(); - - DataRow::where('data_type_id', $data_type->id)->delete(); - - $data_rows = $request->input('rows') ?? []; - $new_data_rows = []; - foreach ($data_rows as $index => $data_row) { - $new_data_row = new DataRow(); - $new_data_row->data_type_id = $data_type->id; - $new_data_row->field = $data_row['field']; - $new_data_row->type = $data_row['type']; - $new_data_row->display_name = $data_row['display_name']; - $new_data_row->required = isset($data_row['required']) ? $data_row['required'] : false; - $new_data_row->browse = isset($data_row['browse']) ? $data_row['browse'] : false; - $new_data_row->read = isset($data_row['read']) ? $data_row['read'] : false; - $new_data_row->edit = isset($data_row['edit']) ? $data_row['edit'] : false; - $new_data_row->add = isset($data_row['add']) ? $data_row['add'] : false; - $new_data_row->delete = isset($data_row['delete']) ? $data_row['delete'] : false; - $new_data_row->details = isset($data_row['details']) ? $data_row['details'] : ''; - if ($data_row['type'] != 'relation') { - $new_data_row->relation = null; - } else { - $relation = []; - if (isset($data_row['relation_type'])) { - $relation['relation_type'] = $data_row['relation_type']; - } - if (isset($data_row['destination_table'])) { - $relation['destination_table'] = $data_row['destination_table']; - } - if (isset($data_row['destination_table_column'])) { - $relation['destination_table_column'] = $data_row['destination_table_column']; - } - if (isset($data_row['destination_table_display_column'])) { - $relation['destination_table_display_column'] = $data_row['destination_table_display_column']; - } - if (isset($data_row['destination_table_display_more_column'])) { - $relation['destination_table_display_more_column'] = $data_row['destination_table_display_more_column']; - } - if (in_array(count($relation), range(4, 5))) { - $new_data_row->relation = json_encode($relation); - } - } - $new_data_row->order = $index + 1; - $new_data_row->save(); - - $new_data_rows[] = $new_data_row; - } - - if ($data_type->generate_permissions) { - Permission::generateFor($data_type->name, true); - } else { - Permission::removeFrom($data_type->name); - } - - $this->addEditMenuItem($data_type); - - $data_type->data_rows = $new_data_rows; - - event(new CRUDDataUpdated($data_type, null)); - - $this->generateAPIDocs($table_name, $data_rows, $data_type); - DB::commit(); - - activity('CRUD') - ->causedBy(auth()->user() ?? null) - ->withProperties([ - 'old' => $data_type, - 'new' => $request->input(), - ]) - ->performedOn($data_type) - ->event('updated') - ->log('CRUD table '.$data_type->slug.' has been updated'); - - return ApiResponse::success($data_type); - } catch (Exception $e) { - DB::rollBack(); - - return ApiResponse::failed($e); - } - } + // public function read(Request $request) + // { + + // try { + // $request->validate([ + // 'table' => 'required|exists:Uasoft\Badaso\Models\DataType,name', + // ]); + + // $table = $request->input('table', ''); + // $data_type = Badaso::model('DataType')::where('name', $table)->first(); + // $data_rows = $this->dataRowsTypeReplace($data_type->dataRows); + + // $table_fields = SchemaManager::describeTable($table); + // $generated_fields = collect($data_rows)->pluck('field')->toArray(); + + // foreach ($table_fields as $key => $column) { + // $field = $key; + // $column = collect($column)->toArray(); + // if (! in_array($field, $generated_fields)) { + // $data_row['data_type_id'] = $data_type->id; + // $data_row['field'] = $key; + // $data_row['type'] = DataTypeToComponent::convert($column['type']); + // $data_row['displayName'] = Str::studly($field); + // $data_row['required'] = $column['notnull'] ? 1 : 0; + // $data_row['browse'] = 1; + // $data_row['read'] = 1; + // $data_row['edit'] = 0; + // $data_row['add'] = 0; + // $data_row['delete'] = 0; + // $data_row['details'] = '{}'; + // $data_row['order'] = null; + + // $data_rows[] = $data_row; + // } + // } + + // $data_type->data_rows = $data_rows; + + // $data['crud'] = collect($data_type)->toArray(); + + // return ApiResponse::success($data); + // } catch (Exception $e) { + // return APIResponse::failed($e); + // } + // } + + // public function readBySlug(Request $request) + // { + // try { + // $request->validate([ + // 'slug' => 'required|exists:Uasoft\Badaso\Models\DataType,slug', + // ]); + + // $slug = $request->input('slug', ''); + // $data_type = Badaso::model('DataType')::where('slug', $slug)->first(); + // $data_rows = $this->dataRowsTypeReplace($data_type->dataRows); + + // $crud_data = $data_type; + // $crud_data->data_rows = collect($data_rows)->toArray(); + + // $data['crud_data'] = $crud_data; + + // return ApiResponse::success($data); + // } catch (Exception $e) { + // return APIResponse::failed($e); + // } + // } + + // public function edit(Request $request) + // { + // $this->addTablePolymorphism($request); + // DB::beginTransaction(); + + // try { + // $request->validate([ + // 'id' => 'required|exists:Uasoft\Badaso\Models\DataType', + // 'name' => [ + // 'required', + // "unique:Uasoft\Badaso\Models\DataType,name,{$request->id}", + // function ($attribute, $value, $fail) { + // if (! Schema::hasTable($value)) { + // $fail(__('badaso::validation.crud.table_not_found', ['table' => $value])); + // } + // }, + // ], + // 'rows' => 'required', + // 'rows.*.field' => [ + // 'required', + // function ($attribute, $value, $fail) use ($request) { + // if (! Schema::hasColumn($request->name, $value)) { + // $split_attribute = explode('.', $attribute); + // $split_attribute[2] = 'relation_type'; + // $field_to_relation = join('.', $split_attribute); + // if (! $field_to_relation == 'belongs_to_many') { + // $request->{$attribute} == $value ? $value : $fail(__('badaso::validation.crud.table_column_not_found', ['table_column' => "$request->name.{$value}"])); + // } + // } else { + // $table_fields = SchemaManager::describeTable($request->name); + // $field = collect($table_fields)->where('field', $value)->first(); + // $row = collect($request->rows)->where('field', $value)->first(); + // if (! $row['add'] && ! $field['autoincrement'] && $field['notnull'] && is_null($field['default'])) { + // $fail(__('badaso::validation.crud.table_column_not_have_default_value', ['table_column' => "$request->name.{$value}"])); + // } elseif ($row['field'] != 'id' && $field['key'] == 'PRI') { + // $fail(__('badaso::validation.crud.id_table_wrong', ['table_column' => "$request->name.{$value}"])); + // } + // } + // }, + // ], + // 'rows.*.type' => 'required', + // 'rows.*.display_name' => 'required', + // 'display_name_singular' => 'required', + // 'notification.*.event' => ['in:onCreate,onRead,onUpdate,onDelete'], + // 'create_soft_delete' => ['required', 'boolean', function ($att, $val, $failed) use ($request) { + // if (isset($request->name) && $val) { + // if (! Schema::hasColumn($request->name, 'deleted_at')) { + // $failed(__('badaso::validation.crud.table_deleted_at_not_exists', [ + // 'table_name' => $request->name, + // ])); + // } + // } + // }], + // ]); + + // $table_name = $request->input('name'); + + // $data_type = DataType::find($request->input('id')); + + // $data_type->name = $table_name; + // $data_type->slug = $request->input('slug') ?? Str::slug($table_name); + // $data_type->display_name_singular = $request->input('display_name_singular'); + // $data_type->display_name_plural = $request->input('display_name_plural') ?? Str::plural($data_type->display_name_singular); + // $data_type->icon = $request->input('icon'); + // $data_type->model_name = $request->input('model_name'); + // $data_type->policy_name = $request->input('policy_name'); + // $data_type->order_column = $request->input('order_column'); + // $data_type->order_display_column = $request->input('order_display_column'); + // $data_type->order_direction = $request->input('order_direction'); + // $data_type->description = $request->input('description'); + // $data_type->generate_permissions = $request->input('generate_permissions'); + // $data_type->server_side = $request->input('server_side'); + // $data_type->details = $request->input('details'); + // $data_type->controller = $request->input('controller'); + // $data_type->notification = json_encode($request->input('notification')); + // $data_type->is_soft_delete = $request->input('create_soft_delete'); + // $data_type->save(); + + // DataRow::where('data_type_id', $data_type->id)->delete(); + + // $data_rows = $request->input('rows') ?? []; + // $new_data_rows = []; + // foreach ($data_rows as $index => $data_row) { + // $new_data_row = new DataRow(); + // $new_data_row->data_type_id = $data_type->id; + // $new_data_row->field = $data_row['field']; + // $new_data_row->type = $data_row['type']; + // $new_data_row->display_name = $data_row['display_name']; + // $new_data_row->required = isset($data_row['required']) ? $data_row['required'] : false; + // $new_data_row->browse = isset($data_row['browse']) ? $data_row['browse'] : false; + // $new_data_row->read = isset($data_row['read']) ? $data_row['read'] : false; + // $new_data_row->edit = isset($data_row['edit']) ? $data_row['edit'] : false; + // $new_data_row->add = isset($data_row['add']) ? $data_row['add'] : false; + // $new_data_row->delete = isset($data_row['delete']) ? $data_row['delete'] : false; + // $new_data_row->details = isset($data_row['details']) ? $data_row['details'] : ''; + // if ($data_row['type'] != 'relation') { + // $new_data_row->relation = null; + // } else { + // $relation = []; + // if (isset($data_row['relation_type'])) { + // $relation['relation_type'] = $data_row['relation_type']; + // } + // if (isset($data_row['destination_table'])) { + // $relation['destination_table'] = $data_row['destination_table']; + // } + // if (isset($data_row['destination_table_column'])) { + // $relation['destination_table_column'] = $data_row['destination_table_column']; + // } + // if (isset($data_row['destination_table_display_column'])) { + // $relation['destination_table_display_column'] = $data_row['destination_table_display_column']; + // } + // if (isset($data_row['destination_table_display_more_column'])) { + // $relation['destination_table_display_more_column'] = $data_row['destination_table_display_more_column']; + // } + // if (in_array(count($relation), range(4, 5))) { + // $new_data_row->relation = json_encode($relation); + // } + // } + // $new_data_row->order = $index + 1; + // $new_data_row->save(); + + // $new_data_rows[] = $new_data_row; + // } + + // if ($data_type->generate_permissions) { + // Permission::generateFor($data_type->name, true); + // } else { + // Permission::removeFrom($data_type->name); + // } + + // $this->addEditMenuItem($data_type); + + // $data_type->data_rows = $new_data_rows; + + // event(new CRUDDataUpdated($data_type, null)); + + // $this->generateAPIDocs($table_name, $data_rows, $data_type); + // DB::commit(); + + // activity('CRUD') + // ->causedBy(auth()->user() ?? null) + // ->withProperties([ + // 'old' => $data_type, + // 'new' => $request->input(), + // ]) + // ->performedOn($data_type) + // ->event('updated') + // ->log('CRUD table '.$data_type->slug.' has been updated'); + + // return ApiResponse::success($data_type); + // } catch (Exception $e) { + // DB::rollBack(); + + // return ApiResponse::failed($e); + // } + // } public function add(Request $request) { @@ -316,7 +319,7 @@ function ($attribute, $value, $fail) { 'rows.*.field' => [ 'required', function ($attribute, $value, $fail) use ($request) { - if (! Schema::hasColumn($request->name, $value)) { + if (! Schema::getColumns($request->name, $value)) { $split_attribute = explode('.', $attribute); $split_attribute[2] = 'relation_type'; $field_to_relation = join('.', $split_attribute); @@ -439,205 +442,205 @@ function ($attribute, $value, $fail) use ($request) { } } - public function delete(Request $request) - { - DB::beginTransaction(); - - try { - $request->validate([ - 'id' => 'required|exists:Uasoft\Badaso\Models\DataType,id', - ]); - - $data_type = DataType::find($request->id); - - $this->deleteAPIDocs($data_type->name); - - Permission::removeFrom($data_type->name); - - $this->deleteMenuItem($data_type); - - $data_type->delete(); - - event(new CRUDDataDeleted($data_type)); - - DB::commit(); - - activity('CRUD') - ->causedBy(auth()->user() ?? null) - ->withProperties(['attributes' => $data_type]) - ->performedOn($data_type) - ->event('deleted') - ->log('CRUD table '.$data_type->slug.' has been deleted'); - - return ApiResponse::success(); - } catch (Exception $e) { - DB::rollBack(); - - return ApiResponse::failed($e); - } - } - - private function addEditMenuItem($data_type) - { - $menu_key = config('badaso.default_menu'); - $menu = Menu::where('key', $menu_key)->first(); - $url = '/'.$menu_key.'/'.$data_type->slug; - - if (is_null($menu)) { - $menu = new Menu(); - $menu->key = $menu_key; - $menu->display_name = Str::studly($menu_key); - $menu->save(); - } - - $menu_item = MenuItem::firstOrNew([ - 'menu_id' => $menu->id, - 'url' => $url, - ]); - - $menu_item = MenuItem::where('menu_id', $menu->id)->where('url', $url)->first(); - if ($menu_item) { - $menu_item->title = $data_type->display_name_plural; - $menu_item->target = '_self'; - $menu_item->icon_class = $data_type->icon; - $menu_item->color = null; - $menu_item->parent_id = null; - $menu_item->permissions = $data_type->generate_permissions ? 'browse_'.$data_type->name : null; - $menu_item->save(); - } else { - $menu_item = new MenuItem(); - $menu_item->menu_id = $menu->id; - $menu_item->url = $url; - $menu_item->title = $data_type->display_name_plural; - $menu_item->target = '_self'; - $menu_item->icon_class = $data_type->icon; - $menu_item->color = null; - $menu_item->parent_id = null; - $menu_item->permissions = $data_type->generate_permissions ? 'browse_'.$data_type->name : null; - $menu_item->order = $menu_item->highestOrderMenuItem(); - $menu_item->save(); - } - } - - private function deleteMenuItem($data_type) - { - $menu_key = config('badaso.default_menu'); - $url = '/'.$menu_key.'/'.$data_type->slug; - MenuItem::where('url', $url)->delete(); - } - - private function generateAPIDocs($table_name, $data_rows, $data_type) - { - $filesystem = new LaravelFileSystem(); - $file_path = ApiDocs::getFilePath($table_name); - $stub = ApiDocs::getStub($table_name, $data_rows, $data_type); - if (! $filesystem->put($file_path, $stub)) { - return false; - } - - return true; - } - - private function deleteAPIDocs($table_name) - { - $filesystem = new LaravelFileSystem(); - $file_path = ApiDocs::getFilePath($table_name); - if ($filesystem->exists($file_path)) { - return $filesystem->delete($file_path); - } - - return false; - } - - private function addTablePolymorphism($request) - { - foreach ($request['rows'] as $key => $value) { - if (isset($value['relation_type']) && $value['relation_type'] == 'belongs_to_many') { - $table = $value['field']; - $rows = [ - 0 => [ - 'id' => 'id', - 'field_name' => 'id', - 'field_type' => 'bigint', - 'field_length' => null, - 'field_null' => false, - 'field_attribute' => true, - 'field_increment' => true, - 'field_index' => 'primary', - 'field_default' => null, - 'undeletable' => true, - ], - 1 => [ - 'id' => $request['name'].'_id', - 'field_name' => $request['name'].'_id', - 'field_type' => 'bigint', - 'field_length' => null, - 'field_null' => false, - 'field_attribute' => true, - 'field_increment' => false, - 'field_index' => 'foreign', - 'field_default' => null, - ], - 2 => [ - 'id' => $value['destination_table'].'_id', - 'field_name' => $value['destination_table'].'_id', - 'field_type' => 'bigint', - 'field_length' => null, - 'field_null' => false, - 'field_attribute' => true, - 'field_increment' => false, - 'field_index' => 'foreign', - 'field_default' => null, - ], - 3 => [ - 'field_name' => 'created_at', - 'field_type' => 'timestamp', - 'field_length' => null, - 'field_null' => true, - 'field_attribute' => false, - 'field_increment' => false, - 'field_index' => null, - 'field_default' => null, - 'undeletable' => true, - 'indexes' => true, - ], - 4 => [ - 'field_name' => 'updated_at', - 'field_type' => 'timestamp', - 'field_length' => null, - 'field_null' => true, - 'field_attribute' => false, - 'field_increment' => false, - 'field_index' => null, - 'field_default' => null, - 'undeletable' => true, - ], - ]; - - $relations = [ - $request['name'].'_id' => [ - 'source_field' => $request['name'].'_id', - 'target_table' => $request['name'], - 'target_field' => 'id', - 'on_delete' => 'cascade', - 'on_update' => 'restrict', - ], - $value['destination_table'].'_id' => [ - 'source_field' => $value['destination_table'].'_id', - 'target_table' => $value['destination_table'], - 'target_field' => 'id', - 'on_delete' => 'cascade', - 'on_update' => 'restrict', - ], - ]; - if (! Schema::hasTable($table)) { - $this->file_name = $this->file_generator->generateBDOMigrationFile($table, 'create', $rows, $relations); - $exitCode = Artisan::call('migrate', [ - '--path' => 'database/migrations/badaso/', - '--force' => true, - ]); - } - } - } - } + // public function delete(Request $request) + // { + // DB::beginTransaction(); + + // try { + // $request->validate([ + // 'id' => 'required|exists:Uasoft\Badaso\Models\DataType,id', + // ]); + + // $data_type = DataType::find($request->id); + + // $this->deleteAPIDocs($data_type->name); + + // Permission::removeFrom($data_type->name); + + // $this->deleteMenuItem($data_type); + + // $data_type->delete(); + + // event(new CRUDDataDeleted($data_type)); + + // DB::commit(); + + // activity('CRUD') + // ->causedBy(auth()->user() ?? null) + // ->withProperties(['attributes' => $data_type]) + // ->performedOn($data_type) + // ->event('deleted') + // ->log('CRUD table '.$data_type->slug.' has been deleted'); + + // return ApiResponse::success(); + // } catch (Exception $e) { + // DB::rollBack(); + + // return ApiResponse::failed($e); + // } + // } + + // private function addEditMenuItem($data_type) + // { + // $menu_key = config('badaso.default_menu'); + // $menu = Menu::where('key', $menu_key)->first(); + // $url = '/'.$menu_key.'/'.$data_type->slug; + + // if (is_null($menu)) { + // $menu = new Menu(); + // $menu->key = $menu_key; + // $menu->display_name = Str::studly($menu_key); + // $menu->save(); + // } + + // $menu_item = MenuItem::firstOrNew([ + // 'menu_id' => $menu->id, + // 'url' => $url, + // ]); + + // $menu_item = MenuItem::where('menu_id', $menu->id)->where('url', $url)->first(); + // if ($menu_item) { + // $menu_item->title = $data_type->display_name_plural; + // $menu_item->target = '_self'; + // $menu_item->icon_class = $data_type->icon; + // $menu_item->color = null; + // $menu_item->parent_id = null; + // $menu_item->permissions = $data_type->generate_permissions ? 'browse_'.$data_type->name : null; + // $menu_item->save(); + // } else { + // $menu_item = new MenuItem(); + // $menu_item->menu_id = $menu->id; + // $menu_item->url = $url; + // $menu_item->title = $data_type->display_name_plural; + // $menu_item->target = '_self'; + // $menu_item->icon_class = $data_type->icon; + // $menu_item->color = null; + // $menu_item->parent_id = null; + // $menu_item->permissions = $data_type->generate_permissions ? 'browse_'.$data_type->name : null; + // $menu_item->order = $menu_item->highestOrderMenuItem(); + // $menu_item->save(); + // } + // } + + // private function deleteMenuItem($data_type) + // { + // $menu_key = config('badaso.default_menu'); + // $url = '/'.$menu_key.'/'.$data_type->slug; + // MenuItem::where('url', $url)->delete(); + // } + + // private function generateAPIDocs($table_name, $data_rows, $data_type) + // { + // $filesystem = new LaravelFileSystem(); + // $file_path = ApiDocs::getFilePath($table_name); + // $stub = ApiDocs::getStub($table_name, $data_rows, $data_type); + // if (! $filesystem->put($file_path, $stub)) { + // return false; + // } + + // return true; + // } + + // private function deleteAPIDocs($table_name) + // { + // $filesystem = new LaravelFileSystem(); + // $file_path = ApiDocs::getFilePath($table_name); + // if ($filesystem->exists($file_path)) { + // return $filesystem->delete($file_path); + // } + + // return false; + // } + + // private function addTablePolymorphism($request) + // { + // foreach ($request['rows'] as $key => $value) { + // if (isset($value['relation_type']) && $value['relation_type'] == 'belongs_to_many') { + // $table = $value['field']; + // $rows = [ + // 0 => [ + // 'id' => 'id', + // 'field_name' => 'id', + // 'field_type' => 'bigint', + // 'field_length' => null, + // 'field_null' => false, + // 'field_attribute' => true, + // 'field_increment' => true, + // 'field_index' => 'primary', + // 'field_default' => null, + // 'undeletable' => true, + // ], + // 1 => [ + // 'id' => $request['name'].'_id', + // 'field_name' => $request['name'].'_id', + // 'field_type' => 'bigint', + // 'field_length' => null, + // 'field_null' => false, + // 'field_attribute' => true, + // 'field_increment' => false, + // 'field_index' => 'foreign', + // 'field_default' => null, + // ], + // 2 => [ + // 'id' => $value['destination_table'].'_id', + // 'field_name' => $value['destination_table'].'_id', + // 'field_type' => 'bigint', + // 'field_length' => null, + // 'field_null' => false, + // 'field_attribute' => true, + // 'field_increment' => false, + // 'field_index' => 'foreign', + // 'field_default' => null, + // ], + // 3 => [ + // 'field_name' => 'created_at', + // 'field_type' => 'timestamp', + // 'field_length' => null, + // 'field_null' => true, + // 'field_attribute' => false, + // 'field_increment' => false, + // 'field_index' => null, + // 'field_default' => null, + // 'undeletable' => true, + // 'indexes' => true, + // ], + // 4 => [ + // 'field_name' => 'updated_at', + // 'field_type' => 'timestamp', + // 'field_length' => null, + // 'field_null' => true, + // 'field_attribute' => false, + // 'field_increment' => false, + // 'field_index' => null, + // 'field_default' => null, + // 'undeletable' => true, + // ], + // ]; + + // $relations = [ + // $request['name'].'_id' => [ + // 'source_field' => $request['name'].'_id', + // 'target_table' => $request['name'], + // 'target_field' => 'id', + // 'on_delete' => 'cascade', + // 'on_update' => 'restrict', + // ], + // $value['destination_table'].'_id' => [ + // 'source_field' => $value['destination_table'].'_id', + // 'target_table' => $value['destination_table'], + // 'target_field' => 'id', + // 'on_delete' => 'cascade', + // 'on_update' => 'restrict', + // ], + // ]; + // if (! Schema::hasTable($table)) { + // $this->file_name = $this->file_generator->generateBDOMigrationFile($table, 'create', $rows, $relations); + // $exitCode = Artisan::call('migrate', [ + // '--path' => 'database/migrations/badaso/', + // '--force' => true, + // ]); + // } + // } + // } + // } } diff --git a/src/Controllers/BadasoTableController.php b/src/Controllers/BadasoTableController.php index 52863f246..5f62001ab 100644 --- a/src/Controllers/BadasoTableController.php +++ b/src/Controllers/BadasoTableController.php @@ -22,12 +22,13 @@ class BadasoTableController extends Controller public function browse(Request $request) { try { - $tables = SchemaManager::listTables(); + // $tables = SchemaManager::listTables(); + $tables = Schema::getTables(); $custom_list = []; foreach ($tables as $key => $value) { $table = []; - $table['value'] = $key; - $table['label'] = ucfirst(str_replace('_', ' ', $key)); + $table['value'] = $value["name"]; + $table['label'] = ucfirst(str_replace('_', ' ', $value["name"])); $custom_list[] = $table; } @@ -49,6 +50,8 @@ public function read(Request $request) $table = $request->table; $table_fields = SchemaManager::describeTable($table); + // $table_fields = Schema::getIndexes($table); + // dd($table_fields); $fields = []; foreach ($table_fields as $key => $column) { $column = collect($column)->toArray(); diff --git a/src/Database/Schema/SchemaManager.php b/src/Database/Schema/SchemaManager.php index bc30798f7..272ef15bc 100644 --- a/src/Database/Schema/SchemaManager.php +++ b/src/Database/Schema/SchemaManager.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Schema\Table as DoctrineTable; use Illuminate\Support\Facades\DB; use Uasoft\Badaso\Database\Types\Type; +use Illuminate\Support\Facades\Schema; abstract class SchemaManager { @@ -18,7 +19,8 @@ public static function __callStatic($method, $args) public static function manager() { - return DB::connection()->getDoctrineSchemaManager(); + // return Schema::connection(); + // return DB::connection()->getDoctrineSchemaManager(); } public static function getDatabaseConnection() @@ -52,14 +54,16 @@ public static function listTables() */ public static function listTableDetails($table_name) { - $columns = static::manager()->listTableColumns($table_name); + // $columns = static::manager()->listTableColumns($table_name); + $columns = Schema::getColumns($table_name); $foreign_keys = []; - if (static::manager()->getDatabasePlatform()->supportsForeignKeyConstraints()) { - $foreign_keys = static::manager()->listTableForeignKeys($table_name); - } + // if (static::manager()->getDatabasePlatform()->supportsForeignKeyConstraints()) { + // $foreign_keys = Schema::getForeignKeys($table_name); + // } + $foreign_keys = Schema::getForeignKeys($table_name); - $indexes = static::manager()->listTableIndexes($table_name); + $indexes = Schema::getIndexes($table_name); return new Table($table_name, $columns, $indexes, $foreign_keys, false, []); } diff --git a/src/Providers/BadasoServiceProvider.php b/src/Providers/BadasoServiceProvider.php index b6364829e..582ffda29 100644 --- a/src/Providers/BadasoServiceProvider.php +++ b/src/Providers/BadasoServiceProvider.php @@ -6,7 +6,7 @@ use Illuminate\Foundation\AliasLoader; use Illuminate\Support\ServiceProvider; use L5Swagger\L5SwaggerServiceProvider; -use Larapack\DoctrineSupport\DoctrineSupportServiceProvider; +use LaravelDoctrine\ORM\DoctrineServiceProvider; use Uasoft\Badaso\Badaso; use Uasoft\Badaso\Commands\AdminCommand; use Uasoft\Badaso\Commands\BackupCommand; @@ -99,7 +99,7 @@ public function boot() public function register() { $this->app->register(OrchestratorEventServiceProvider::class); - $this->app->register(DoctrineSupportServiceProvider::class); + $this->app->register(DoctrineServiceProvider::class); $this->app->register(DropboxServiceProvider::class); $this->app->register(GoogleDriveServiceProvider::class); $this->app->register(LogViewerServiceProvider::class); From c2a82184c207fc51e458150f6b799eef35620de6 Mon Sep 17 00:00:00 2001 From: Miftahurrahmi Date: Mon, 3 Jun 2024 17:03:09 +0700 Subject: [PATCH 02/10] trying fix schema class --- src/Controllers/BadasoTableController.php | 3 +-- src/Database/Schema/SchemaManager.php | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Controllers/BadasoTableController.php b/src/Controllers/BadasoTableController.php index 5f62001ab..c3a408932 100644 --- a/src/Controllers/BadasoTableController.php +++ b/src/Controllers/BadasoTableController.php @@ -47,11 +47,10 @@ public function read(Request $request) $request->validate([ 'table' => 'required', ]); - $table = $request->table; $table_fields = SchemaManager::describeTable($table); + // $table_fields = Schema::getIndexes($table); - // dd($table_fields); $fields = []; foreach ($table_fields as $key => $column) { $column = collect($column)->toArray(); diff --git a/src/Database/Schema/SchemaManager.php b/src/Database/Schema/SchemaManager.php index 272ef15bc..7a5790152 100644 --- a/src/Database/Schema/SchemaManager.php +++ b/src/Database/Schema/SchemaManager.php @@ -20,7 +20,7 @@ public static function __callStatic($method, $args) public static function manager() { // return Schema::connection(); - // return DB::connection()->getDoctrineSchemaManager(); + return DB::connection()->getDoctrineSchemaManager(); } public static function getDatabaseConnection() @@ -76,7 +76,7 @@ public static function listTableDetails($table_name) */ public static function describeTable($table_name) { - Type::registerCustomPlatformTypes(); + // Type::registerCustomPlatformTypes(); $table = static::listTableDetails($table_name); From 3fc42fe90563cc95f05610ca989de9d1826de778 Mon Sep 17 00:00:00 2001 From: miftahurrahmi Date: Fri, 7 Jun 2024 17:03:50 +0700 Subject: [PATCH 03/10] update class schema --- src/Controllers/BadasoTableController.php | 2 - src/Database/Schema/SchemaManager.php | 122 +++++++++++----------- src/Database/Types/Type.php | 92 +++++++++++----- 3 files changed, 131 insertions(+), 85 deletions(-) diff --git a/src/Controllers/BadasoTableController.php b/src/Controllers/BadasoTableController.php index c3a408932..8088792cf 100644 --- a/src/Controllers/BadasoTableController.php +++ b/src/Controllers/BadasoTableController.php @@ -49,8 +49,6 @@ public function read(Request $request) ]); $table = $request->table; $table_fields = SchemaManager::describeTable($table); - - // $table_fields = Schema::getIndexes($table); $fields = []; foreach ($table_fields as $key => $column) { $column = collect($column)->toArray(); diff --git a/src/Database/Schema/SchemaManager.php b/src/Database/Schema/SchemaManager.php index 7a5790152..519da72f1 100644 --- a/src/Database/Schema/SchemaManager.php +++ b/src/Database/Schema/SchemaManager.php @@ -2,78 +2,65 @@ namespace Uasoft\Badaso\Database\Schema; -use Doctrine\DBAL\Schema\SchemaException; -use Doctrine\DBAL\Schema\Table as DoctrineTable; +use Doctrine\DBAL\Schema\Table; +use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\DB; -use Uasoft\Badaso\Database\Types\Type; use Illuminate\Support\Facades\Schema; +use Uasoft\Badaso\Database\Types\Type; abstract class SchemaManager { - // todo: trim parameters - public static function __callStatic($method, $args) { - return static::manager()->$method(...$args); - } - - public static function manager() - { - // return Schema::connection(); - return DB::connection()->getDoctrineSchemaManager(); - } - - public static function getDatabaseConnection() - { - return DB::connection()->getDoctrineConnection(); + return Schema::$method(...$args); } public static function tableExists($table) { - if (! is_array($table)) { + if (!is_array($table)) { $table = [$table]; } - return static::manager()->tablesExist($table); + foreach ($table as $tbl) { + if (!Schema::hasTable($tbl)) { + return false; + } + } + + return true; } public static function listTables() { - $tables = []; - - foreach (static::manager()->listTableNames() as $table_name) { - $tables[$table_name] = static::listTableDetails($table_name); + $tables = DB::select('SHOW TABLES'); + $database_name = DB::getDatabaseName(); + $tables_key = "Tables_in_$database_name"; + + $result = []; + foreach ($tables as $table) { + $table_name = $table->$tables_key; + $result[$table_name] = static::listTableDetails($table_name); } - return $tables; + return $result; } - /** - * @param string $table_name - * @return \Uasoft\Badaso\Database\Schema\Table - */ public static function listTableDetails($table_name) { - // $columns = static::manager()->listTableColumns($table_name); - $columns = Schema::getColumns($table_name); - - $foreign_keys = []; - // if (static::manager()->getDatabasePlatform()->supportsForeignKeyConstraints()) { - // $foreign_keys = Schema::getForeignKeys($table_name); - // } - $foreign_keys = Schema::getForeignKeys($table_name); + $columns = Schema::getColumnListing($table_name); - $indexes = Schema::getIndexes($table_name); + // $foreign_keys = static::listTableForeignKeys($table_name); + $foreign_keys[] = Schema::getForeignKeys($table_name); + // $indexes = Schema::listTableIndexes($table_name); + $indexes[] = Schema::getIndexes($table_name); + return new Table($table_name, $columns, $indexes, $foreign_keys, []); + } - return new Table($table_name, $columns, $indexes, $foreign_keys, false, []); + public static function getColumnDetails($table_name, $column_name) + { + return DB::select(DB::raw("SHOW COLUMNS FROM `$table_name` LIKE '$column_name'"))[0]; } - /** - * Describes given table. - * - * @param string $table_name - * @return \Illuminate\Support\Collection - */ public static function describeTable($table_name) { // Type::registerCustomPlatformTypes(); @@ -109,42 +96,59 @@ public static function listTableColumnNames($table_name) { Type::registerCustomPlatformTypes(); - $column_names = []; - - foreach (static::manager()->listTableColumns($table_name) as $column) { - $column_names[] = $column->getName(); - } - - return $column_names; + return Schema::getColumnListing($table_name); } public static function createTable($table) { - if (! ($table instanceof DoctrineTable)) { + if (!($table instanceof Blueprint)) { $table = Table::make($table); } - static::manager()->createTable($table); + Schema::create($table->getTable(), function (Blueprint $blueprint) use ($table) { + foreach ($table->getColumns() as $column) { + $blueprint->addColumn($column->getType(), $column->getName(), $column->getOptions()); + } + + foreach ($table->getIndexes() as $index) { + $blueprint->index($index->getColumns(), $index->getName(), $index->getOptions()); + } + + foreach ($table->getForeignKeys() as $foreignKey) { + $blueprint->foreign($foreignKey->getLocalColumns()) + ->references($foreignKey->getForeignColumns()) + ->on($foreignKey->getForeignTable()) + ->onDelete($foreignKey->onDelete()) + ->onUpdate($foreignKey->onUpdate()); + } + }); } public static function getDoctrineTable($table) { $table = trim($table); - if (! static::tableExists($table)) { - throw SchemaException::tableDoesNotExist($table); + if (!static::tableExists($table)) { + throw new \Exception("Table $table does not exist."); } - return static::manager()->listTableDetails($table); + return static::listTableDetails($table); } public static function getDoctrineColumn($table, $column) { - return static::getDoctrineTable($table)->getColumn($column); + return static::getColumnDetails($table, $column); + } + + public static function listTableForeignKeys($table_name) + { + $foreignKeys = DB::select(DB::raw("SELECT * FROM information_schema.key_column_usage WHERE TABLE_NAME = '$table_name' AND TABLE_SCHEMA = '" . DB::getDatabaseName() . "' AND REFERENCED_COLUMN_NAME IS NOT NULL")); + return $foreignKeys; } - public static function getDoctrineForeignKeys($table) + public static function listTableIndexes($table_name) { - return static::manager()->listTableForeignKeys($table); + $indexes = DB::select(DB::raw("SHOW INDEX FROM `$table_name`")); + return $indexes; } } diff --git a/src/Database/Types/Type.php b/src/Database/Types/Type.php index f6c64c6e4..5fe72aa04 100644 --- a/src/Database/Types/Type.php +++ b/src/Database/Types/Type.php @@ -2,10 +2,10 @@ namespace Uasoft\Badaso\Database\Types; -use Doctrine\DBAL\Platforms\AbstractPlatform as DoctrineAbstractPlatform; -use Doctrine\DBAL\Types\Type as DoctrineType; +use Illuminate\Support\Facades\DB; use Uasoft\Badaso\Database\Platforms\Platform; -use Uasoft\Badaso\Database\Schema\SchemaManager; +use Doctrine\DBAL\Types\Type as DoctrineType; +use Doctrine\DBAL\Platforms\AbstractPlatform as DoctrineAbstractPlatform; abstract class Type extends DoctrineType { @@ -15,6 +15,7 @@ abstract class Type extends DoctrineType protected static $platform_types = []; protected static $custom_type_options = []; protected static $type_categories = []; + protected static $types = []; const NAME = 'UNDEFINED_TYPE_NAME'; const NOT_SUPPORTED = 'notSupported'; @@ -29,7 +30,7 @@ public function getName() return static::NAME; } - public static function toArray(DoctrineType $type) + public static function toArray($type) { $custom_type_options = $type->customOptions ?? []; @@ -44,15 +45,15 @@ public static function getPlatformTypes() return static::$platform_types; } - if (! static::$custom_type_registered) { + if (!static::$custom_type_registered) { static::registerCustomPlatformTypes(); } - $platform = SchemaManager::getDatabasePlatform(); + $platform_name = DB::getDriverName(); static::$platform_types = Platform::getPlatformTypes( - $platform->getName(), - static::getPlatformTypeMapping($platform) + $platform_name, + static::getPlatformTypeMapping($platform_name) ); static::$platform_types = static::$platform_types->map(function ($type) { @@ -62,14 +63,15 @@ public static function getPlatformTypes() return static::$platform_types; } - public static function getPlatformTypeMapping(DoctrineAbstractPlatform $platform) + public static function getPlatformTypeMapping(DoctrineAbstractPlatform $platform_name) { if (static::$platform_type_mapping) { return static::$platform_type_mapping; } + // Anda perlu mendefinisikan cara mengambil pemetaan tipe khusus untuk platform tertentu static::$platform_type_mapping = collect( - get_protected_property($platform, 'doctrineTypeMapping') + get_protected_property($platform_name, 'doctrineTypeMapping') ); return static::$platform_type_mapping; @@ -77,30 +79,29 @@ public static function getPlatformTypeMapping(DoctrineAbstractPlatform $platform public static function registerCustomPlatformTypes($force = false) { - if (static::$custom_type_registered && ! $force) { + if (static::$custom_type_registered && !$force) { return; } - $platform = SchemaManager::getDatabasePlatform(); - $platform_name = ucfirst($platform->getName()); - + $platform_name = DB::getDriverName(); + $custom_types = array_merge( static::getPlatformCustomTypes('Common'), static::getPlatformCustomTypes($platform_name) ); - + foreach ($custom_types as $type) { $name = $type::NAME; - if (static::hasType($name)) { - static::overrideType($name, $type); - } else { + if (!static::hasType($name)) { static::addType($name, $type); + } else { + static::overrideType($name, $type); } $db_type = defined("{$type}::DBTYPE") ? $type::DBTYPE : $name; - - $platform->registerDoctrineTypeMapping($db_type, $name); + + static::registerDoctrineTypeMapping($db_type, $name); } static::addCustomTypeOptions($platform_name); @@ -108,6 +109,29 @@ public static function registerCustomPlatformTypes($force = false) static::$custom_type_registered = true; } + protected static function registerDoctrineTypeMapping($db_type, $name) + { + $databaseConfig = config('database.connections.' . config('database.default')); + + $connectionParams = [ + 'dbname' => $databaseConfig['database'], + 'user' => $databaseConfig['username'], + 'password' => $databaseConfig['password'], + 'host' => $databaseConfig['host'], + 'driver' => 'pdo_mysql', // Sesuaikan dengan driver yang digunakan, misal: 'pdo_pgsql' untuk PostgreSQL + 'port' => $databaseConfig['port'], + ]; + + $doctrine_connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams); + $platform = $doctrine_connection->getDatabasePlatform(); + + if ($platform->hasDoctrineTypeMappingFor($db_type)) { + $platform->registerDoctrineTypeMapping($db_type, $name); + } else { + throw new \Doctrine\DBAL\Exception("Type to be overwritten $db_type does not exist."); + } + } + protected static function addCustomTypeOptions($platform_name) { static::registerCommonCustomTypeOptions(); @@ -126,12 +150,12 @@ protected static function addCustomTypeOptions($platform_name) protected static function getPlatformCustomTypes($platform_name) { - $types_path = __DIR__.DIRECTORY_SEPARATOR.$platform_name.DIRECTORY_SEPARATOR; - $namespace = __NAMESPACE__.'\\'.$platform_name.'\\'; + $types_path = __DIR__ . DIRECTORY_SEPARATOR . $platform_name . DIRECTORY_SEPARATOR; + $namespace = __NAMESPACE__ . '\\' . $platform_name . '\\'; $types = []; - foreach (glob($types_path.'*.php') as $class_file) { - $types[] = $namespace.str_replace( + foreach (glob($types_path . '*.php') as $class_file) { + $types[] = $namespace . str_replace( '.php', '', str_replace($types_path, '', $class_file) @@ -330,4 +354,24 @@ public static function getTypeCategories() return static::$type_categories; } + + public static function hasType($name) + { + return DoctrineType::hasType($name); + } + + public static function getType($name) + { + return static::$types[$name] ?? null; + } + + public static function addType($name, $type) + { + static::$types[$name] = new $type(); + } + + public static function overrideType($name, $type) + { + static::$types[$name] = new $type(); + } } From 872dc529d99bfc30be0c783ae919c6b088f4553e Mon Sep 17 00:00:00 2001 From: miftahurrahmi Date: Thu, 27 Jun 2024 09:17:02 +0700 Subject: [PATCH 04/10] update badaso v2 laravel 11 --- src/Controllers/BadasoCRUDController.php | 875 +++++++++---------- src/Controllers/BadasoDatabaseController.php | 1 - src/Controllers/BadasoTableController.php | 7 +- src/Database/Schema/SchemaManager.php | 153 ++-- src/Database/Types/Type.php | 81 +- 5 files changed, 551 insertions(+), 566 deletions(-) diff --git a/src/Controllers/BadasoCRUDController.php b/src/Controllers/BadasoCRUDController.php index cead5e4d3..6dffcc1ee 100644 --- a/src/Controllers/BadasoCRUDController.php +++ b/src/Controllers/BadasoCRUDController.php @@ -46,20 +46,18 @@ public function browse(Request $request) // end init table watch table $protected_tables = Badaso::getProtectedTables(); - // $tables = SchemaManager::listTables(); - $tables = Schema::getTables(); - + $tables = SchemaManager::listTables(); $tables_with_crud_data = []; foreach ($tables as $key => $value) { - if (! in_array($value["name"], $protected_tables)) { + + if (!in_array($key, $protected_tables)) { // add table watch config - $config_watch_tables->addWatchTable($value["name"]); - + $config_watch_tables->addWatchTable($key); // end add table watch config $table_with_crud_data = []; - $table_with_crud_data['table_name'] = $value["name"]; - $table_with_crud_data['crud_data'] = Badaso::model('DataType')::where('name', $value["name"])->first(); + $table_with_crud_data['table_name'] = $key; + $table_with_crud_data['crud_data'] = Badaso::model('DataType')::where('name', $key)->first(); $tables_with_crud_data[] = $table_with_crud_data; } } @@ -72,230 +70,229 @@ public function browse(Request $request) } } - // public function read(Request $request) - // { - - // try { - // $request->validate([ - // 'table' => 'required|exists:Uasoft\Badaso\Models\DataType,name', - // ]); - - // $table = $request->input('table', ''); - // $data_type = Badaso::model('DataType')::where('name', $table)->first(); - // $data_rows = $this->dataRowsTypeReplace($data_type->dataRows); - - // $table_fields = SchemaManager::describeTable($table); - // $generated_fields = collect($data_rows)->pluck('field')->toArray(); - - // foreach ($table_fields as $key => $column) { - // $field = $key; - // $column = collect($column)->toArray(); - // if (! in_array($field, $generated_fields)) { - // $data_row['data_type_id'] = $data_type->id; - // $data_row['field'] = $key; - // $data_row['type'] = DataTypeToComponent::convert($column['type']); - // $data_row['displayName'] = Str::studly($field); - // $data_row['required'] = $column['notnull'] ? 1 : 0; - // $data_row['browse'] = 1; - // $data_row['read'] = 1; - // $data_row['edit'] = 0; - // $data_row['add'] = 0; - // $data_row['delete'] = 0; - // $data_row['details'] = '{}'; - // $data_row['order'] = null; - - // $data_rows[] = $data_row; - // } - // } - - // $data_type->data_rows = $data_rows; - - // $data['crud'] = collect($data_type)->toArray(); - - // return ApiResponse::success($data); - // } catch (Exception $e) { - // return APIResponse::failed($e); - // } - // } - - // public function readBySlug(Request $request) - // { - // try { - // $request->validate([ - // 'slug' => 'required|exists:Uasoft\Badaso\Models\DataType,slug', - // ]); - - // $slug = $request->input('slug', ''); - // $data_type = Badaso::model('DataType')::where('slug', $slug)->first(); - // $data_rows = $this->dataRowsTypeReplace($data_type->dataRows); - - // $crud_data = $data_type; - // $crud_data->data_rows = collect($data_rows)->toArray(); - - // $data['crud_data'] = $crud_data; - - // return ApiResponse::success($data); - // } catch (Exception $e) { - // return APIResponse::failed($e); - // } - // } - - // public function edit(Request $request) - // { - // $this->addTablePolymorphism($request); - // DB::beginTransaction(); - - // try { - // $request->validate([ - // 'id' => 'required|exists:Uasoft\Badaso\Models\DataType', - // 'name' => [ - // 'required', - // "unique:Uasoft\Badaso\Models\DataType,name,{$request->id}", - // function ($attribute, $value, $fail) { - // if (! Schema::hasTable($value)) { - // $fail(__('badaso::validation.crud.table_not_found', ['table' => $value])); - // } - // }, - // ], - // 'rows' => 'required', - // 'rows.*.field' => [ - // 'required', - // function ($attribute, $value, $fail) use ($request) { - // if (! Schema::hasColumn($request->name, $value)) { - // $split_attribute = explode('.', $attribute); - // $split_attribute[2] = 'relation_type'; - // $field_to_relation = join('.', $split_attribute); - // if (! $field_to_relation == 'belongs_to_many') { - // $request->{$attribute} == $value ? $value : $fail(__('badaso::validation.crud.table_column_not_found', ['table_column' => "$request->name.{$value}"])); - // } - // } else { - // $table_fields = SchemaManager::describeTable($request->name); - // $field = collect($table_fields)->where('field', $value)->first(); - // $row = collect($request->rows)->where('field', $value)->first(); - // if (! $row['add'] && ! $field['autoincrement'] && $field['notnull'] && is_null($field['default'])) { - // $fail(__('badaso::validation.crud.table_column_not_have_default_value', ['table_column' => "$request->name.{$value}"])); - // } elseif ($row['field'] != 'id' && $field['key'] == 'PRI') { - // $fail(__('badaso::validation.crud.id_table_wrong', ['table_column' => "$request->name.{$value}"])); - // } - // } - // }, - // ], - // 'rows.*.type' => 'required', - // 'rows.*.display_name' => 'required', - // 'display_name_singular' => 'required', - // 'notification.*.event' => ['in:onCreate,onRead,onUpdate,onDelete'], - // 'create_soft_delete' => ['required', 'boolean', function ($att, $val, $failed) use ($request) { - // if (isset($request->name) && $val) { - // if (! Schema::hasColumn($request->name, 'deleted_at')) { - // $failed(__('badaso::validation.crud.table_deleted_at_not_exists', [ - // 'table_name' => $request->name, - // ])); - // } - // } - // }], - // ]); - - // $table_name = $request->input('name'); - - // $data_type = DataType::find($request->input('id')); - - // $data_type->name = $table_name; - // $data_type->slug = $request->input('slug') ?? Str::slug($table_name); - // $data_type->display_name_singular = $request->input('display_name_singular'); - // $data_type->display_name_plural = $request->input('display_name_plural') ?? Str::plural($data_type->display_name_singular); - // $data_type->icon = $request->input('icon'); - // $data_type->model_name = $request->input('model_name'); - // $data_type->policy_name = $request->input('policy_name'); - // $data_type->order_column = $request->input('order_column'); - // $data_type->order_display_column = $request->input('order_display_column'); - // $data_type->order_direction = $request->input('order_direction'); - // $data_type->description = $request->input('description'); - // $data_type->generate_permissions = $request->input('generate_permissions'); - // $data_type->server_side = $request->input('server_side'); - // $data_type->details = $request->input('details'); - // $data_type->controller = $request->input('controller'); - // $data_type->notification = json_encode($request->input('notification')); - // $data_type->is_soft_delete = $request->input('create_soft_delete'); - // $data_type->save(); - - // DataRow::where('data_type_id', $data_type->id)->delete(); - - // $data_rows = $request->input('rows') ?? []; - // $new_data_rows = []; - // foreach ($data_rows as $index => $data_row) { - // $new_data_row = new DataRow(); - // $new_data_row->data_type_id = $data_type->id; - // $new_data_row->field = $data_row['field']; - // $new_data_row->type = $data_row['type']; - // $new_data_row->display_name = $data_row['display_name']; - // $new_data_row->required = isset($data_row['required']) ? $data_row['required'] : false; - // $new_data_row->browse = isset($data_row['browse']) ? $data_row['browse'] : false; - // $new_data_row->read = isset($data_row['read']) ? $data_row['read'] : false; - // $new_data_row->edit = isset($data_row['edit']) ? $data_row['edit'] : false; - // $new_data_row->add = isset($data_row['add']) ? $data_row['add'] : false; - // $new_data_row->delete = isset($data_row['delete']) ? $data_row['delete'] : false; - // $new_data_row->details = isset($data_row['details']) ? $data_row['details'] : ''; - // if ($data_row['type'] != 'relation') { - // $new_data_row->relation = null; - // } else { - // $relation = []; - // if (isset($data_row['relation_type'])) { - // $relation['relation_type'] = $data_row['relation_type']; - // } - // if (isset($data_row['destination_table'])) { - // $relation['destination_table'] = $data_row['destination_table']; - // } - // if (isset($data_row['destination_table_column'])) { - // $relation['destination_table_column'] = $data_row['destination_table_column']; - // } - // if (isset($data_row['destination_table_display_column'])) { - // $relation['destination_table_display_column'] = $data_row['destination_table_display_column']; - // } - // if (isset($data_row['destination_table_display_more_column'])) { - // $relation['destination_table_display_more_column'] = $data_row['destination_table_display_more_column']; - // } - // if (in_array(count($relation), range(4, 5))) { - // $new_data_row->relation = json_encode($relation); - // } - // } - // $new_data_row->order = $index + 1; - // $new_data_row->save(); - - // $new_data_rows[] = $new_data_row; - // } - - // if ($data_type->generate_permissions) { - // Permission::generateFor($data_type->name, true); - // } else { - // Permission::removeFrom($data_type->name); - // } - - // $this->addEditMenuItem($data_type); - - // $data_type->data_rows = $new_data_rows; - - // event(new CRUDDataUpdated($data_type, null)); - - // $this->generateAPIDocs($table_name, $data_rows, $data_type); - // DB::commit(); - - // activity('CRUD') - // ->causedBy(auth()->user() ?? null) - // ->withProperties([ - // 'old' => $data_type, - // 'new' => $request->input(), - // ]) - // ->performedOn($data_type) - // ->event('updated') - // ->log('CRUD table '.$data_type->slug.' has been updated'); - - // return ApiResponse::success($data_type); - // } catch (Exception $e) { - // DB::rollBack(); - - // return ApiResponse::failed($e); - // } - // } + public function read(Request $request) + { + try { + $request->validate([ + 'table' => 'required|exists:Uasoft\Badaso\Models\DataType,name', + ]); + + $table = $request->input('table', ''); + $data_type = Badaso::model('DataType')::where('name', $table)->first(); + $data_rows = $this->dataRowsTypeReplace($data_type->dataRows); + + $table_fields = SchemaManager::describeTable($table); + $generated_fields = collect($data_rows)->pluck('field')->toArray(); + + foreach ($table_fields as $key => $column) { + $field = $key; + $column = collect($column)->toArray(); + if (!in_array($field, $generated_fields)) { + $data_row['data_type_id'] = $data_type->id; + $data_row['field'] = $key; + $data_row['type'] = DataTypeToComponent::convert($column['type']); + $data_row['displayName'] = Str::studly($field); + $data_row['required'] = $column['notnull'] ? 1 : 0; + $data_row['browse'] = 1; + $data_row['read'] = 1; + $data_row['edit'] = 0; + $data_row['add'] = 0; + $data_row['delete'] = 0; + $data_row['details'] = '{}'; + $data_row['order'] = null; + + $data_rows[] = $data_row; + } + } + + $data_type->data_rows = $data_rows; + + $data['crud'] = collect($data_type)->toArray(); + + return ApiResponse::success($data); + } catch (Exception $e) { + return APIResponse::failed($e); + } + } + + public function readBySlug(Request $request) + { + try { + $request->validate([ + 'slug' => 'required|exists:Uasoft\Badaso\Models\DataType,slug', + ]); + + $slug = $request->input('slug', ''); + $data_type = Badaso::model('DataType')::where('slug', $slug)->first(); + $data_rows = $this->dataRowsTypeReplace($data_type->dataRows); + + $crud_data = $data_type; + $crud_data->data_rows = collect($data_rows)->toArray(); + + $data['crud_data'] = $crud_data; + + return ApiResponse::success($data); + } catch (Exception $e) { + return APIResponse::failed($e); + } + } + + public function edit(Request $request) + { + $this->addTablePolymorphism($request); + DB::beginTransaction(); + + try { + $request->validate([ + 'id' => 'required|exists:Uasoft\Badaso\Models\DataType', + 'name' => [ + 'required', + "unique:Uasoft\Badaso\Models\DataType,name,{$request->id}", + function ($attribute, $value, $fail) { + if (!Schema::hasTable($value)) { + $fail(__('badaso::validation.crud.table_not_found', ['table' => $value])); + } + }, + ], + 'rows' => 'required', + 'rows.*.field' => [ + 'required', + function ($attribute, $value, $fail) use ($request) { + if (!Schema::hasColumn($request->name, $value)) { + $split_attribute = explode('.', $attribute); + $split_attribute[2] = 'relation_type'; + $field_to_relation = join('.', $split_attribute); + if (!$field_to_relation == 'belongs_to_many') { + $request->{$attribute} == $value ? $value : $fail(__('badaso::validation.crud.table_column_not_found', ['table_column' => "$request->name.{$value}"])); + } + } else { + $table_fields = SchemaManager::describeTable($request->name); + $field = collect($table_fields)->where('field', $value)->first(); + $row = collect($request->rows)->where('field', $value)->first(); + if (!$row['add'] && !$field['autoincrement'] && $field['notnull'] && is_null($field['default'])) { + $fail(__('badaso::validation.crud.table_column_not_have_default_value', ['table_column' => "$request->name.{$value}"])); + } elseif ($row['field'] != 'id' && $field['key'] == 'PRI') { + $fail(__('badaso::validation.crud.id_table_wrong', ['table_column' => "$request->name.{$value}"])); + } + } + }, + ], + 'rows.*.type' => 'required', + 'rows.*.display_name' => 'required', + 'display_name_singular' => 'required', + 'notification.*.event' => ['in:onCreate,onRead,onUpdate,onDelete'], + 'create_soft_delete' => ['required', 'boolean', function ($att, $val, $failed) use ($request) { + if (isset($request->name) && $val) { + if (!Schema::hasColumn($request->name, 'deleted_at')) { + $failed(__('badaso::validation.crud.table_deleted_at_not_exists', [ + 'table_name' => $request->name, + ])); + } + } + }], + ]); + + $table_name = $request->input('name'); + + $data_type = DataType::find($request->input('id')); + + $data_type->name = $table_name; + $data_type->slug = $request->input('slug') ?? Str::slug($table_name); + $data_type->display_name_singular = $request->input('display_name_singular'); + $data_type->display_name_plural = $request->input('display_name_plural') ?? Str::plural($data_type->display_name_singular); + $data_type->icon = $request->input('icon'); + $data_type->model_name = $request->input('model_name'); + $data_type->policy_name = $request->input('policy_name'); + $data_type->order_column = $request->input('order_column'); + $data_type->order_display_column = $request->input('order_display_column'); + $data_type->order_direction = $request->input('order_direction'); + $data_type->description = $request->input('description'); + $data_type->generate_permissions = $request->input('generate_permissions'); + $data_type->server_side = $request->input('server_side'); + $data_type->details = $request->input('details'); + $data_type->controller = $request->input('controller'); + $data_type->notification = json_encode($request->input('notification')); + $data_type->is_soft_delete = $request->input('create_soft_delete'); + $data_type->save(); + + DataRow::where('data_type_id', $data_type->id)->delete(); + + $data_rows = $request->input('rows') ?? []; + $new_data_rows = []; + foreach ($data_rows as $index => $data_row) { + $new_data_row = new DataRow(); + $new_data_row->data_type_id = $data_type->id; + $new_data_row->field = $data_row['field']; + $new_data_row->type = $data_row['type']; + $new_data_row->display_name = $data_row['display_name']; + $new_data_row->required = isset($data_row['required']) ? $data_row['required'] : false; + $new_data_row->browse = isset($data_row['browse']) ? $data_row['browse'] : false; + $new_data_row->read = isset($data_row['read']) ? $data_row['read'] : false; + $new_data_row->edit = isset($data_row['edit']) ? $data_row['edit'] : false; + $new_data_row->add = isset($data_row['add']) ? $data_row['add'] : false; + $new_data_row->delete = isset($data_row['delete']) ? $data_row['delete'] : false; + $new_data_row->details = isset($data_row['details']) ? $data_row['details'] : ''; + if ($data_row['type'] != 'relation') { + $new_data_row->relation = null; + } else { + $relation = []; + if (isset($data_row['relation_type'])) { + $relation['relation_type'] = $data_row['relation_type']; + } + if (isset($data_row['destination_table'])) { + $relation['destination_table'] = $data_row['destination_table']; + } + if (isset($data_row['destination_table_column'])) { + $relation['destination_table_column'] = $data_row['destination_table_column']; + } + if (isset($data_row['destination_table_display_column'])) { + $relation['destination_table_display_column'] = $data_row['destination_table_display_column']; + } + if (isset($data_row['destination_table_display_more_column'])) { + $relation['destination_table_display_more_column'] = $data_row['destination_table_display_more_column']; + } + if (in_array(count($relation), range(4, 5))) { + $new_data_row->relation = json_encode($relation); + } + } + $new_data_row->order = $index + 1; + $new_data_row->save(); + + $new_data_rows[] = $new_data_row; + } + + if ($data_type->generate_permissions) { + Permission::generateFor($data_type->name, true); + } else { + Permission::removeFrom($data_type->name); + } + + $this->addEditMenuItem($data_type); + + $data_type->data_rows = $new_data_rows; + + event(new CRUDDataUpdated($data_type, null)); + + $this->generateAPIDocs($table_name, $data_rows, $data_type); + DB::commit(); + + activity('CRUD') + ->causedBy(auth()->user() ?? null) + ->withProperties([ + 'old' => $data_type, + 'new' => $request->input(), + ]) + ->performedOn($data_type) + ->event('updated') + ->log('CRUD table ' . $data_type->slug . ' has been updated'); + + return ApiResponse::success($data_type); + } catch (Exception $e) { + DB::rollBack(); + + return ApiResponse::failed($e); + } + } public function add(Request $request) { @@ -309,7 +306,7 @@ public function add(Request $request) 'required', 'unique:Uasoft\Badaso\Models\DataType', function ($attribute, $value, $fail) { - if (! Schema::hasTable($value)) { + if (!Schema::hasTable($value)) { $fail(__('badaso::validation.crud.table_not_found', ['table' => $value])); } }, @@ -319,18 +316,18 @@ function ($attribute, $value, $fail) { 'rows.*.field' => [ 'required', function ($attribute, $value, $fail) use ($request) { - if (! Schema::getColumns($request->name, $value)) { + if (!Schema::hasColumn($request->name, $value)) { $split_attribute = explode('.', $attribute); $split_attribute[2] = 'relation_type'; $field_to_relation = join('.', $split_attribute); - if (! $field_to_relation == 'belongs_to_many') { + if (!$field_to_relation == 'belongs_to_many') { $fail(__('badaso::validation.crud.table_column_not_found', ['table_column' => "$request->name.{$value}"])); } } else { $table_fields = SchemaManager::describeTable($request->name); $field = collect($table_fields)->where('field', $value)->first(); $row = collect($request->rows)->where('field', $value)->first(); - if (! $row['add'] && ! $field['autoincrement'] && $field['notnull'] && is_null($field['default'])) { + if (!$row['add'] && !$field['autoincrement'] && $field['notnull'] && is_null($field['default'])) { $fail(__('badaso::validation.crud.table_column_not_have_default_value', ['table_column' => "$request->name.{$value}"])); } elseif ($row['field'] != 'id' && $field['key'] == 'PRI') { $fail(__('badaso::validation.crud.id_table_wrong', ['table_column' => "$request->name.{$value}"])); @@ -344,7 +341,7 @@ function ($attribute, $value, $fail) use ($request) { 'notification.*.event' => ['in:onCreate,onRead,onUpdate,onDelete'], 'create_soft_delete' => ['required', 'boolean', function ($att, $val, $failed) use ($request) { if (isset($request->name) && $val) { - if (! Schema::hasColumn($request->name, 'deleted_at')) { + if (!Schema::hasColumn($request->name, 'deleted_at')) { $failed(__('badaso::validation.crud.table_deleted_at_not_exists', [ 'table_name' => $request->name, ])); @@ -432,7 +429,7 @@ function ($attribute, $value, $fail) use ($request) { ->withProperties(['attributes' => $new_data_type]) ->performedOn($new_data_type) ->event('created') - ->log('CRUD table '.$new_data_type->slug.' has been created'); + ->log('CRUD table ' . $new_data_type->slug . ' has been created'); return ApiResponse::success($new_data_type); } catch (Exception $e) { @@ -442,205 +439,205 @@ function ($attribute, $value, $fail) use ($request) { } } - // public function delete(Request $request) - // { - // DB::beginTransaction(); - - // try { - // $request->validate([ - // 'id' => 'required|exists:Uasoft\Badaso\Models\DataType,id', - // ]); - - // $data_type = DataType::find($request->id); - - // $this->deleteAPIDocs($data_type->name); - - // Permission::removeFrom($data_type->name); - - // $this->deleteMenuItem($data_type); - - // $data_type->delete(); - - // event(new CRUDDataDeleted($data_type)); - - // DB::commit(); - - // activity('CRUD') - // ->causedBy(auth()->user() ?? null) - // ->withProperties(['attributes' => $data_type]) - // ->performedOn($data_type) - // ->event('deleted') - // ->log('CRUD table '.$data_type->slug.' has been deleted'); - - // return ApiResponse::success(); - // } catch (Exception $e) { - // DB::rollBack(); - - // return ApiResponse::failed($e); - // } - // } - - // private function addEditMenuItem($data_type) - // { - // $menu_key = config('badaso.default_menu'); - // $menu = Menu::where('key', $menu_key)->first(); - // $url = '/'.$menu_key.'/'.$data_type->slug; - - // if (is_null($menu)) { - // $menu = new Menu(); - // $menu->key = $menu_key; - // $menu->display_name = Str::studly($menu_key); - // $menu->save(); - // } - - // $menu_item = MenuItem::firstOrNew([ - // 'menu_id' => $menu->id, - // 'url' => $url, - // ]); - - // $menu_item = MenuItem::where('menu_id', $menu->id)->where('url', $url)->first(); - // if ($menu_item) { - // $menu_item->title = $data_type->display_name_plural; - // $menu_item->target = '_self'; - // $menu_item->icon_class = $data_type->icon; - // $menu_item->color = null; - // $menu_item->parent_id = null; - // $menu_item->permissions = $data_type->generate_permissions ? 'browse_'.$data_type->name : null; - // $menu_item->save(); - // } else { - // $menu_item = new MenuItem(); - // $menu_item->menu_id = $menu->id; - // $menu_item->url = $url; - // $menu_item->title = $data_type->display_name_plural; - // $menu_item->target = '_self'; - // $menu_item->icon_class = $data_type->icon; - // $menu_item->color = null; - // $menu_item->parent_id = null; - // $menu_item->permissions = $data_type->generate_permissions ? 'browse_'.$data_type->name : null; - // $menu_item->order = $menu_item->highestOrderMenuItem(); - // $menu_item->save(); - // } - // } - - // private function deleteMenuItem($data_type) - // { - // $menu_key = config('badaso.default_menu'); - // $url = '/'.$menu_key.'/'.$data_type->slug; - // MenuItem::where('url', $url)->delete(); - // } - - // private function generateAPIDocs($table_name, $data_rows, $data_type) - // { - // $filesystem = new LaravelFileSystem(); - // $file_path = ApiDocs::getFilePath($table_name); - // $stub = ApiDocs::getStub($table_name, $data_rows, $data_type); - // if (! $filesystem->put($file_path, $stub)) { - // return false; - // } - - // return true; - // } - - // private function deleteAPIDocs($table_name) - // { - // $filesystem = new LaravelFileSystem(); - // $file_path = ApiDocs::getFilePath($table_name); - // if ($filesystem->exists($file_path)) { - // return $filesystem->delete($file_path); - // } - - // return false; - // } - - // private function addTablePolymorphism($request) - // { - // foreach ($request['rows'] as $key => $value) { - // if (isset($value['relation_type']) && $value['relation_type'] == 'belongs_to_many') { - // $table = $value['field']; - // $rows = [ - // 0 => [ - // 'id' => 'id', - // 'field_name' => 'id', - // 'field_type' => 'bigint', - // 'field_length' => null, - // 'field_null' => false, - // 'field_attribute' => true, - // 'field_increment' => true, - // 'field_index' => 'primary', - // 'field_default' => null, - // 'undeletable' => true, - // ], - // 1 => [ - // 'id' => $request['name'].'_id', - // 'field_name' => $request['name'].'_id', - // 'field_type' => 'bigint', - // 'field_length' => null, - // 'field_null' => false, - // 'field_attribute' => true, - // 'field_increment' => false, - // 'field_index' => 'foreign', - // 'field_default' => null, - // ], - // 2 => [ - // 'id' => $value['destination_table'].'_id', - // 'field_name' => $value['destination_table'].'_id', - // 'field_type' => 'bigint', - // 'field_length' => null, - // 'field_null' => false, - // 'field_attribute' => true, - // 'field_increment' => false, - // 'field_index' => 'foreign', - // 'field_default' => null, - // ], - // 3 => [ - // 'field_name' => 'created_at', - // 'field_type' => 'timestamp', - // 'field_length' => null, - // 'field_null' => true, - // 'field_attribute' => false, - // 'field_increment' => false, - // 'field_index' => null, - // 'field_default' => null, - // 'undeletable' => true, - // 'indexes' => true, - // ], - // 4 => [ - // 'field_name' => 'updated_at', - // 'field_type' => 'timestamp', - // 'field_length' => null, - // 'field_null' => true, - // 'field_attribute' => false, - // 'field_increment' => false, - // 'field_index' => null, - // 'field_default' => null, - // 'undeletable' => true, - // ], - // ]; - - // $relations = [ - // $request['name'].'_id' => [ - // 'source_field' => $request['name'].'_id', - // 'target_table' => $request['name'], - // 'target_field' => 'id', - // 'on_delete' => 'cascade', - // 'on_update' => 'restrict', - // ], - // $value['destination_table'].'_id' => [ - // 'source_field' => $value['destination_table'].'_id', - // 'target_table' => $value['destination_table'], - // 'target_field' => 'id', - // 'on_delete' => 'cascade', - // 'on_update' => 'restrict', - // ], - // ]; - // if (! Schema::hasTable($table)) { - // $this->file_name = $this->file_generator->generateBDOMigrationFile($table, 'create', $rows, $relations); - // $exitCode = Artisan::call('migrate', [ - // '--path' => 'database/migrations/badaso/', - // '--force' => true, - // ]); - // } - // } - // } - // } + public function delete(Request $request) + { + DB::beginTransaction(); + + try { + $request->validate([ + 'id' => 'required|exists:Uasoft\Badaso\Models\DataType,id', + ]); + + $data_type = DataType::find($request->id); + + $this->deleteAPIDocs($data_type->name); + + Permission::removeFrom($data_type->name); + + $this->deleteMenuItem($data_type); + + $data_type->delete(); + + event(new CRUDDataDeleted($data_type)); + + DB::commit(); + + activity('CRUD') + ->causedBy(auth()->user() ?? null) + ->withProperties(['attributes' => $data_type]) + ->performedOn($data_type) + ->event('deleted') + ->log('CRUD table ' . $data_type->slug . ' has been deleted'); + + return ApiResponse::success(); + } catch (Exception $e) { + DB::rollBack(); + + return ApiResponse::failed($e); + } + } + + private function addEditMenuItem($data_type) + { + $menu_key = config('badaso.default_menu'); + $menu = Menu::where('key', $menu_key)->first(); + $url = '/' . $menu_key . '/' . $data_type->slug; + + if (is_null($menu)) { + $menu = new Menu(); + $menu->key = $menu_key; + $menu->display_name = Str::studly($menu_key); + $menu->save(); + } + + $menu_item = MenuItem::firstOrNew([ + 'menu_id' => $menu->id, + 'url' => $url, + ]); + + $menu_item = MenuItem::where('menu_id', $menu->id)->where('url', $url)->first(); + if ($menu_item) { + $menu_item->title = $data_type->display_name_plural; + $menu_item->target = '_self'; + $menu_item->icon_class = $data_type->icon; + $menu_item->color = null; + $menu_item->parent_id = null; + $menu_item->permissions = $data_type->generate_permissions ? 'browse_' . $data_type->name : null; + $menu_item->save(); + } else { + $menu_item = new MenuItem(); + $menu_item->menu_id = $menu->id; + $menu_item->url = $url; + $menu_item->title = $data_type->display_name_plural; + $menu_item->target = '_self'; + $menu_item->icon_class = $data_type->icon; + $menu_item->color = null; + $menu_item->parent_id = null; + $menu_item->permissions = $data_type->generate_permissions ? 'browse_' . $data_type->name : null; + $menu_item->order = $menu_item->highestOrderMenuItem(); + $menu_item->save(); + } + } + + private function deleteMenuItem($data_type) + { + $menu_key = config('badaso.default_menu'); + $url = '/' . $menu_key . '/' . $data_type->slug; + MenuItem::where('url', $url)->delete(); + } + + private function generateAPIDocs($table_name, $data_rows, $data_type) + { + $filesystem = new LaravelFileSystem(); + $file_path = ApiDocs::getFilePath($table_name); + $stub = ApiDocs::getStub($table_name, $data_rows, $data_type); + if (!$filesystem->put($file_path, $stub)) { + return false; + } + + return true; + } + + private function deleteAPIDocs($table_name) + { + $filesystem = new LaravelFileSystem(); + $file_path = ApiDocs::getFilePath($table_name); + if ($filesystem->exists($file_path)) { + return $filesystem->delete($file_path); + } + + return false; + } + + private function addTablePolymorphism($request) + { + foreach ($request['rows'] as $key => $value) { + if (isset($value['relation_type']) && $value['relation_type'] == 'belongs_to_many') { + $table = $value['field']; + $rows = [ + 0 => [ + 'id' => 'id', + 'field_name' => 'id', + 'field_type' => 'bigint', + 'field_length' => null, + 'field_null' => false, + 'field_attribute' => true, + 'field_increment' => true, + 'field_index' => 'primary', + 'field_default' => null, + 'undeletable' => true, + ], + 1 => [ + 'id' => $request['name'] . '_id', + 'field_name' => $request['name'] . '_id', + 'field_type' => 'bigint', + 'field_length' => null, + 'field_null' => false, + 'field_attribute' => true, + 'field_increment' => false, + 'field_index' => 'foreign', + 'field_default' => null, + ], + 2 => [ + 'id' => $value['destination_table'] . '_id', + 'field_name' => $value['destination_table'] . '_id', + 'field_type' => 'bigint', + 'field_length' => null, + 'field_null' => false, + 'field_attribute' => true, + 'field_increment' => false, + 'field_index' => 'foreign', + 'field_default' => null, + ], + 3 => [ + 'field_name' => 'created_at', + 'field_type' => 'timestamp', + 'field_length' => null, + 'field_null' => true, + 'field_attribute' => false, + 'field_increment' => false, + 'field_index' => null, + 'field_default' => null, + 'undeletable' => true, + 'indexes' => true, + ], + 4 => [ + 'field_name' => 'updated_at', + 'field_type' => 'timestamp', + 'field_length' => null, + 'field_null' => true, + 'field_attribute' => false, + 'field_increment' => false, + 'field_index' => null, + 'field_default' => null, + 'undeletable' => true, + ], + ]; + + $relations = [ + $request['name'] . '_id' => [ + 'source_field' => $request['name'] . '_id', + 'target_table' => $request['name'], + 'target_field' => 'id', + 'on_delete' => 'cascade', + 'on_update' => 'restrict', + ], + $value['destination_table'] . '_id' => [ + 'source_field' => $value['destination_table'] . '_id', + 'target_table' => $value['destination_table'], + 'target_field' => 'id', + 'on_delete' => 'cascade', + 'on_update' => 'restrict', + ], + ]; + if (!Schema::hasTable($table)) { + $this->file_name = $this->file_generator->generateBDOMigrationFile($table, 'create', $rows, $relations); + $exitCode = Artisan::call('migrate', [ + '--path' => 'database/migrations/badaso/', + '--force' => true, + ]); + } + } + } + } } diff --git a/src/Controllers/BadasoDatabaseController.php b/src/Controllers/BadasoDatabaseController.php index ee1d12ef3..6cbff66c2 100644 --- a/src/Controllers/BadasoDatabaseController.php +++ b/src/Controllers/BadasoDatabaseController.php @@ -109,7 +109,6 @@ function ($attribute, $value, $fail) { Rule::notIn(Badaso::getProtectedTables()), ], ]); - $columns = SchemaManager::describeTable($request->table)->toArray(); $columnsFK = SchemaManager::getDoctrineForeignKeys($request->table); $fKConstraints = []; diff --git a/src/Controllers/BadasoTableController.php b/src/Controllers/BadasoTableController.php index 8088792cf..374a25f49 100644 --- a/src/Controllers/BadasoTableController.php +++ b/src/Controllers/BadasoTableController.php @@ -22,13 +22,12 @@ class BadasoTableController extends Controller public function browse(Request $request) { try { - // $tables = SchemaManager::listTables(); - $tables = Schema::getTables(); + $tables = SchemaManager::listTables(); $custom_list = []; foreach ($tables as $key => $value) { $table = []; - $table['value'] = $value["name"]; - $table['label'] = ucfirst(str_replace('_', ' ', $value["name"])); + $table['value'] = $key; + $table['label'] = ucfirst(str_replace('_', ' ', $key)); $custom_list[] = $table; } diff --git a/src/Database/Schema/SchemaManager.php b/src/Database/Schema/SchemaManager.php index 519da72f1..2bc4b4e48 100644 --- a/src/Database/Schema/SchemaManager.php +++ b/src/Database/Schema/SchemaManager.php @@ -2,17 +2,30 @@ namespace Uasoft\Badaso\Database\Schema; -use Doctrine\DBAL\Schema\Table; -use Illuminate\Database\Schema\Blueprint; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; +use Doctrine\DBAL\Schema\SchemaException; +use Doctrine\DBAL\Schema\Table as DoctrineTable; +use Illuminate\Support\Facades\DB; use Uasoft\Badaso\Database\Types\Type; +use Doctrine\DBAL\DriverManager; abstract class SchemaManager { + // todo: trim parameters + public static function __callStatic($method, $args) { - return Schema::$method(...$args); + return static::manager()->$method(...$args); + } + + public static function manager() + { + return DB::connection()->select('SELECT schema_name FROM information_schema.schemata'); + } + + public static function getDatabaseConnection() + { + return DB::connection()->getDoctrineConnection(); } public static function tableExists($table) @@ -21,52 +34,84 @@ public static function tableExists($table) $table = [$table]; } - foreach ($table as $tbl) { - if (!Schema::hasTable($tbl)) { - return false; - } - } - - return true; + return static::manager()->tablesExist($table); } public static function listTables() { - $tables = DB::select('SHOW TABLES'); - $database_name = DB::getDatabaseName(); - $tables_key = "Tables_in_$database_name"; - - $result = []; - foreach ($tables as $table) { - $table_name = $table->$tables_key; - $result[$table_name] = static::listTableDetails($table_name); + // $sm = static::registerConnection()->createSchemaManager(); + // $list_tables = $sm->listTables(); + + $sm = Schema::getTables(); + + $tables = []; + + foreach ($sm as $key => $table_name) { + // $tables[$table_name["name"]] = static::listTableDetails($table_name["name"]); + $tables[$table_name["name"]] = $table_name["name"]; } - - return $result; + + return $tables; } + /** + * @param string $table_name + * @return \Uasoft\Badaso\Database\Schema\Table + */ public static function listTableDetails($table_name) { - $columns = Schema::getColumnListing($table_name); + $sm =static::registerConnection()->createSchemaManager(); + $columns = $sm->listTableColumns($table_name); + + $foreign_keys = []; + if (static::registerConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) { + $foreign_keys = $sm->listTableForeignKeys($table_name); + } - // $foreign_keys = static::listTableForeignKeys($table_name); - $foreign_keys[] = Schema::getForeignKeys($table_name); - // $indexes = Schema::listTableIndexes($table_name); - $indexes[] = Schema::getIndexes($table_name); - return new Table($table_name, $columns, $indexes, $foreign_keys, []); + $indexes = $sm->listTableIndexes($table_name); + + return new Table($table_name, $columns, $indexes, [],$foreign_keys, []); } - public static function getColumnDetails($table_name, $column_name) + public static function registerConnection() { - return DB::select(DB::raw("SHOW COLUMNS FROM `$table_name` LIKE '$column_name'"))[0]; + $databaseConfig = config('database.connections.' . config('database.default')); + $driver_name = DB::getDriverName(); + if($driver_name == 'mysql' || $driver_name = 'pgsql') { + $connectionParams = [ + 'dbname' => $databaseConfig['database'], + 'user' => $databaseConfig['username'], + 'password' => $databaseConfig['password'], + 'host' => $databaseConfig['host'], + 'driver' => 'pdo_' . $driver_name, + 'port' => $databaseConfig['port'], + ]; + } else { + $connectionParams = [ + 'user' => $databaseConfig['username'], + 'password' => $databaseConfig['password'], + 'path' => $databaseConfig['database'], + 'driver' => 'pdo_' . $driver_name, + ]; + } + + $doctrine_connection = DriverManager::getConnection($connectionParams); + + return $doctrine_connection; } + /** + * Describes given table. + * + * @param string $table_name + * @return \Illuminate\Support\Collection + */ public static function describeTable($table_name) { - // Type::registerCustomPlatformTypes(); + Type::registerCustomPlatformTypes(); $table = static::listTableDetails($table_name); - + return collect($table->columns)->map(function ($column) use ($table) { $column_array = Column::toArray($column); @@ -96,32 +141,22 @@ public static function listTableColumnNames($table_name) { Type::registerCustomPlatformTypes(); - return Schema::getColumnListing($table_name); + $column_names = []; + + foreach (static::manager()->listTableColumns($table_name) as $column) { + $column_names[] = $column->getName(); + } + + return $column_names; } public static function createTable($table) { - if (!($table instanceof Blueprint)) { + if (!($table instanceof DoctrineTable)) { $table = Table::make($table); } - Schema::create($table->getTable(), function (Blueprint $blueprint) use ($table) { - foreach ($table->getColumns() as $column) { - $blueprint->addColumn($column->getType(), $column->getName(), $column->getOptions()); - } - - foreach ($table->getIndexes() as $index) { - $blueprint->index($index->getColumns(), $index->getName(), $index->getOptions()); - } - - foreach ($table->getForeignKeys() as $foreignKey) { - $blueprint->foreign($foreignKey->getLocalColumns()) - ->references($foreignKey->getForeignColumns()) - ->on($foreignKey->getForeignTable()) - ->onDelete($foreignKey->onDelete()) - ->onUpdate($foreignKey->onUpdate()); - } - }); + static::manager()->createTable($table); } public static function getDoctrineTable($table) @@ -129,26 +164,20 @@ public static function getDoctrineTable($table) $table = trim($table); if (!static::tableExists($table)) { - throw new \Exception("Table $table does not exist."); + throw SchemaException::tableDoesNotExist($table); } - return static::listTableDetails($table); + return static::manager()->listTableDetails($table); } public static function getDoctrineColumn($table, $column) { - return static::getColumnDetails($table, $column); - } - - public static function listTableForeignKeys($table_name) - { - $foreignKeys = DB::select(DB::raw("SELECT * FROM information_schema.key_column_usage WHERE TABLE_NAME = '$table_name' AND TABLE_SCHEMA = '" . DB::getDatabaseName() . "' AND REFERENCED_COLUMN_NAME IS NOT NULL")); - return $foreignKeys; + return static::getDoctrineTable($table)->getColumn($column); } - public static function listTableIndexes($table_name) + public static function getDoctrineForeignKeys($table) { - $indexes = DB::select(DB::raw("SHOW INDEX FROM `$table_name`")); - return $indexes; + $sm = static::registerConnection()->createSchemaManager(); + return $sm->listTableForeignKeys($table); } } diff --git a/src/Database/Types/Type.php b/src/Database/Types/Type.php index 5fe72aa04..b973ebffe 100644 --- a/src/Database/Types/Type.php +++ b/src/Database/Types/Type.php @@ -2,10 +2,12 @@ namespace Uasoft\Badaso\Database\Types; -use Illuminate\Support\Facades\DB; -use Uasoft\Badaso\Database\Platforms\Platform; -use Doctrine\DBAL\Types\Type as DoctrineType; use Doctrine\DBAL\Platforms\AbstractPlatform as DoctrineAbstractPlatform; +use Doctrine\DBAL\Types\Type as DoctrineType; +use Uasoft\Badaso\Database\Platforms\Platform; +use Uasoft\Badaso\Database\Schema\SchemaManager; +use Illuminate\Support\Facades\DB; +use Doctrine\DBAL\DriverManager; abstract class Type extends DoctrineType { @@ -15,7 +17,6 @@ abstract class Type extends DoctrineType protected static $platform_types = []; protected static $custom_type_options = []; protected static $type_categories = []; - protected static $types = []; const NAME = 'UNDEFINED_TYPE_NAME'; const NOT_SUPPORTED = 'notSupported'; @@ -30,7 +31,7 @@ public function getName() return static::NAME; } - public static function toArray($type) + public static function toArray(DoctrineType $type) { $custom_type_options = $type->customOptions ?? []; @@ -49,11 +50,13 @@ public static function getPlatformTypes() static::registerCustomPlatformTypes(); } + $doctrine_connection = SchemaManager::registerConnection(); + $platform = $doctrine_connection->getDatabasePlatform(); $platform_name = DB::getDriverName(); static::$platform_types = Platform::getPlatformTypes( $platform_name, - static::getPlatformTypeMapping($platform_name) + static::getPlatformTypeMapping($platform) ); static::$platform_types = static::$platform_types->map(function ($type) { @@ -63,15 +66,14 @@ public static function getPlatformTypes() return static::$platform_types; } - public static function getPlatformTypeMapping(DoctrineAbstractPlatform $platform_name) + public static function getPlatformTypeMapping(DoctrineAbstractPlatform $platform) { if (static::$platform_type_mapping) { return static::$platform_type_mapping; } - // Anda perlu mendefinisikan cara mengambil pemetaan tipe khusus untuk platform tertentu static::$platform_type_mapping = collect( - get_protected_property($platform_name, 'doctrineTypeMapping') + get_protected_property($platform, 'doctrineTypeMapping') ); return static::$platform_type_mapping; @@ -83,25 +85,27 @@ public static function registerCustomPlatformTypes($force = false) return; } - $platform_name = DB::getDriverName(); - + $doctrine_connection = SchemaManager::registerConnection(); + $platform = $doctrine_connection->getDatabasePlatform(); + $platform_name = ucfirst(DB::getDriverName()); + $custom_types = array_merge( static::getPlatformCustomTypes('Common'), static::getPlatformCustomTypes($platform_name) ); - + foreach ($custom_types as $type) { $name = $type::NAME; - if (!static::hasType($name)) { - static::addType($name, $type); - } else { + if (static::hasType($name)) { static::overrideType($name, $type); + } else { + static::addType($name, $type); } $db_type = defined("{$type}::DBTYPE") ? $type::DBTYPE : $name; - - static::registerDoctrineTypeMapping($db_type, $name); + + $platform->registerDoctrineTypeMapping($db_type, $name); } static::addCustomTypeOptions($platform_name); @@ -109,29 +113,6 @@ public static function registerCustomPlatformTypes($force = false) static::$custom_type_registered = true; } - protected static function registerDoctrineTypeMapping($db_type, $name) - { - $databaseConfig = config('database.connections.' . config('database.default')); - - $connectionParams = [ - 'dbname' => $databaseConfig['database'], - 'user' => $databaseConfig['username'], - 'password' => $databaseConfig['password'], - 'host' => $databaseConfig['host'], - 'driver' => 'pdo_mysql', // Sesuaikan dengan driver yang digunakan, misal: 'pdo_pgsql' untuk PostgreSQL - 'port' => $databaseConfig['port'], - ]; - - $doctrine_connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams); - $platform = $doctrine_connection->getDatabasePlatform(); - - if ($platform->hasDoctrineTypeMappingFor($db_type)) { - $platform->registerDoctrineTypeMapping($db_type, $name); - } else { - throw new \Doctrine\DBAL\Exception("Type to be overwritten $db_type does not exist."); - } - } - protected static function addCustomTypeOptions($platform_name) { static::registerCommonCustomTypeOptions(); @@ -354,24 +335,4 @@ public static function getTypeCategories() return static::$type_categories; } - - public static function hasType($name) - { - return DoctrineType::hasType($name); - } - - public static function getType($name) - { - return static::$types[$name] ?? null; - } - - public static function addType($name, $type) - { - static::$types[$name] = new $type(); - } - - public static function overrideType($name, $type) - { - static::$types[$name] = new $type(); - } } From 1a9cdaf17f92f68108c1a1ba7da41550847b0a87 Mon Sep 17 00:00:00 2001 From: Miftahurrahmi Date: Fri, 28 Jun 2024 16:51:13 +0700 Subject: [PATCH 05/10] update to laravel 11 --- .github/workflows/phpunit.yml | 16 ++++++------ src/Database/Schema/SchemaManager.php | 31 ++++++++++------------- src/Database/Types/Common/VarCharType.php | 4 +-- src/Database/Types/Type.php | 5 ++-- src/Helpers/DataTypeToComponent.php | 1 + 5 files changed, 28 insertions(+), 29 deletions(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 897d747d3..290a0e750 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -38,15 +38,15 @@ jobs: strategy: fail-fast: false matrix: - php: [8.0, 8.1] - laravel: ["8.*", "9.*"] - laravel-require: [8.0] + php: [8.2] + laravel: ["11.*"] + laravel-require: [11.0] mysql-version: [8.0] coverage: [none] include: - - php: 8.1 - laravel: 9.* - laravel-require: 8.0 + - php: 8.2 + laravel: 11.* + laravel-require: 11.0 mysql-version: 8.0 coverage: xdebug @@ -130,8 +130,8 @@ jobs: sed -i 's/DB_PASSWORD=/DB_PASSWORD=postgres/g' .env && sed -i 's/DB_HOST=127.0.0.1/DB_HOST=localhost/g' .env && sed -i 's/DB_USERNAME=root/DB_USERNAME=postgres/g' .env && - sed -i 's/DB_PORT=3306/DB_PORT=5432/g' .env - + sed -i 's/DB_PORT=3306/DB_PORT=5432/g' .env + # Pgsql | Laravel setup - name: Pgsql | Laravel setup diff --git a/src/Database/Schema/SchemaManager.php b/src/Database/Schema/SchemaManager.php index 2bc4b4e48..a1c3d5974 100644 --- a/src/Database/Schema/SchemaManager.php +++ b/src/Database/Schema/SchemaManager.php @@ -39,18 +39,14 @@ public static function tableExists($table) public static function listTables() { - // $sm = static::registerConnection()->createSchemaManager(); - // $list_tables = $sm->listTables(); - $sm = Schema::getTables(); - + $tables = []; - + foreach ($sm as $key => $table_name) { - // $tables[$table_name["name"]] = static::listTableDetails($table_name["name"]); $tables[$table_name["name"]] = $table_name["name"]; } - + return $tables; } @@ -62,14 +58,14 @@ public static function listTableDetails($table_name) { $sm =static::registerConnection()->createSchemaManager(); $columns = $sm->listTableColumns($table_name); - + $foreign_keys = []; if (static::registerConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) { $foreign_keys = $sm->listTableForeignKeys($table_name); } $indexes = $sm->listTableIndexes($table_name); - + return new Table($table_name, $columns, $indexes, [],$foreign_keys, []); } @@ -77,24 +73,25 @@ public static function registerConnection() { $databaseConfig = config('database.connections.' . config('database.default')); $driver_name = DB::getDriverName(); - if($driver_name == 'mysql' || $driver_name = 'pgsql') { + + if($driver_name == 'mysql' || $driver_name == 'pgsql') { $connectionParams = [ 'dbname' => $databaseConfig['database'], 'user' => $databaseConfig['username'], 'password' => $databaseConfig['password'], 'host' => $databaseConfig['host'], - 'driver' => 'pdo_' . $driver_name, + 'driver' => 'pdo_' . $driver_name, 'port' => $databaseConfig['port'], ]; - } else { + } + + if ($driver_name == 'sqlite') { $connectionParams = [ - 'user' => $databaseConfig['username'], - 'password' => $databaseConfig['password'], 'path' => $databaseConfig['database'], - 'driver' => 'pdo_' . $driver_name, + 'driver' => 'pdo_' . $driver_name, ]; } - + $doctrine_connection = DriverManager::getConnection($connectionParams); return $doctrine_connection; @@ -111,7 +108,7 @@ public static function describeTable($table_name) Type::registerCustomPlatformTypes(); $table = static::listTableDetails($table_name); - + return collect($table->columns)->map(function ($column) use ($table) { $column_array = Column::toArray($column); diff --git a/src/Database/Types/Common/VarCharType.php b/src/Database/Types/Common/VarCharType.php index 235314abf..a7f1a97f3 100644 --- a/src/Database/Types/Common/VarCharType.php +++ b/src/Database/Types/Common/VarCharType.php @@ -6,10 +6,10 @@ class VarCharType extends DoctrineStringType { - const NAME = 'varchar'; + const NAME = 'string'; public function getName() { - return static::NAME; + return 'varchar'; } } diff --git a/src/Database/Types/Type.php b/src/Database/Types/Type.php index b973ebffe..c575cb702 100644 --- a/src/Database/Types/Type.php +++ b/src/Database/Types/Type.php @@ -87,13 +87,14 @@ public static function registerCustomPlatformTypes($force = false) $doctrine_connection = SchemaManager::registerConnection(); $platform = $doctrine_connection->getDatabasePlatform(); - $platform_name = ucfirst(DB::getDriverName()); + // $platform_name = ucfirst(DB::getDriverName()); + $platform_name = ucfirst($platform->getName()); $custom_types = array_merge( static::getPlatformCustomTypes('Common'), static::getPlatformCustomTypes($platform_name) ); - + foreach ($custom_types as $type) { $name = $type::NAME; diff --git a/src/Helpers/DataTypeToComponent.php b/src/Helpers/DataTypeToComponent.php index f59161e42..ca982623e 100644 --- a/src/Helpers/DataTypeToComponent.php +++ b/src/Helpers/DataTypeToComponent.php @@ -8,6 +8,7 @@ class DataTypeToComponent protected static $type_list = [ 'CHAR' => 'text', 'VARCHAR' => 'text', + 'STRING' => 'text', 'BINARY' => 'text', 'VARBINARY' => 'text', 'TINYBLOB' => 'text', From 7bb651af31ea199f6daf6344fa60a6ebe8445def Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 28 Jun 2024 09:52:23 +0000 Subject: [PATCH 06/10] Apply fixes from StyleCI --- src/Config/badaso-hidden-tables.php | 2 +- src/Controllers/BadasoCRUDController.php | 59 ++++++++++++------------ src/Database/Schema/SchemaManager.php | 25 +++++----- src/Database/Types/Type.php | 15 +++--- 4 files changed, 50 insertions(+), 51 deletions(-) diff --git a/src/Config/badaso-hidden-tables.php b/src/Config/badaso-hidden-tables.php index a13d1e67d..0768694c7 100644 --- a/src/Config/badaso-hidden-tables.php +++ b/src/Config/badaso-hidden-tables.php @@ -31,5 +31,5 @@ 'jobs', 'job_batches', 'password_reset_tokens', - 'sessions' + 'sessions', ]; diff --git a/src/Controllers/BadasoCRUDController.php b/src/Controllers/BadasoCRUDController.php index 6dffcc1ee..5a79d0ee8 100644 --- a/src/Controllers/BadasoCRUDController.php +++ b/src/Controllers/BadasoCRUDController.php @@ -49,8 +49,7 @@ public function browse(Request $request) $tables = SchemaManager::listTables(); $tables_with_crud_data = []; foreach ($tables as $key => $value) { - - if (!in_array($key, $protected_tables)) { + if (! in_array($key, $protected_tables)) { // add table watch config $config_watch_tables->addWatchTable($key); // end add table watch config @@ -87,7 +86,7 @@ public function read(Request $request) foreach ($table_fields as $key => $column) { $field = $key; $column = collect($column)->toArray(); - if (!in_array($field, $generated_fields)) { + if (! in_array($field, $generated_fields)) { $data_row['data_type_id'] = $data_type->id; $data_row['field'] = $key; $data_row['type'] = DataTypeToComponent::convert($column['type']); @@ -149,7 +148,7 @@ public function edit(Request $request) 'required', "unique:Uasoft\Badaso\Models\DataType,name,{$request->id}", function ($attribute, $value, $fail) { - if (!Schema::hasTable($value)) { + if (! Schema::hasTable($value)) { $fail(__('badaso::validation.crud.table_not_found', ['table' => $value])); } }, @@ -158,18 +157,18 @@ function ($attribute, $value, $fail) { 'rows.*.field' => [ 'required', function ($attribute, $value, $fail) use ($request) { - if (!Schema::hasColumn($request->name, $value)) { + if (! Schema::hasColumn($request->name, $value)) { $split_attribute = explode('.', $attribute); $split_attribute[2] = 'relation_type'; $field_to_relation = join('.', $split_attribute); - if (!$field_to_relation == 'belongs_to_many') { + if (! $field_to_relation == 'belongs_to_many') { $request->{$attribute} == $value ? $value : $fail(__('badaso::validation.crud.table_column_not_found', ['table_column' => "$request->name.{$value}"])); } } else { $table_fields = SchemaManager::describeTable($request->name); $field = collect($table_fields)->where('field', $value)->first(); $row = collect($request->rows)->where('field', $value)->first(); - if (!$row['add'] && !$field['autoincrement'] && $field['notnull'] && is_null($field['default'])) { + if (! $row['add'] && ! $field['autoincrement'] && $field['notnull'] && is_null($field['default'])) { $fail(__('badaso::validation.crud.table_column_not_have_default_value', ['table_column' => "$request->name.{$value}"])); } elseif ($row['field'] != 'id' && $field['key'] == 'PRI') { $fail(__('badaso::validation.crud.id_table_wrong', ['table_column' => "$request->name.{$value}"])); @@ -183,7 +182,7 @@ function ($attribute, $value, $fail) use ($request) { 'notification.*.event' => ['in:onCreate,onRead,onUpdate,onDelete'], 'create_soft_delete' => ['required', 'boolean', function ($att, $val, $failed) use ($request) { if (isset($request->name) && $val) { - if (!Schema::hasColumn($request->name, 'deleted_at')) { + if (! Schema::hasColumn($request->name, 'deleted_at')) { $failed(__('badaso::validation.crud.table_deleted_at_not_exists', [ 'table_name' => $request->name, ])); @@ -284,7 +283,7 @@ function ($attribute, $value, $fail) use ($request) { ]) ->performedOn($data_type) ->event('updated') - ->log('CRUD table ' . $data_type->slug . ' has been updated'); + ->log('CRUD table '.$data_type->slug.' has been updated'); return ApiResponse::success($data_type); } catch (Exception $e) { @@ -306,7 +305,7 @@ public function add(Request $request) 'required', 'unique:Uasoft\Badaso\Models\DataType', function ($attribute, $value, $fail) { - if (!Schema::hasTable($value)) { + if (! Schema::hasTable($value)) { $fail(__('badaso::validation.crud.table_not_found', ['table' => $value])); } }, @@ -316,18 +315,18 @@ function ($attribute, $value, $fail) { 'rows.*.field' => [ 'required', function ($attribute, $value, $fail) use ($request) { - if (!Schema::hasColumn($request->name, $value)) { + if (! Schema::hasColumn($request->name, $value)) { $split_attribute = explode('.', $attribute); $split_attribute[2] = 'relation_type'; $field_to_relation = join('.', $split_attribute); - if (!$field_to_relation == 'belongs_to_many') { + if (! $field_to_relation == 'belongs_to_many') { $fail(__('badaso::validation.crud.table_column_not_found', ['table_column' => "$request->name.{$value}"])); } } else { $table_fields = SchemaManager::describeTable($request->name); $field = collect($table_fields)->where('field', $value)->first(); $row = collect($request->rows)->where('field', $value)->first(); - if (!$row['add'] && !$field['autoincrement'] && $field['notnull'] && is_null($field['default'])) { + if (! $row['add'] && ! $field['autoincrement'] && $field['notnull'] && is_null($field['default'])) { $fail(__('badaso::validation.crud.table_column_not_have_default_value', ['table_column' => "$request->name.{$value}"])); } elseif ($row['field'] != 'id' && $field['key'] == 'PRI') { $fail(__('badaso::validation.crud.id_table_wrong', ['table_column' => "$request->name.{$value}"])); @@ -341,7 +340,7 @@ function ($attribute, $value, $fail) use ($request) { 'notification.*.event' => ['in:onCreate,onRead,onUpdate,onDelete'], 'create_soft_delete' => ['required', 'boolean', function ($att, $val, $failed) use ($request) { if (isset($request->name) && $val) { - if (!Schema::hasColumn($request->name, 'deleted_at')) { + if (! Schema::hasColumn($request->name, 'deleted_at')) { $failed(__('badaso::validation.crud.table_deleted_at_not_exists', [ 'table_name' => $request->name, ])); @@ -429,7 +428,7 @@ function ($attribute, $value, $fail) use ($request) { ->withProperties(['attributes' => $new_data_type]) ->performedOn($new_data_type) ->event('created') - ->log('CRUD table ' . $new_data_type->slug . ' has been created'); + ->log('CRUD table '.$new_data_type->slug.' has been created'); return ApiResponse::success($new_data_type); } catch (Exception $e) { @@ -467,7 +466,7 @@ public function delete(Request $request) ->withProperties(['attributes' => $data_type]) ->performedOn($data_type) ->event('deleted') - ->log('CRUD table ' . $data_type->slug . ' has been deleted'); + ->log('CRUD table '.$data_type->slug.' has been deleted'); return ApiResponse::success(); } catch (Exception $e) { @@ -481,7 +480,7 @@ private function addEditMenuItem($data_type) { $menu_key = config('badaso.default_menu'); $menu = Menu::where('key', $menu_key)->first(); - $url = '/' . $menu_key . '/' . $data_type->slug; + $url = '/'.$menu_key.'/'.$data_type->slug; if (is_null($menu)) { $menu = new Menu(); @@ -502,7 +501,7 @@ private function addEditMenuItem($data_type) $menu_item->icon_class = $data_type->icon; $menu_item->color = null; $menu_item->parent_id = null; - $menu_item->permissions = $data_type->generate_permissions ? 'browse_' . $data_type->name : null; + $menu_item->permissions = $data_type->generate_permissions ? 'browse_'.$data_type->name : null; $menu_item->save(); } else { $menu_item = new MenuItem(); @@ -513,7 +512,7 @@ private function addEditMenuItem($data_type) $menu_item->icon_class = $data_type->icon; $menu_item->color = null; $menu_item->parent_id = null; - $menu_item->permissions = $data_type->generate_permissions ? 'browse_' . $data_type->name : null; + $menu_item->permissions = $data_type->generate_permissions ? 'browse_'.$data_type->name : null; $menu_item->order = $menu_item->highestOrderMenuItem(); $menu_item->save(); } @@ -522,7 +521,7 @@ private function addEditMenuItem($data_type) private function deleteMenuItem($data_type) { $menu_key = config('badaso.default_menu'); - $url = '/' . $menu_key . '/' . $data_type->slug; + $url = '/'.$menu_key.'/'.$data_type->slug; MenuItem::where('url', $url)->delete(); } @@ -531,7 +530,7 @@ private function generateAPIDocs($table_name, $data_rows, $data_type) $filesystem = new LaravelFileSystem(); $file_path = ApiDocs::getFilePath($table_name); $stub = ApiDocs::getStub($table_name, $data_rows, $data_type); - if (!$filesystem->put($file_path, $stub)) { + if (! $filesystem->put($file_path, $stub)) { return false; } @@ -568,8 +567,8 @@ private function addTablePolymorphism($request) 'undeletable' => true, ], 1 => [ - 'id' => $request['name'] . '_id', - 'field_name' => $request['name'] . '_id', + 'id' => $request['name'].'_id', + 'field_name' => $request['name'].'_id', 'field_type' => 'bigint', 'field_length' => null, 'field_null' => false, @@ -579,8 +578,8 @@ private function addTablePolymorphism($request) 'field_default' => null, ], 2 => [ - 'id' => $value['destination_table'] . '_id', - 'field_name' => $value['destination_table'] . '_id', + 'id' => $value['destination_table'].'_id', + 'field_name' => $value['destination_table'].'_id', 'field_type' => 'bigint', 'field_length' => null, 'field_null' => false, @@ -615,22 +614,22 @@ private function addTablePolymorphism($request) ]; $relations = [ - $request['name'] . '_id' => [ - 'source_field' => $request['name'] . '_id', + $request['name'].'_id' => [ + 'source_field' => $request['name'].'_id', 'target_table' => $request['name'], 'target_field' => 'id', 'on_delete' => 'cascade', 'on_update' => 'restrict', ], - $value['destination_table'] . '_id' => [ - 'source_field' => $value['destination_table'] . '_id', + $value['destination_table'].'_id' => [ + 'source_field' => $value['destination_table'].'_id', 'target_table' => $value['destination_table'], 'target_field' => 'id', 'on_delete' => 'cascade', 'on_update' => 'restrict', ], ]; - if (!Schema::hasTable($table)) { + if (! Schema::hasTable($table)) { $this->file_name = $this->file_generator->generateBDOMigrationFile($table, 'create', $rows, $relations); $exitCode = Artisan::call('migrate', [ '--path' => 'database/migrations/badaso/', diff --git a/src/Database/Schema/SchemaManager.php b/src/Database/Schema/SchemaManager.php index a1c3d5974..80e7982f5 100644 --- a/src/Database/Schema/SchemaManager.php +++ b/src/Database/Schema/SchemaManager.php @@ -2,12 +2,12 @@ namespace Uasoft\Badaso\Database\Schema; -use Illuminate\Support\Facades\Schema; +use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Schema\SchemaException; use Doctrine\DBAL\Schema\Table as DoctrineTable; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Schema; use Uasoft\Badaso\Database\Types\Type; -use Doctrine\DBAL\DriverManager; abstract class SchemaManager { @@ -30,7 +30,7 @@ public static function getDatabaseConnection() public static function tableExists($table) { - if (!is_array($table)) { + if (! is_array($table)) { $table = [$table]; } @@ -44,7 +44,7 @@ public static function listTables() $tables = []; foreach ($sm as $key => $table_name) { - $tables[$table_name["name"]] = $table_name["name"]; + $tables[$table_name['name']] = $table_name['name']; } return $tables; @@ -56,7 +56,7 @@ public static function listTables() */ public static function listTableDetails($table_name) { - $sm =static::registerConnection()->createSchemaManager(); + $sm = static::registerConnection()->createSchemaManager(); $columns = $sm->listTableColumns($table_name); $foreign_keys = []; @@ -66,21 +66,21 @@ public static function listTableDetails($table_name) $indexes = $sm->listTableIndexes($table_name); - return new Table($table_name, $columns, $indexes, [],$foreign_keys, []); + return new Table($table_name, $columns, $indexes, [], $foreign_keys, []); } public static function registerConnection() { - $databaseConfig = config('database.connections.' . config('database.default')); + $databaseConfig = config('database.connections.'.config('database.default')); $driver_name = DB::getDriverName(); - if($driver_name == 'mysql' || $driver_name == 'pgsql') { + if ($driver_name == 'mysql' || $driver_name == 'pgsql') { $connectionParams = [ 'dbname' => $databaseConfig['database'], 'user' => $databaseConfig['username'], 'password' => $databaseConfig['password'], 'host' => $databaseConfig['host'], - 'driver' => 'pdo_' . $driver_name, + 'driver' => 'pdo_'.$driver_name, 'port' => $databaseConfig['port'], ]; } @@ -88,7 +88,7 @@ public static function registerConnection() if ($driver_name == 'sqlite') { $connectionParams = [ 'path' => $databaseConfig['database'], - 'driver' => 'pdo_' . $driver_name, + 'driver' => 'pdo_'.$driver_name, ]; } @@ -149,7 +149,7 @@ public static function listTableColumnNames($table_name) public static function createTable($table) { - if (!($table instanceof DoctrineTable)) { + if (! ($table instanceof DoctrineTable)) { $table = Table::make($table); } @@ -160,7 +160,7 @@ public static function getDoctrineTable($table) { $table = trim($table); - if (!static::tableExists($table)) { + if (! static::tableExists($table)) { throw SchemaException::tableDoesNotExist($table); } @@ -175,6 +175,7 @@ public static function getDoctrineColumn($table, $column) public static function getDoctrineForeignKeys($table) { $sm = static::registerConnection()->createSchemaManager(); + return $sm->listTableForeignKeys($table); } } diff --git a/src/Database/Types/Type.php b/src/Database/Types/Type.php index c575cb702..41a05be54 100644 --- a/src/Database/Types/Type.php +++ b/src/Database/Types/Type.php @@ -4,10 +4,9 @@ use Doctrine\DBAL\Platforms\AbstractPlatform as DoctrineAbstractPlatform; use Doctrine\DBAL\Types\Type as DoctrineType; +use Illuminate\Support\Facades\DB; use Uasoft\Badaso\Database\Platforms\Platform; use Uasoft\Badaso\Database\Schema\SchemaManager; -use Illuminate\Support\Facades\DB; -use Doctrine\DBAL\DriverManager; abstract class Type extends DoctrineType { @@ -46,7 +45,7 @@ public static function getPlatformTypes() return static::$platform_types; } - if (!static::$custom_type_registered) { + if (! static::$custom_type_registered) { static::registerCustomPlatformTypes(); } @@ -81,7 +80,7 @@ public static function getPlatformTypeMapping(DoctrineAbstractPlatform $platform public static function registerCustomPlatformTypes($force = false) { - if (static::$custom_type_registered && !$force) { + if (static::$custom_type_registered && ! $force) { return; } @@ -132,12 +131,12 @@ protected static function addCustomTypeOptions($platform_name) protected static function getPlatformCustomTypes($platform_name) { - $types_path = __DIR__ . DIRECTORY_SEPARATOR . $platform_name . DIRECTORY_SEPARATOR; - $namespace = __NAMESPACE__ . '\\' . $platform_name . '\\'; + $types_path = __DIR__.DIRECTORY_SEPARATOR.$platform_name.DIRECTORY_SEPARATOR; + $namespace = __NAMESPACE__.'\\'.$platform_name.'\\'; $types = []; - foreach (glob($types_path . '*.php') as $class_file) { - $types[] = $namespace . str_replace( + foreach (glob($types_path.'*.php') as $class_file) { + $types[] = $namespace.str_replace( '.php', '', str_replace($types_path, '', $class_file) From 5e0a72a95b221f1fb30ba2bdaa6624cf70af3d83 Mon Sep 17 00:00:00 2001 From: Miftahurrahmi Date: Fri, 28 Jun 2024 17:03:09 +0700 Subject: [PATCH 07/10] update laravel sanctum version in gitpod --- .gitpod/composer.json | 164 +++++++++++++++++++++--------------------- CONTRIBUTING.md | 30 +++----- 2 files changed, 91 insertions(+), 103 deletions(-) diff --git a/.gitpod/composer.json b/.gitpod/composer.json index ec19e502b..d24517480 100644 --- a/.gitpod/composer.json +++ b/.gitpod/composer.json @@ -1,86 +1,84 @@ { - "name": "laravel/laravel", - "type": "project", - "description": "The Laravel Framework.", - "keywords": [ - "framework", - "laravel" + "name": "laravel/laravel", + "type": "project", + "description": "The Laravel Framework.", + "keywords": [ + "framework", + "laravel" + ], + "license": "MIT", + "require": { + "php": "^8.2", + "laravel/framework": "^11.0", + "laravel/tinker": "^2.9", + "laravel-doctrine/orm": "2.1", + "psr/http-message": "1.1", + "arcanedev/log-viewer": "^11.0", + "darkaonline/l5-swagger": "^8.6", + "phpstan/phpstan": "^1.11", + "spatie/laravel-backup": "^8.8", + "unisharp/laravel-filemanager": "^2.9", + "spatie/laravel-activitylog": "^4.8", + "php-open-source-saver/jwt-auth": "^2.3", + "google/apiclient": "^2.16", + "spatie/flysystem-dropbox": "^3.0", + "masbug/flysystem-google-drive-ext": "^2.3", + "laravel/octane": "^2.3", + "laravel/sanctum": "^4.0", + "laravel/ui": "^4.5", + "webpatser/laravel-uuid": "^4.0", + "predis/predis": "^2.2", + "league/flysystem-aws-s3-v3": "^3.28", + "league/flysystem": "^3.28", + "doctrine/dbal": "^3.8" + }, + "require-dev": { + "fakerphp/faker": "^1.23", + "laravel/pint": "^1.13", + "laravel/sail": "^1.26", + "mockery/mockery": "^1.6", + "nunomaduro/collision": "^8.0", + "phpunit/phpunit": "^11.0.1", + "spatie/laravel-ignition": "^2.4" + }, + "autoload": { + "psr-4": { + "App\\": "app/", + "Database\\Factories\\": "database/factories/", + "Database\\Seeders\\": "database/seeders/", + "Uasoft\\Badaso\\": "packages/badaso/core/src/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, + "scripts": { + "post-autoload-dump": [ + "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", + "@php artisan package:discover --ansi" ], - "license": "MIT", - "require": { - "php": "^7.3|^8.0", - "fruitcake/laravel-cors": "^2.0", - "laravel/framework": "^8.75", - "masbug/flysystem-google-drive-ext": "*", - "google/apiclient": "^2.12", - "arcanedev/log-viewer": "^8.1|^9.0", - "darkaonline/l5-swagger": "^8.0|^8.3", - "doctrine/dbal": "^2.5", - "guzzlehttp/guzzle": "^7.2", - "larapack/doctrine-support": "^0.1.9", - "laravel/octane": "^1.2", - "laravel/sanctum": "^2.14.1", - "laravel/tinker": "^2.7", - "laravel/ui": "^3.4", - "league/flysystem": "~1.1|~2.0|~3.0", - "league/flysystem-aws-s3-v3": "^1.0.28|^3.0", - "php-open-source-saver/jwt-auth": "^1.4", - "phpstan/phpstan": "^1.4", - "predis/predis": "^1.1", - "symfony/var-exporter": "^5.2.0|^6.0", - "spatie/flysystem-dropbox": "^1.2.2|^2.0", - "spatie/laravel-activitylog": "^3.17|^4.4", - "spatie/laravel-backup": "^6.16|^8.1.0", - "unisharp/laravel-filemanager": "^2.4", - "webpatser/laravel-uuid": "^4.0", - "zircote/swagger-php": "^3.2" - }, - "require-dev": { - "facade/ignition": "^2.5", - "fakerphp/faker": "^1.9.1", - "laravel/sail": "^1.0.1", - "mockery/mockery": "^1.4.4", - "nunomaduro/collision": "^5.10", - "phpunit/phpunit": "^9.5.10" - }, - "autoload": { - "psr-4": { - "App\\": "app/", - "Database\\Factories\\": "database/factories/", - "Database\\Seeders\\": "database/seeders/", - "Uasoft\\Badaso\\": "packages/badaso/core/src/" - } - }, - "autoload-dev": { - "psr-4": { - "Tests\\": "tests/" - } - }, - "scripts": { - "post-autoload-dump": [ - "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", - "@php artisan package:discover --ansi" - ], - "post-update-cmd": [ - "@php artisan vendor:publish --tag=laravel-assets --ansi --force" - ], - "post-root-package-install": [ - "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" - ], - "post-create-project-cmd": [ - "@php artisan key:generate --ansi" - ] - }, - "extra": { - "laravel": { - "dont-discover": [] - } - }, - "config": { - "optimize-autoloader": true, - "preferred-install": "dist", - "sort-packages": true - }, - "minimum-stability": "dev", - "prefer-stable": true + "post-update-cmd": [ + "@php artisan vendor:publish --tag=laravel-assets --ansi --force" + ], + "post-root-package-install": [ + "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" + ], + "post-create-project-cmd": [ + "@php artisan key:generate --ansi" + ] + }, + "extra": { + "laravel": { + "dont-discover": [] + } + }, + "config": { + "optimize-autoloader": true, + "preferred-install": "dist", + "sort-packages": true + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f335c6df9..2b1b32bf9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -45,7 +45,7 @@ Steps for registering and getting a license on Badaso Dashboard can be found on After getting the license, you can proceed to Badaso installation. -1, Clone badaso into Laravel project. Sample: +1. Clone badaso into Laravel project. Sample: - Root Laravel Project - /packages // new folder - /badaso // new folder @@ -56,26 +56,16 @@ cd into badaso directory, then run git clone https://github.com/uasoft-indonesia/badaso.git ``` -2. Add the following Badaso provider and JWT provider to ```/config/app.php```. +2. Add the following Badaso provider ```/bootstrap/providers.php```. ``` -'providers' => [ +'return' => [ ..., - Uasoft\Badaso\Providers\BadasoServiceProvider::class, - PHPOpenSourceSaver\JWTAuth\Providers\LaravelServiceProvider::class, + "Uasoft\\Badaso\\Providers\\BadasoServiceProvider" ] ``` -3. Add the following aliases to ```config/app.php```. -``` -'aliases' => [ - ..., - 'JWTAuth' => PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth::class, - 'JWTFactory' => PHPOpenSourceSaver\JWTAuth\Facades\JWTFactory::class, -] -``` - -4. Add badaso providers to autoload +3. Add badaso providers to autoload ``` "autoload": { @@ -87,21 +77,21 @@ git clone https://github.com/uasoft-indonesia/badaso.git } ``` -5. Copy required library from ```packages/badaso/core/composer.json``` to ```/composer.json``` then ```composer install``` +4. Copy required library from ```packages/badaso/core/composer.json``` to ```/composer.json``` then ```composer install``` -6. Run the following commands to update dependencies in package.json and webpack. +5. Run the following commands to update dependencies in package.json and webpack. ``` php artisan badaso:setup ``` -7. Run the following commands in sequence. +6. Run the following commands in sequence. ``` composer dump-autoload php artisan migrate php artisan db:seed --class=BadasoSeeder ``` -8. Open the ```env``` file then add the following lines. +7. Open the ```env``` file then add the following lines. ``` #Set a key as secret key for generating JWT token JWT_SECRET= @@ -131,7 +121,7 @@ MIX_LOG_VIEWER_ROUTE="log-viewer" MIX_ADMIN_PANEL_ROUTE_PREFIX, MIX_API_ROUTE_PREFIX & MIX_LOG_VIEWER_ROUTE should be different ::: -9. Add the following Badaso guard and auth provider in ```config/auth.php```. Make sure to use Badaso guard as auth default in ```config/auth.php```. +8. Add the following Badaso guard and auth provider in ```config/auth.php```. Make sure to use Badaso guard as auth default in ```config/auth.php```. ```php From 912d4be3b641b0b491ad74cd26ae247b55816e8a Mon Sep 17 00:00:00 2001 From: Miftahurrahmi Date: Fri, 28 Jun 2024 17:09:54 +0700 Subject: [PATCH 08/10] update phpstan --- .github/workflows/phpstan.yml | 10 +++++----- .github/workflows/phpunit.yml | 2 +- .gitpod.yml | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 6a24f73ba..833bbe9f3 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -19,9 +19,9 @@ jobs: strategy: fail-fast: false matrix: - php: [8.0] - laravel: ["8.*"] - laravel-require: [8.0] + php: [8.2] + laravel: ["11.*"] + laravel-require: [11.0] name: "PHP ${{ matrix.php }} / Laravel ${{ matrix.laravel }}" @@ -36,7 +36,7 @@ jobs: extensions: mbstring, pdo_sqlite, fileinfo, gd, php_pgsql coverage: none - # Install legacy factories for Laravel 8/9 + # Install legacy factories for Laravel 11 - name: Install legacy factories for Laravel run: composer create-project laravel/laravel:^${{matrix.laravel-require}} laravel @@ -59,7 +59,7 @@ jobs: # Badaso Core Test PHPStan - name: Badaso core test PHPStan - run: cd laravel && + run: cd laravel && ./vendor/bin/phpstan analyse -c vendor/badaso/core/phpstan.neon.dist diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 290a0e750..ab3f533ef 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -70,7 +70,7 @@ jobs: extensions: mbstring, pdo_sqlite, fileinfo, gd, php_pgsql, json, openssl, php_pcov coverage: xdebug - # Install legacy factories for Laravel 8/9 + # Install legacy factories for Laravel 11 - name: Install legacy factories for Laravel run: composer create-project laravel/laravel:^${{matrix.laravel-require}} laravel diff --git a/.gitpod.yml b/.gitpod.yml index f11c923a0..e80cb6bba 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -2,7 +2,7 @@ image: file: .gitpod.Dockerfile tasks: - name: Starting Project - init: composer create-project laravel/laravel:^8.0 laravel && + init: composer create-project laravel/laravel:^11.0 laravel && mkdir -p laravel/packages/badaso/core && mv {,.}* laravel/packages/badaso/core 2>/dev/null; true && cd laravel && From 9db8ba411da6a5dcf2c572b3aad3077362b79c3a Mon Sep 17 00:00:00 2001 From: Miftahurrahmi Date: Tue, 9 Jul 2024 17:08:12 +0700 Subject: [PATCH 09/10] fixing unit test --- src/Controllers/BadasoCRUDController.php | 2 + src/Database/Schema/SchemaManager.php | 5 +- .../js/layout/admin/footer/Footer.vue | 2 +- tests/Feature/BadasoApiCrudManagementTest.php | 60 ++++++------------- 4 files changed, 23 insertions(+), 46 deletions(-) diff --git a/src/Controllers/BadasoCRUDController.php b/src/Controllers/BadasoCRUDController.php index 5a79d0ee8..f30a6f6ff 100644 --- a/src/Controllers/BadasoCRUDController.php +++ b/src/Controllers/BadasoCRUDController.php @@ -316,6 +316,7 @@ function ($attribute, $value, $fail) { 'required', function ($attribute, $value, $fail) use ($request) { if (! Schema::hasColumn($request->name, $value)) { + $split_attribute = explode('.', $attribute); $split_attribute[2] = 'relation_type'; $field_to_relation = join('.', $split_attribute); @@ -432,6 +433,7 @@ function ($attribute, $value, $fail) use ($request) { return ApiResponse::success($new_data_type); } catch (Exception $e) { + // dd($e); DB::rollBack(); return ApiResponse::failed($e); diff --git a/src/Database/Schema/SchemaManager.php b/src/Database/Schema/SchemaManager.php index 80e7982f5..d097ebf71 100644 --- a/src/Database/Schema/SchemaManager.php +++ b/src/Database/Schema/SchemaManager.php @@ -60,9 +60,7 @@ public static function listTableDetails($table_name) $columns = $sm->listTableColumns($table_name); $foreign_keys = []; - if (static::registerConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) { - $foreign_keys = $sm->listTableForeignKeys($table_name); - } + $foreign_keys = $sm->listTableForeignKeys($table_name); $indexes = $sm->listTableIndexes($table_name); @@ -129,7 +127,6 @@ public static function describeTable($table_name) $indexType = array_values($column_array['indexes'])[0]['type']; $column_array['key'] = substr($indexType, 0, 3); } - return $column_array; }); } diff --git a/src/resources/js/layout/admin/footer/Footer.vue b/src/resources/js/layout/admin/footer/Footer.vue index fa17208de..092426194 100644 --- a/src/resources/js/layout/admin/footer/Footer.vue +++ b/src/resources/js/layout/admin/footer/Footer.vue @@ -2,7 +2,7 @@