dc-rest/inc/class.rest.query.get.specs.php

114 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2017-05-01 00:51:42 +02:00
<?php
2017-09-08 09:18:48 +02:00
class RestQueryGetSpecs extends RestDocumentation //some common functions betwin this 2 class
2017-05-01 00:51:42 +02:00
{
2017-09-08 09:18:48 +02:00
protected function getSwaggerArray(){
/*
* need $this -> chapter initied
* return the swagger doc (as an array)
* with the real basePath and the URL
* if chapter is 'all' it will concat all swagger definitions.
*/
global $core;
if(!ctype_alpha($this->chapter)){
return false;
}
if($this->chapter == 'all'){
//concat all specs array in one
$url = parse_url($core->blog->url.$core->url->getBase('rest'));
preg_match('/https?:\/\/[^\/]*(\/.*)/',$core->blog->url.$core->url->getBase('rest'),$end);
$specs = array(
"swagger" => "2.0",
"info:" => array(
"title" => "Dotclear API",
"description" => "Manage yours blogs with this API",
"version" => "0.1"
),
"host" => $url['host'],
"schemes" => array(
$url['scheme']
),
"basePath" =>$end[1],
"produces" => array(
"application/json"
),
"paths" =>array(),
"definitions" => array(),
);
$files = scandir(path::real(dirname(__FILE__).'/../documentation/'));
foreach($files as $file){
if (preg_match('/^swagger\-(.*)\.json$/',$file)){
$swag = json_decode(
file_get_contents(
path::real(dirname(__FILE__).'/../documentation/').'/'.$file
),true
);
$specs["paths"] = array_merge($specs["paths"], $swag["paths"]);
$specs["definitions"] = array_merge($specs["definitions"], $swag["definitions"]);
}
}
return $specs;
}
$files = scandir(path::real(dirname(__FILE__).'/../documentation/'));
foreach($files as $file){
if($file == 'swagger-'.$this->chapter.'.json'){
//return path::real(dirname(__FILE__).'/../documentation/').'/'.$file;
$specs = json_decode(
file_get_contents(
path::real(dirname(__FILE__).'/../documentation/').'/'.$file
),true
);
//change some parameters
$url = parse_url($core->blog->url.$core->url->getBase('rest'));
$specs['host'] = $url['host'];
$specs['schemes'] = $url['scheme'];
preg_match('/https?:\/\/[^\/]*(\/.*)/',$core->blog->url.$core->url->getBase('rest'),$end);
$specs['basePath'] = $end[1];
return $specs;
}
}
return false;
}
public function __construct($args){
2017-05-05 21:46:14 +02:00
global $core;
2017-05-18 21:49:46 +02:00
$this->response_code = 200;
2017-05-05 21:46:14 +02:00
$this->required_perms = 'unauth';
if($this->is_allowed() === false){
return;
}
2017-09-08 09:18:48 +02:00
if($args == "specs"){
$this->chapter = 'all';
}else{
list($osef,$this->chapter) = explode("/",$args);
}
$specs = $this->getSwaggerArray();
if($specs === false){
$this->response_code = 404;
$this->response_message = array(
'error' => 'Not found'.$body,
'code' => 404
);
return;
}
2017-05-05 21:46:14 +02:00
$this->response_message = $specs;
return;
}
2017-09-08 09:18:48 +02:00
}