Skip to content

Commit

Permalink
Merge branch 'release/v2.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
enfoqueNativo committed May 18, 2020
2 parents 6b015ee + f7ba8b1 commit 035decd
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ vendor
composer.lock
nbproject/*
.idea
/node_modules
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# CHANGELOG
## v 2.0.3 18/05/2020
* Agrega template para un recurso que de información de la API (sirve como healthcheck)
* Agrega posibilidad de especificar un logo para la documentacion
* Agrega mecanismo para hacer univoco el campo operationId
* Agrega parametrización de info básica para armado documentación en JSON

## v 2.0.2 06/10/2017
* Fix encoding mensaje error en respuesta

Expand Down
21 changes: 15 additions & 6 deletions src/SIUToba/rest/docs/anotaciones_docs.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public function __construct($archivo)
$this->get_annotations($this->reflexion);
}

/**
* Devuelve el nombre (con Namespace) de la clase
* @return string
*/
public function get_nombre_clase()
{
return $this->reflexion->getNamespaceName() .'/'. $this->reflexion->getName();
}

/**
* Parsea una reflexion (metodo, clase) y devuelve las anotaciones en un arreglo.
*
Expand Down Expand Up @@ -227,7 +236,7 @@ public function get_respuestas_metodo($metodo)
$matches = array();
//200 [array] $tipo descripcion
$resultado = preg_match("/(\d{3})?\s*([ary]*)\s*(\{[\":\w\$\s]+\})?\s*(.*)/i", $respuesta, $matches);
if (0 === $resultado || false === $resultado) {
if (0 === $resultado || false === $resultado) {
continue;
}

Expand Down Expand Up @@ -273,16 +282,16 @@ protected function get_tipo_datos($tipo)
return;
}

$refs = explode(':', $tipo);
$refs = explode(':', $tipo);
if (false === $refs) {
$tipoRef = array('type' => trim($tipo)); //Basic type - no name
} else {
if (substr($refs[0], 0, 1) == '$') {
$tipoRef = array('$ref' => "#/definitions/". trim($refs[1])); //Referred type {"$ref": "Defined@Model"}
} else {
$tipoRef = array('type' => trim($refs[1])); //Basic type - named {"id" : "integer"}
}
}
return $tipoRef;
$tipoRef = array('type' => trim($refs[1])); //Basic type - named {"id" : "integer"}
}
}
return $tipoRef;
}
}
33 changes: 29 additions & 4 deletions src/SIUToba/rest/docs/controlador_docs.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class controlador_docs
protected $api_url;

protected $list;
protected $settings = array('titulo' => 'Api Title', 'version' => '1.0');

public function __construct($api_root, $api_url)
{
Expand All @@ -39,6 +40,16 @@ public function add_punto_partida($path)
}
}

/**
* Permite fijar las opciones para la generacion de documentacion
*
* @param array $settings Array asociativo conteniendo las opciones
* ['titulo' => '', 'version' => '', 'url_logo' => '',..]
*/
public function set_config($settings=array())
{
$this->settings = \array_merge($this->settings, $settings);
}

/**
* Retorna la documentacion en formato swagger para el path. Si el path
Expand All @@ -59,9 +70,10 @@ protected function getResourceList()
$list = array();
$this->list = & $list;
$list['swagger'] = "2.0";
$list['info'] = array('title' => 'API Title', 'version' => '1.0'); //TODO: Read from settings
$list['info'] = array('title' => $this->settings['titulo'], 'version' => $this->settings['version']);
$list['basePath'] = $this->api_url;
$list['produces'] = array("application/json");
$list = $this->add_extension_logo($list);

$this->list['paths'] = array();
$this->list['definitions'] = array();
Expand Down Expand Up @@ -116,6 +128,7 @@ protected function add_apis($path)
/** @var $reflexion anotaciones_docs */
$reflexion = $this->get_annotaciones_de_path($path);
$metodos = $reflexion->get_metodos();
$nombre_clase = $reflexion->get_nombre_clase();

