NxTIC/manager/module/module.php

213 lines
6.1 KiB
PHP
Raw Normal View History

2016-04-18 08:30:40 +00:00
<?php
namespace manager\module;
use \manager\ManagerError;
use \manager\ResourceDispatcher;
2016-04-18 08:30:40 +00:00
class module{
/* PERMET DE TESTER L'API
*
*/
2016-04-18 08:30:40 +00:00
public static function method(){
return array(
'ModuleError' => ManagerError::Success,
'ReceivedArguments' => func_get_args()
2016-04-18 08:30:40 +00:00
);
}
/* PERMET DE TESTER UNE L'ORDRE DES PARAMÈTRES
*
*/
public static function phpunitParams($params){
extract($params);
return array(
'ModuleError' => ManagerError::Success,
'p1' => $p1,
'p2' => $p2
);
}
/* RENVOIE UNE DESCRIPTION EN MARKDOWN DES MODULES DE L'API
*
* @return markdown<String> Description des modules
*
*/
public static function markdown(){
/* [1] Récupération de la configuration
=========================================================*/
// On récupère le fichier et on le parse
$modules = json_decode( ResourceDispatcher::getResource('f/json/modules/conf'), true );
// Gestion de l'erreur de parsage
if( $modules == null )
return array( 'ModuleError' => ManagerError::ParsingFailed );
/* [2] Mise en forme de la liste des modules
=========================================================*/
$markdown = "## Module List\n";
foreach($modules as $moduleName=>$moduleData)
$markdown .= "- $moduleName\n";
/* [3] Mise en forme des méthodes des modules
=========================================================*/
$markdown .= "----\n## Method List & Description\n";
$count = 1;
foreach($modules as $moduleName=>$moduleData){
$markdown .= "### $count - '$moduleName' methods\n";
foreach($moduleData as $methodName=>$methodData)
$markdown .= "`$methodName` - ".$methodData['description']."\n";
$markdown .= "----\n";
$count++;
}
/* [n] Gestion du retour
=========================================================*/
return array(
'ModuleError' => ManagerError::Success,
'headers' => array(
'Content-Type' => 'text/markdown; charset=utf-8',
'Content-Transfer-Encoding' => 'binary',
'Content-Disposition' => 'attachment; filename=NxTIC.apib',
'Pragma' => 'no-cache',
'Expires' => '0'
),
'body' => $markdown
);
}
2016-04-18 08:30:40 +00:00
/* RENVOIE UNE DOC API_BLUEPRINT DE L'API
*
* @return apiBlueprint<String> Description des modules au format API Blueprint
*
*/
public static function apiBlueprint(){
/* [1] Récupération de la configuration
=========================================================*/
// On récupère le fichier et on le parse
$modules = json_decode( ResourceDispatcher::getResource('f/json/modules/conf'), true );
2016-04-18 08:30:40 +00:00
// Gestion de l'erreur de parsage
if( $modules == null )
return array( 'ModuleError' => ManagerError::ParsingFailed );
/* [2] Pour chaque module
=========================================================*/
$content = '';
foreach($modules as $module=>$methods){
$content .= "## $module [/$module] \n\n";
/* [3] Pour chaque méthode
=========================================================*/
foreach($methods as $methName=>$method){
/* (1) Description */
$content .= "### $methName [POST /$module/$methName]\n\n";
$content .= $method['description']."\n";
if( count($method['permissions']) > 0)
$content .= '> Permissions `'.implode('``', $method['permissions'])."`\n\n";
2016-06-03 15:29:37 +00:00
// Liste des paramètres
if( isset($method['parameters']) && count($method['parameters']) > 0 ){
// On explicite tous les paramètres
$content .= "+ Parameters\n\n";
foreach($method['parameters'] as $argName=>$argument){
$optional = isset($argument['optional']) && $argument['optional'] === true;
$content .= " + _${argName}_ (${argument['type']}, ".( $optional ? 'optional' : 'required' ).") - ${argument['description']}\n";
}
$content .= "\n";
}
/* (2) Requête */
$content .= "+ Request (multipart/form-data)\n\n";
2016-06-03 15:29:37 +00:00
// Header
$content .= " + Headers\n\n";
$content.= " Authorization: Digest {yourAccessToken}\n\n";
2016-06-03 15:29:37 +00:00
if( isset($method['parameters']) && count($method['parameters']) > 0 ){
2016-06-03 15:29:37 +00:00
// Body
$content .= " + Body\n\n ";
$c = 0;
foreach($method['parameters'] as $argName=>$argument){
if( $c > 0 ) $content .= '&';
$content .= "$argName=_${argName}_";
$c++;
}
2016-06-03 15:29:37 +00:00
$content .= "\n\n";
// Schema
$content .= " + Schema\n\n";
$content .= " {\n";
foreach($method['parameters'] as $argName=>$argData)
$content .= " \"$argName\": _${argName}_\n";
$content .= " }\n\n";
}
/* (3) Réponse */
$content .= "+ Response 200 (application/json)\n\n";
if( isset($method['output']) && count($method['output']) > 0 ){
2016-06-03 15:29:37 +00:00
// Body
$content .= " + Body\n\n";
$content .= " {\n";
2016-06-03 15:29:37 +00:00
foreach($method['output'] as $outName=>$outData)
$content .= " \"$outName\": @$outName\n";
$content .= " }\n\n";
2016-06-03 15:29:37 +00:00
// Schema
$content .= " + Schema\n\n";
$content .= " {\n";
foreach($method['output'] as $outName=>$outData)
$content .= " \"$outName\": @$outName\n";
$content .= " }\n\n";
// On explicite tous les paramètres
$content .= " + Attributes (object)\n\n";
foreach($method['output'] as $outName=>$outData)
$content .= " + $outName (${outData['type']}) - ${outData['description']}\n";
}
2016-06-03 15:29:37 +00:00
$content .= "\n\n";
}
}
return array(
'ModuleError' => ManagerError::Success,
'headers' => array(
'Content-Type' => 'application/octet-stream; charset=utf-8',
'Content-Transfer-Encoding' => 'binary',
'Content-Disposition' => 'attachment; filename=NxTIC.apib',
'Pragma' => 'no-cache',
'Expires' => '0'
),
'body' => $content
);
}
2016-04-18 08:30:40 +00:00
}
2016-04-18 08:30:40 +00:00
?>