Skip to content

Commit

Permalink
Merge branch 'task/v2/laravel11' of https://github.com/uasoft-indones…
Browse files Browse the repository at this point in the history
…ia/badaso into task/v2/laravel11
  • Loading branch information
miftahurrahmi committed Jun 28, 2024
2 parents 5e0a72a + 66db609 commit 0e773fb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/Config/badaso-hidden-tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
'jobs',
'job_batches',
'password_reset_tokens',
'sessions'
'sessions',
];
59 changes: 29 additions & 30 deletions src/Controllers/BadasoCRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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]));
}
},
Expand All @@ -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}"]));
Expand All @@ -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,
]));
Expand Down Expand Up @@ -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) {
Expand All @@ -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]));
}
},
Expand All @@ -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}"]));
Expand All @@ -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,
]));
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
}
Expand All @@ -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();
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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/',
Expand Down
25 changes: 13 additions & 12 deletions src/Database/Schema/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -30,7 +30,7 @@ public static function getDatabaseConnection()

public static function tableExists($table)
{
if (!is_array($table)) {
if (! is_array($table)) {
$table = [$table];
}

Expand All @@ -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;
Expand All @@ -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 = [];
Expand All @@ -66,29 +66,29 @@ 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'],
];
}

if ($driver_name == 'sqlite') {
$connectionParams = [
'path' => $databaseConfig['database'],
'driver' => 'pdo_' . $driver_name,
'driver' => 'pdo_'.$driver_name,
];
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -160,7 +160,7 @@ public static function getDoctrineTable($table)
{
$table = trim($table);

if (!static::tableExists($table)) {
if (! static::tableExists($table)) {
throw SchemaException::tableDoesNotExist($table);
}

Expand All @@ -175,6 +175,7 @@ public static function getDoctrineColumn($table, $column)
public static function getDoctrineForeignKeys($table)
{
$sm = static::registerConnection()->createSchemaManager();

return $sm->listTableForeignKeys($table);
}
}
15 changes: 7 additions & 8 deletions src/Database/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -46,7 +45,7 @@ public static function getPlatformTypes()
return static::$platform_types;
}

if (!static::$custom_type_registered) {
if (! static::$custom_type_registered) {
static::registerCustomPlatformTypes();
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0e773fb

Please sign in to comment.