load(); function env($key) { global $_ENV; return array_key_exists($key, $_ENV) ? $_ENV[$key] : null; } $database = new Medoo([ 'type' => 'mysql', 'host' => env("DB_HOST"), 'database' => env("DB_DATABASE"), 'username' => env("DB_USER"), 'password' => env("DB_PASSWORD") ]); $data = json_decode(file_get_contents("php://input"), true); $routesDirectory = __DIR__ . "/routes"; $getFolderContentUtil = null; $getFolderContentUtil = function($dir) use($routesDirectory, &$getFolderContentUtil, $router, $data, $database) { $files = scandir($dir); $filter = '/\.php$/'; foreach($files as $key => $value) { $path = realpath($dir.DIRECTORY_SEPARATOR.$value); $pathWithoutRoot = str_replace($routesDirectory, '', $path); $pathWithoutRelativeRoot = str_replace($dir, '', $path); if(!is_dir($path)) { if(empty($filter) || preg_match($filter, $path)) { $error = false; $response = require $path; if(!empty($response) && is_array($response)) { foreach($response as $key => $fn) { $router->{$key}(str_replace(".php", '', $pathWithoutRelativeRoot), function() use($router, $fn, $data, $error, $database) { $responseResult = $fn($data, getallheaders(), $database, $error, $router); if($responseResult) { $responseResult["status"] = array_key_exists("status", $responseResult) ? $responseResult["status"] : 200; http_response_code($responseResult["status"]); echo json_encode($responseResult["data"]); exit(); } }); } } } } else if($value != "." && $value != "..") { $router->mount($pathWithoutRelativeRoot, function() use($getFolderContentUtil, $path, $filter) { $getFolderContentUtil($path); }); } } }; $getFolderContentUtil($routesDirectory); $router->run();