500 * 1024 * 1024) { $limit = "-1";} $actual_memory = ini_get("memory_limit"); if ($actual_memory != "-1" && $actual_memory != "4G" && $size > 2000000 ) { ini_set("memory_limit", $limit); ini_set("max_execution_time", "600"); } } } } if(! function_exists("setup_db_connection")) { function setup_db_connection(int $tenant = null) { if(! defined("_MYSQL_DT")) { define("_MYSQL_DT", "Y-m-d H:i:s"); } try { \Illuminate\Support\Facades\DB::connection('mysql')->getPdo(); } catch (\Throwable $th) { get_config_variables(); config([ "database.connections.mysql.host" => config("json.db_host", null), "database.connections.mysql.database" => config("json.db_name", null), "database.connections.mysql.username" => config("json.db_root_user", null), "database.connections.mysql.password" => config("json.db_root_pass", null) ]); \Illuminate\Support\Facades\DB::purge('mysql'); } if(! empty($tenant) && is_numeric($tenant)) { try { \Illuminate\Support\Facades\DB::connection('mysql-tenant')->getPdo(); if(config('json.prefix_db') . "_ente{$tenant}" !== config("database.connections.mysql-tenant.database")) { throw new \Exception("We must change db connection"); } } catch (\Throwable $th) { get_config_variables(); config([ "database.connections.mysql-tenant.host" => config("json.db_host", null), "database.connections.mysql-tenant.database" => config('json.prefix_db') . "_ente{$tenant}", "database.connections.mysql-tenant.username" => config("json.db_root_user", null), "database.connections.mysql-tenant.password" => config("json.db_root_pass", null), ]); \Illuminate\Support\Facades\DB::purge('mysql-tenant'); } } } } if(! function_exists("get_config_variables")) { function get_config_variables() { $path = \dirname(base_path()); if (\file_exists("{$path}/config.json")) { $config = \file_get_contents("{$path}/config.json"); if(is_json($config)) { $config = json_decode($config, true); config(["json" => $config]); } } } } if(! function_exists("get_json_configuration_info")) { function get_json_configuration_info(String $filename) { $filename = str_replace('.json', '', $filename); $path = \dirname(base_path()); $path = "{$path}/json/{$filename}.json"; if (\file_exists($path)) { $info = \file_get_contents($path); if(is_json($info)) { $info = json_decode($info, true); config(["info.{$filename}" => $info]); } } } } if(! function_exists("is_json")) { function is_json($string) { json_decode($string); return json_last_error() === JSON_ERROR_NONE; } } if(! function_exists("restore_database_dump")) { function restore_database_dump($host, $username, $password, $database, $dump_path, $command="zcat") { exec("{$command} {$dump_path} | mysql -u {$username} -p{$password} -h {$host} {$database} 2>&1", $output); if(! empty($output) ) { foreach($output as $line) { $line = strtolower($line); if(!str_contains($line, "[warning]") && !str_contains($line, "[warn]")) { throw new \Illuminate\Database\QueryException("Error while restoring dump {$dump_path}", [], new Exception(json_encode($output), 1)); } } } } }