Last active
August 29, 2015 14:04
-
-
Save Topener/5a60f5aece9335a1329b to your computer and use it in GitHub Desktop.
Flight PHP auto generate from YAML file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: "/user" | |
method : POST | |
function: | |
User | |
create | |
fields: | |
username | |
password | |
- name: "/user/@id" | |
method: GET | |
function: | |
User | |
get |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// load the Flight.php file | |
require __DIR__ . '/flight/Flight.php'; | |
// load the API config | |
$api_config = yaml_parse_file(__DIR__ .'/api.yaml'); | |
// loop through the endpoint from the YAML file | |
foreach ($api_config as $endpoint){ | |
// generate the ROUTE | |
Flight::route($endpoint['method'] . ' ' . $endpoint['name'], function() use ($endpoint){ | |
// are there query dependencies (POST/GET) | |
if (isset($endpoint['fields'])){ | |
foreach ($endpoint['fields'] as $reqField){ | |
if (!isset(Flight::request()->query[$reqField])){ | |
// If a dependency is missing, halt the request with 412 | |
Flight::halt(412, 'precondition failed'); | |
return false; | |
} | |
} | |
} | |
// open the static function with the arguments passed from flight | |
$endpoint['function'][0]::$endpoint['function'][1](func_get_args()); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment