Skip to content

Commit

Permalink
Composer updates
Browse files Browse the repository at this point in the history
Added ability to annotate CRUD routers
Added functionality to secure and cache dynamic routes
  • Loading branch information
andrevanzuydam committed Jun 30, 2024
1 parent 7e20c38 commit cee59af
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 293 deletions.
125 changes: 70 additions & 55 deletions Tina4/Api/Swagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class Swagger implements \JsonSerializable
{
public $root;
public $annotation;
public $subFolder;
public $swagger = [];
public $ormObjects = []; //define which objects should be returned in the schema
Expand All @@ -39,27 +40,44 @@ public function __construct($root = null, $title = "Open API", $apiDescription =
}

$this->subFolder = $subFolder;
$this->annotation = new Annotation();

$paths = (object)[];

foreach ($arrRoutes as $arId => $route) {
$route["routePath"] = str_replace("//", "/", $this->subFolder . $route["routePath"]);

$method = strtolower($route["method"]);
//echo $method;

if (!empty($route["class"])) {
$reflectionClass = new \ReflectionClass($route["class"]);
$reflection = $reflectionClass->getMethod($route["function"]);
} else {
$reflection = new \ReflectionFunction($route["function"]);
}


$doc = $reflection->getDocComment();
$annotations = $this->annotation->parseAnnotations($doc);

if (!empty($reflection) && !empty($reflection->getClosureScopeClass()) && $reflection->getClosureScopeClass()->name === "Tina4\Crud") {
$reflectionCaller = new \ReflectionFunction($route["caller"]["args"][2]);
$crudDocumentation = $reflectionCaller->getDocComment();
$crudAnnotations = $this->annotation->parseAnnotations($crudDocumentation, "");

foreach ($annotations as $annotation => $annotationValue) {

if (!empty($crudAnnotations[$annotation])) {
$annotations[$annotation][0] = str_replace("{" . $annotation . "}", $crudAnnotations[$annotation][0], $annotationValue[0]);
$annotations[$annotation][0] = str_replace("{path}", $route["caller"]["args"][0], $annotations[$annotation][0]);
} else {
if ($annotation === "example") {
$annotations[$annotation][0] = (new \ReflectionClass($route["caller"]["args"][1]))->getShortName();
} else {
unset($annotations[$annotation]);
}
}

preg_match_all('#@(.*?)(\r\n|\n)#s', $doc, $annotations);
}
}

$summary = "None";
$description = "None";
Expand All @@ -70,67 +88,64 @@ public function __construct($root = null, $title = "Open API", $apiDescription =
$addParams = [];
$security = [];
$example = null;
foreach ($annotations[0] as $aid => $annotation) {
preg_match_all('/^(@[a-zA-Z]*)([\w\s,\W]*)$/m', $annotation, $matches, PREG_SET_ORDER, 0);
if (count($matches) > 0) {
$matches = $matches[0];
} else {
$matches = null;
}
foreach ($annotations as $annotationName => $annotationValue) {
$annotationValue = $annotationValue[0];

if (!empty($matches[2])) {
$matches[2] = trim($matches[2]);
if ($annotationName === "summary") {
$summary = $annotationValue;
}

if (!empty($matches)) {
if ($matches[1] === "@summary") {
$summary = $matches[2];
} elseif ($matches[1] === "@description") {
$description = str_replace("\n", "", $matches[2]);
} elseif ($matches[1] === "@tags") {
$tags = explode(",", $matches[2]);
foreach ($tags as $tid => $tag) {
$tags[$tid] = str_replace("\n", "", $tag);
}
} elseif ($matches[1] === "@queryParams" || $matches[1] === "@params") {
$queryParams = explode(",", $matches[2]);
} elseif ($matches[1] === "@example") {


$this->ormObjects[] = trim(str_replace("\n", "", "\\" . $matches[2]));
$example = [];
$className = trim(str_replace("\n", "", "\\" . $matches[2]));


if (class_exists($className)) {
$exampleObject = (new $className);

if (method_exists($exampleObject, "asArray")) {
$example["data"] = (object)$exampleObject->asArray();
$fields = $exampleObject->getFieldDefinitions();
$properties = (object)[];
if ($fields !== null) {
foreach ($fields as $field) {
$properties->{$field->fieldName} = (object)["type" => $field->dataType];
}
else
if ($annotationName === "description")
{
$description = str_replace("\n", "", $annotationValue);
}
else
if ($annotationName === "tags")
{
$tags = explode(",", $annotationValue);
foreach ($tags as $tid => $tag) {
$tags[$tid] = trim(str_replace("\n", "", $tag));
}
}
else
if ($annotationName === "params" || $annotationName === "queryParams")
{
$queryParams = explode(",", $annotationValue);
}
else
if ($annotationName === "example") {
$this->ormObjects[] = trim(str_replace("\n", "", "\\" . $annotationValue));
$example = [];
$className = trim(str_replace("\n", "", "\\" . $annotationValue));

if (class_exists($className)) {
$exampleObject = (new $className);

if (method_exists($exampleObject, "asArray")) {
$example["data"] = (object)$exampleObject->asArray();
$fields = $exampleObject->getFieldDefinitions();
$properties = (object)[];
if ($fields !== null) {
foreach ($fields as $field) {
$properties->{$field->fieldName} = (object)["type" => $field->dataType];
}
$example["properties"] = $properties;
} else {
$example["data"] = (object)json_decode(json_encode($exampleObject));
$example["properties"] = (object)[];
}
$example["properties"] = $properties;
} else {
$className = substr($className, 1);
$example["data"] = json_decode($className);
$example["data"] = (object)json_decode(json_encode($exampleObject));
$example["properties"] = (object)[];
}

} else {
$className = substr($className, 1);
$example["data"] = json_decode($className);
$example["properties"] = (object)[];
}
}

if ($matches[1] === "@secure" || $method != "GET") {
$security = [(object)["bearerAuth" => []]];
}
if ($annotationName === "@secure" || $method != "GET") {
$security = [(object)["bearerAuth" => []]];
}

}

if ($summary === "None" && $description !== "None") {
Expand Down
69 changes: 38 additions & 31 deletions Tina4/Routing/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,17 @@ public static function getObjects(Request $request)
* @param ORM $object
* @param $function
* @param bool $secure
* @params $secure
* @param bool $cached
*/
public static function route($path, ORM $object, $function, $secure = false): void
public static function route($path, ORM $object, $function, bool $secure = false, bool $cached = false): void
{
list(, $caller) = debug_backtrace(false);

//What if the path has ids in it ? /store/{id}/{hash}
/**
* @description {$path} CRUD
* @tags CRUD
* @secure
* @no-cache
* @description {description} for {path}
* @summary Get all for {path}
* @tags {tags}
*/
Route::get(
$path . "/form",
Expand All @@ -195,12 +196,15 @@ function (Response $response, Request $request) use ($object, $function) {

return $response($htmlResult, HTTP_OK);
}
);
)
->secure($secure)
->cache($cached);

/**
* @description {$path} CRUD
* @tags CRUD
* @no-cache
* @description {description} for {path}
* @summary Post for {path}
* @tags {tags}
* @example {example}
*/
Route::post(
$path,
Expand All @@ -220,13 +224,13 @@ function (Response $response, Request $request) use ($object, $function) {

return $response($jsonResult, HTTP_OK);
}
);
)->secure($secure)
->cache($cached);

/**
* @description {$path} CRUD
* @tags CRUD
* @secure
* @no-cache
* @description {description} for {path}
* @summary Get for {path}
* @tags {tags}
*/
Route::get(
$path,
Expand All @@ -236,14 +240,15 @@ function (Response $response, Request $request) use ($object, $function) {

return $response($jsonResult, HTTP_OK);
}
);
)->secure($secure)
->cache($cached);


/**
* @description {$path} CRUD
* @tags CRUD
* @secure
* @no-cache
* @description {description} for {path}
* @summary Get by Id for {path}
* @tags {tags}
* @example {example}
*/
Route::get(
$path . "/{id}",
Expand All @@ -257,13 +262,14 @@ function (Response $response, Request $request) use ($object, $function) {

return $response($jsonResult, HTTP_OK);
}
);
)->secure($secure)
->cache($cached);

/**
* @description {$path} CRUD
* @tags CRUD
* @secure
* @no-cache
* @description {description} for {path}
* @summary Post for Id for {path}
* @tags {tags}
* @example {example}
*/
Route::post(
$path . "/{id}",
Expand All @@ -281,13 +287,13 @@ function (Response $response, Request $request) use ($object, $function) {

return $response($jsonResult, HTTP_OK);
}
);
)->secure($secure)
->cache($cached);

/**
* @description {$path} CRUD
* @tags CRUD
* @secure
* @no-cache
* @description {description} for {path}
* @summary Delete by Id for {path}
* @tags {tags}
*/
Route::delete(
$path . "/{id}",
Expand All @@ -305,7 +311,8 @@ function (Response $response, Request $request) use ($object, $function) {

return $response($jsonResult, HTTP_OK);
}
);
)->secure($secure)
->cache($cached);
}

/**
Expand Down
Loading

0 comments on commit cee59af

Please sign in to comment.