prod-releaser.php/build/api/core/Documentation.php

250 lines
5.7 KiB
PHP
Raw Normal View History

<?php
namespace api\core;
use \error\core\Error;
use \error\core\Err;
use \api\core\Request;
use \api\core\Config;
class Documentation{
/* (1) Attributes
---------------------------------------------------------*/
/* (1) Static */
/* (2) Instance */
/* (2) Builds the documentation
*
---------------------------------------------------------*/
public static function generate(Request $rq=null){
/* (1) Get data from config
---------------------------------------------------------*/
/* (1) If no index for this path -> exit */
if( !isset(Config::get()->index[$rq->get('id')['path']]) )
return new Response(new Error(Err::WrongPathModule));
/* (2) Instance store: configuration for this path */
$cfg = Config::get()->index[$rq->get('id')['path']];
/* (2) For each method
---------------------------------------------------------*/
foreach($cfg as $method=>$spec){
/* (1) Build uri with args */
2017-12-13 11:44:39 +00:00
$cfg[$method]['uri_scheme'] = self::uri_scheme($rq->get('id')['path'], $spec);
/* (2) Build human-readable permission list */
$cfg[$method]['permissions'] = "accessible with:".self::permissions($spec);
unset($cfg[$method]['per']);
/* (3) Build ease parameter list */
$cfg[$method]['parameters'] = self::parameters($spec);
unset($cfg[$method]['par']);
}
$response = new Response();
// $response->append('uri', $built_uri);
$response->append('methods', $cfg);
return $response;
}
/* (3) Builds uri with GET parameter inside
*
* @uri<String> Base URI
* @spec<array> Specification
*
---------------------------------------------------------*/
2017-12-13 11:44:39 +00:00
private static function uri_scheme($uri=null, $spec=null){
/* (1) If no param return nothing */
if( !isset($spec['par']) || !is_array($spec['par']) || count($spec['par']) <= 0 )
return;
/* (2) If uri is only '/', reset to nothing */
if( $uri == '/' )
$uri = '';
/* (1) Manage GET parameters
---------------------------------------------------------*/
/* (1) Get highest 'URL' parameter available in the spec */
$highest = 0;
foreach($spec['par'] as $pname=>$pspec){
$is_url = strlen($pname) > 3 && substr($pname, 0, 3) == 'URL' && is_numeric(substr($pname, 3));
if( $is_url && intval(substr($pname, 3)) > $highest )
$highest = intval(substr($pname, 3));
}
/* (2) Add each GET parameter (even if not in spec) */
for( $i = 0 ; $i <= $highest ; $i++ ){
/* (2.1) If not in the spec -> set default to empty */
if( !isset($spec['par']["URL$i"]) ){
$uri .= '/';
continue;
}
$pspec = $spec['par']["URL$i"];
/* (2.2) If 'rename' set the rename content */
if( isset($pspec['ren']) && is_string($pspec['ren']) ){
$uri .= '/@'.$pspec['ren'];
continue;
}
/* (2.3) If no rename set the default name */
$uri .= "/@url$i";
}
return $uri;
}
/* (4) Builds a readable permission list
*
* @spec<array> Specification
*
---------------------------------------------------------*/
private static function permissions($spec=null){
/* (1) If no perm return nothing */
if( !isset($spec['per']) || !is_array($spec['per']) || count($spec['per']) <= 0 )
return 'anyone';
/* (1) Manage permission groups
---------------------------------------------------------*/
$perm = '';
$first_or = true;
foreach($spec['per'] as $or){
/* (1) Ignore non-array values */
if( !is_array($or) )
continue;
$perm .= !$first_or ? ') or' : '';
$first_or = false;
$first_and = true;
/* (2) Manage AND */
foreach($or as $and){
$perm .= $first_and ? ' (' : ' and ';
$perm .= "$and";
$first_and = false;
}
}
if( !$first_or )
$perm .= ')';
return $perm;
}
/* (5) Builds a readable parameter list
*
* @spec<array> Specification
*
---------------------------------------------------------*/
private static function parameters($spec=null){
/* (1) If no param return nothing */
if( !isset($spec['par']) || !is_array($spec['par']) || count($spec['par']) <= 0 )
return;
/* (2) Initialize parameter output */
$param = [
'GET' => [],
'POST' => []
];
$post_index = -1;
/* (1) Set 'URL' parameter available in the spec */
foreach($spec['par'] as $pname=>$pspec){
/* (1) Manage POST parameters
---------------------------------------------------------*/
if( strlen($pname) >= 3 && substr($pname, 0, 3) == 'URL' && is_numeric(substr($pname, 3)) ){
/* (1) Get the URL index (position) */
$index = intval(substr($pname,3));
/* (2) Manage 'rename' property */
$name = ( isset($pspec['ren']) && is_string($pspec['ren']) ) ? $pspec['ren'] : $pname;
/* (3) Set default values */
$param['GET'][$index] = [
'name' => $name,
'required' => !isset($pspec['opt']) || $pspec['opt'] !== true
];
/* (4) Manage 'default' property */
if( !$param['GET'][$index]['required'] )
$param['GET'][$index]['default'] = isset($pspec['def']) ? $pspec['def'] : null;
continue;
}
/* (2) Manage GET+POST parameters
---------------------------------------------------------*/
/* (1) Get the POST index */
$post_index = $post_index + 1;
/* (2) Manage 'rename' property */
$name = ( isset($pspec['ren']) && is_string($pspec['ren']) ) ? $pspec['ren'] : $pname;
/* (3) Set default values */
$param['POST'][$post_index] = [
'name' => $name,
'required' => !isset($pspec['opt']) || $pspec['opt'] !== true
];
/* (4) Manage 'default' property */
if( !$param['POST'][$post_index]['required'] )
$param['POST'][$post_index]['default'] = isset($pspec['def']) ? $pspec['def'] : null;
}
return $param;
}
}