Skip to content

Instantly share code, notes, and snippets.

@Topener
Last active August 29, 2015 14:04
Show Gist options
  • Save Topener/5a60f5aece9335a1329b to your computer and use it in GitHub Desktop.
Save Topener/5a60f5aece9335a1329b to your computer and use it in GitHub Desktop.
Flight PHP auto generate from YAML file
- name: "/user"
method : POST
function:
User
create
fields:
username
email
password
- name: "/user/@id"
method: GET
function:
User
get
<?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