$montaje = $this->get_montaje_de_path($path);
$prefijo_montaje = $montaje ? '/' . $montaje : '';
Expand Down Expand Up @@ -177,7 +190,7 @@ protected function add_apis($path)
$operation['summary'] = $reflexion->get_summary_metodo($metodo);
$operation['description'] = $reflexion->get_notes_metodo($metodo);

$operation['operationId'] = $nombre_metodo;
$operation['operationId'] = "$nombre_clase:{$metodo['nombre']}";
$operation['parameters'] = array_merge($params_path, $params_body, $params_query);

$operation['responses'] = $reflexion->get_respuestas_metodo($metodo);
Expand Down Expand Up @@ -312,7 +325,7 @@ protected function add_modelos($path)
* @param $params List of body params
* @return array List of body params with schema definitions
*/
protected function add_tipos_en_modelo($params)
protected function add_tipos_en_modelo($params)
{
$non_predefined_types = array_keys($this->list['definitions']);
$param_keys = array_keys($params);
Expand All @@ -321,7 +334,7 @@ protected function add_tipos_en_modelo($params)
$params[$key]['schema'] = array('$ref' => "#/definitions/". trim($params[$key]['type']));
}
}

return $params;
}

Expand All @@ -346,4 +359,16 @@ protected function empieza_con($prefijo, $string)
{
return substr($string, 0, strlen($prefijo)) === $prefijo;
}

protected function add_extension_logo($list)
{
//Agrega el logo si esta presente
if (isset($this->settings['url_logo'])) {
$valid = (false !== filter_var($this->settings['url_logo'], FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));
if ($valid) {
$list['info']['x-logo'] = array('url' => $this->settings['api_logo']);
}
}
return $list;
}
}
10 changes: 10 additions & 0 deletions src/SIUToba/rest/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static function get_default_settings()
'http.version' => '1.1',
// API version
'api_version' => '1.0.0',
'api_titulo' => 'Api Reference',
);
}

Expand Down Expand Up @@ -366,8 +367,17 @@ protected function loggear_acceso_ok($usuario)
*/
protected function mostrar_documentacion($url)
{
$config = [
'titulo' => $this->container['settings']['api_titulo'],
'version' => $this->container['settings']['api_version'],
];
if (isset($this->container['settings']['logo'])) {
$config['url_logo'] = $this->container['settings']['logo'];
}

$this->logger->debug("Iniciando documentacion");
$controlador = $this->controlador_documentacion;
$controlador->set_config($config);
$url = strstr($url, '/');
$controlador->get_documentacion($url);
}
Expand Down
40 changes: 40 additions & 0 deletions src/SIUToba/rest/templates/template_info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace SIUToba\rest\templates;

use SIUToba\rest\rest;
use SIUToba\rest\lib\modelable;
use SIUToba\rest\lib\rest_error_interno;

class template_info implements modelable
{
public static function _get_modelos(): array
{
return array('info' => [
'version' => array('type' => 'string'),
'api_version' => array('type' => 'string'),
'api_major' => array('type'=> 'string'),
'api_minor' => array('type' => 'string')
]);
}

/**
* Devuelve informacion acerca de la API
*
* @responses 200 {$ref:info} OK
*/
public function get()
{
$version = rest::app()->config('version');
$api = rest::app()->config('api_version');
$api_major = rest::app()->config('api_major');
$api_minor = rest::app()->config('api_minor');

if (is_null($version) || is_null($api) || is_null($api_major) || is_null($api_minor)) {
rest::response()->error_negocio(['La información solicitada no esta disponible'], 500);
} else {
$datos = array( 'version' => $version,'api_version' => $api,
'api_major' => $api_major,'api_minor' => $api_minor);
rest::response()->get_list(array($datos));
}
}
}

0 comments on commit 035decd

Please sign in to comment.