Skip to content

Commit

Permalink
montajes y guiones medios en las urls
Browse files Browse the repository at this point in the history
  • Loading branch information
aacotroneo committed Mar 10, 2015
1 parent 4b98830 commit f35567b
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 10 deletions.
46 changes: 37 additions & 9 deletions src/SIUToba/rest/docs/controlador_docs.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
use SIUToba\rest\lib\ruteador;
use SIUToba\rest\rest;

/**
* @TODO: refactorizar esto - no es critico, pero se fue extendiendo, y no tiene tests (ups)
*
*/
class controlador_docs
{
protected $api_root;
Expand Down Expand Up @@ -96,6 +100,9 @@ protected function add_apis($path)
$reflexion = $this->get_annotaciones_de_path($path);
$metodos = $reflexion->get_metodos();

$montaje = $this->get_montaje_de_path($path);
$prefijo_montaje = $montaje ? '/' . $montaje : '';

foreach ($metodos as $metodo) {
$parametros = $metodo['parametros'];
$nombre_metodo = $metodo['nombre'];
Expand All @@ -117,30 +124,35 @@ protected function add_apis($path)
$params_path = array();
$partes_path = explode('/', $path);

if ($montaje) {
array_shift($partes_path);
}

foreach ($partes_nombre as $parte) {
$partes_path[] = $parte;
}

$nro_parametro = 0;
$api_path = ''; // $path;
$api_path = $prefijo_montaje; // $path;

foreach ($partes_path as $parte) {
$api_path .= "/".$parte;
$parte = str_replace('_', '-', $parte); //no permito '_' en las colecciones
$api_path .= "/" . $parte;
if (isset($parametros[$nro_parametro])) {
$param_name = $parametros[$nro_parametro++];
$api_path .= "/{".$param_name."}";
$api_path .= "/{" . $param_name . "}";
$params_path[] = $this->get_parametro_path($param_name, $parte);
}
}
if ($alias) {
$api_path .= '/'.$alias;
$api_path .= '/' . $alias;
}
////--------------------------------------------------------
$params_query = $reflexion->get_parametros_metodo($metodo, 'query');
$params_body = $reflexion->get_parametros_metodo($metodo, 'body');

$operation = array();
$operation['tags'] = array($path);
$operation['tags'] = array( str_replace('_', '-', $path)); //cambio el _ para mostrarlo
$operation['method'] = strtolower($prefijo_metodo);
$operation['summary'] = $reflexion->get_summary_metodo($metodo);
$operation['description'] = $reflexion->get_notes_metodo($metodo);
Expand Down Expand Up @@ -176,7 +188,7 @@ protected function reordenar_operaciones(&$paths)
$orden_ops = array();
foreach ($paths as $metodo => $detalle) {
//3GET,3PUT,6DELETE,6UPDATE
$orden_ops[] = strlen($metodo).$metodo;
$orden_ops[] = strlen($metodo) . $metodo;
}
array_multisort($orden_ops, SORT_ASC, $paths);
}
Expand Down Expand Up @@ -211,10 +223,10 @@ protected function get_url_de_clase($ruta_absoluta)
$recurso = substr($clase_recurso, strlen($prefijo)); //padre
if ($this->termina_con($recurso, dirname($path_relativo))) {
// /rest/padre/hijo/recurso_hijo.php => /padre/hijo
$url = substr($path_relativo, 0, -strlen($clase_recurso.'.php') - 1);
$url = substr($path_relativo, 0, -strlen($clase_recurso . '.php') - 1);
} else {
// /rest/padre/recurso_hijo.php => /padre/hijo
$url = substr($path_relativo, 0, -strlen($clase_recurso.'.php'));
$url = substr($path_relativo, 0, -strlen($clase_recurso . '.php'));
$url .= $recurso;
}

Expand All @@ -234,6 +246,22 @@ protected function get_annotaciones_de_path($path)
return new anotaciones_docs($archivo['archivo']);
}

protected function get_montaje_de_path($path)
{
$lector = rest::app()->lector_recursos; //new lector_recursos_archivo($this->api_root);

$partes_url = explode('/', $path);

if ($es_montaje = $lector->es_montaje($partes_url[0])) {
$montaje = $partes_url[0];
} else {
$montaje = "";
}

return $montaje;
}


/**
* @param $path
*
Expand All @@ -253,7 +281,7 @@ protected function add_modelos($path)
$specs = $modelo->to_swagger($objeto->_get_modelos());
$this->list['definitions'] = array_merge($this->list['definitions'], $specs);
} else {
rest::app()->logger->debug('El objeto no tiene el metodo _get_modelos. Clase: '.get_class($objeto));
rest::app()->logger->debug('El objeto no tiene el metodo _get_modelos. Clase: ' . get_class($objeto));

return array();
}
Expand Down
7 changes: 6 additions & 1 deletion src/SIUToba/rest/lib/lector_recursos_archivo.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ public function get_directorio_recursos()
*/
public function get_recurso($path, $montaje = '')
{
$prefijo_montaje =!empty($montaje)? DIRECTORY_SEPARATOR.$montaje: '';


//Busco del mas especifico al mas general
while (!empty($path)) {
$recurso = array_pop($path);
if (!empty($path)) {
$ruta_padres = $montaje.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $path);
$ruta_padres = $prefijo_montaje.implode(DIRECTORY_SEPARATOR, $path);
} else {
$ruta_padres = $montaje;
}
Expand Down Expand Up @@ -72,9 +75,11 @@ protected function existe_recurso($path, $name)
$como_carpeta_archivo = $directorio.$name.DIRECTORY_SEPARATOR.$nombre_recurso;

if ($file = $this->obtener_archivo($como_archivo)) {

return $file;
}
if ($file = $this->obtener_archivo($como_carpeta_archivo)) {
// echo $file . "- $directorio - Path:$path - Name: $name\n";
return $file;
}

Expand Down
2 changes: 2 additions & 0 deletions src/SIUToba/rest/lib/ruteador.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function buscar_controlador($method, $url)
if ($key % 2) {
$parametros[] = $valor;
} else {
$valor = str_replace('_', ' ', $valor); //no permito '_' en las colecciones
$valor = str_replace('-', '_', $valor); //paso todos los - de la url a '_'
$colecciones[] = $valor;
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/_ejemplo/rest/a/c/recurso_c.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php


class recurso_c {

function get_list($id_a)
{
}

function get($id_a, $id_c)
{
}

}
27 changes: 27 additions & 0 deletions tests/_ejemplo/rest/a/recurso_a.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

class recurso_a
{

function get_list()
{

}

function get_list__alias()
{

}


function delete($id)
{

}

function get_b_list($id_a)
{
}


}
10 changes: 10 additions & 0 deletions tests/_ejemplo/rest/d_e/recurso_d_e.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

class recurso_d_e
{

function get($id_de)
{

}
}
19 changes: 19 additions & 0 deletions tests/_ejemplo/rest/montaje/d/recurso_d.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php


class recurso_d {

//montaje/d
function get_list()
{
}
//montaje/d/{ID_d}
function get($id_d)
{
}

function get_b($id_d, $id_b)
{
}

}
1 change: 1 addition & 0 deletions tests/lib/ruteadorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public function testGetSubrecursoPadreComplejo()
$this->assertEquals($parametros, $rec->parametros);
}


public function testAliases()
{
$path = 'padre/alias';
Expand Down
Loading

0 comments on commit f35567b

Please sign in to comment.