diff --git a/src/Config/badaso-hidden-tables.php b/src/Config/badaso-hidden-tables.php index a13d1e67..0768694c 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 6dffcc1e..5a79d0ee 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 a1c3d597..80e7982f 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 c575cb70..41a05be5 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)