Skip to content

Commit a4619c1

Browse files
committed
Tidy docs
1 parent e143f42 commit a4619c1

2 files changed

Lines changed: 60 additions & 15 deletions

File tree

Annotations/AnnotationGenerator.php

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ public function generatePluginApiAnnotations(string $pluginName, bool $writeToFi
121121
$pluginMetadata = Proxy::getInstance()->getMetadata()[$className] ?? [];
122122

123123
$annotations = [[sprintf('@OA\Tag(name="%s")', $pluginName)]];
124-
// I decided to not include the description in the tag annotation so that it automatically pulls the API class comment as the description.
125-
// if (!empty($pluginMetadata['__documentation'])) {
126-
// $tagLines = $this->buildLinesForAnnotationObject('@OA\Tag', [
127-
// sprintf('name="%s"', $pluginName),
128-
// sprintf('description="%s"', $this->normaliseDescriptionText($pluginMetadata['__documentation'])),
129-
// ]);
130-
// $this->removeTrailingCommaFromLastLine($tagLines);
131-
// $annotations[] = $tagLines;
132-
// }
124+
// I decided to not include the description in the tag annotation so that it automatically pulls the API class comment as the description.
125+
if (!empty($pluginMetadata['__documentation'])) {
126+
$tagLines = $this->buildLinesForAnnotationObject('@OA\Tag', [
127+
sprintf('name="%s"', $pluginName),
128+
sprintf('description="%s"', $this->normaliseDescriptionText($pluginMetadata['__documentation'])),
129+
]);
130+
$this->removeTrailingCommaFromLastLine($tagLines);
131+
$annotations[] = $tagLines;
132+
}
133133

134134
foreach (array_keys($pluginMetadata) as $metadataMethod) {
135135
if (!$reflectionClass->hasMethod($metadataMethod)) {
@@ -254,11 +254,12 @@ protected function buildAnnotationForMethod(array $rules, string $pluginName, \R
254254

255255
$params = $this->determineParameters($rules, $pluginName, $methodName, $reflectionMethod);
256256
$responses = $this->determineResponses($rules, $pluginName, $methodName, $reflectionMethod, $params);
257+
$description = $this->determineDescription($pluginName, $methodName, $reflectionMethod);
257258

258259
$isPost = !empty($rules['plugins'][$pluginName]['methodsRequiringPost'])
259260
&& in_array($methodName, $rules['plugins'][$pluginName]['methodsRequiringPost']);
260261

261-
return $this->compileOperationLines($path, $opId, $pluginName, $params, $responses, $isPost);
262+
return $this->compileOperationLines($path, $opId, $pluginName, $params, $responses, $description, $isPost);
262263
}
263264

264265
/**
@@ -354,6 +355,24 @@ public function getResponseInfoFromDocBlock(string $docBlock): array
354355
}
355356

356357
/**
358+
* Extract the description/summary from a given docblock
359+
*
360+
* @param string $docBlock The comment block from a method, which hopefully contains a description.
361+
*
362+
* @return string Description/summary extracted from the docblock
363+
*/
364+
public function getDescriptionFromDocBlock(string $docBlock): string
365+
{
366+
$factory = DocBlockFactory::createInstance();
367+
$docBlockObject = $factory->create($docBlock);
368+
369+
return $docBlockObject->getSummary();
370+
}
371+
372+
373+
374+
375+
/**
357376
* This is a helper method for building the path used for an operation annotation. It takes a path template, like
358377
* the one from the config array and populates it with the plugin name and API method name.
359378
*
@@ -578,6 +597,30 @@ protected function determineParameters(array $rules, string $plugin, string $met
578597
];
579598
}
580599

600+
601+
602+
603+
/**
604+
* Get the description/summary of a given method
605+
*
606+
* @param string $plugin Name of the plugin. E.g. TagManager.
607+
* @param string $method The name of the method being annotated.
608+
* @param \ReflectionMethod $reflectionMethod The reflective representation of the method to provide metadata.
609+
*
610+
* @return string Description of the method
611+
*/
612+
protected function determineDescription(string $plugin, string $method, \ReflectionMethod $reflectionMethod): string
613+
{
614+
$description = '';
615+
$docBlock = $reflectionMethod->getDocComment();
616+
if (!empty($docBlock)) {
617+
$description = $this->getDescriptionFromDocBlock($docBlock);
618+
}
619+
620+
return $description;
621+
}
622+
623+
581624
/**
582625
* Map the PHP type to the OpenAPI type. The currently available types for v3.1.1 are the following: “null”,
583626
* “boolean”, “object”, “array”, “number”, “string”, or “integer”.
@@ -1717,12 +1760,13 @@ public function buildSchemaObjectArrays(array $typesMap, string $default = '', s
17171760
*
17181761
* @return string[] The array of all the lines of the operation annotation object.
17191762
*/
1720-
public function compileOperationLines(string $path, string $opId, string $plugin, array $params, array $responses, bool $isPost = false): array
1763+
public function compileOperationLines(string $path, string $opId, string $plugin, array $params, array $responses, string $description, bool $isPost = false): array
17211764
{
17221765
$operationValuesMap = [
17231766
'path="' . $path . '"',
17241767
'operationId="' . $opId . '"',
17251768
'tags={"' . $plugin . '"}',
1769+
'description="' . $description . '"',
17261770
];
17271771
foreach ($params['refs'] ?? [] as $ref) {
17281772
$operationValuesMap[] = '@OA\Parameter(ref="' . $ref . '")';

Specs/SpecGenerator.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,18 @@ public function generateSpec(array $pluginNames, string $format = 'json', string
7373
Manager::getInstance()->checkIsPluginActivated($pluginName);
7474

7575
$pluginDir = Manager::getInstance()::getPluginDirectory($pluginName);
76-
$pluginAnnotationsSource = $pluginDir . '/API.php';
76+
// $pluginAnnotationsSource = $pluginDir . '/API.php';
77+
$pluginAnnotationsSource = $currentPluginDir . '/tmp/annotations/' . $pluginName . 'GeneratedAnnotations.php';
7778
try {
7879
$openapi = (new Generator(StaticContainer::get(NullLogger::class)))->generate([
7980
$pluginAnnotationsSource,
8081
]);
8182
} catch (\Throwable $e) {
8283
throw new \Exception('There was an error testing the API annotations for plugin ' . $pluginName, 0, $e);
8384
}
84-
if (trim($openapi->toYaml()) === 'openapi: ' . OpenApi::DEFAULT_VERSION) {
85-
throw new \Exception("The $pluginName plugin's API class does not appear to be annotated yet.");
86-
}
85+
// if (trim($openapi->toYaml()) === 'openapi: ' . OpenApi::DEFAULT_VERSION) {
86+
// throw new \Exception("The $pluginName plugin's API class does not appear to be annotated yet.");
87+
// }
8788
$pluginDirs[$pluginName] = $pluginAnnotationsSource;
8889
}
8990

0 commit comments

Comments
 (0)