Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Contract/Service/DocBlockParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface DocBlockParserInterface
/**
* @return array<string, PhpDocChildNode>
*/
public function parseDocBlock(string $docBlock): array;
public function parseDocBlock(string $docBlock, ?string $parameterName = null): array;

/**
* @return string[]
Expand Down
16 changes: 11 additions & 5 deletions src/Model/Doc/Request/RequestDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,28 @@

class RequestDoc
{
private ?ComponentSchemaDoc $componentSchemaDoc = null;
/** @var ComponentSchemaDoc[] */
private array $componentSchemaDocs = [];

/** @var ParameterDoc[] */
private array $parameters = [];

/** @var mixed[] */
private array $requestBody = [];

public function getComponentSchemaDoc(): ?ComponentSchemaDoc
/**
* @return ComponentSchemaDoc[]
*/
public function getComponentSchemaDocs(): array
{
return $this->componentSchemaDoc;
return $this->componentSchemaDocs;
}

public function setComponentSchemaDoc(?ComponentSchemaDoc $componentSchemaDoc): self
public function addComponentSchemaDoc(?ComponentSchemaDoc $componentSchemaDoc): self
{
$this->componentSchemaDoc = $componentSchemaDoc;
if ($componentSchemaDoc !== null) {
$this->componentSchemaDocs[$componentSchemaDoc->getName()] = $componentSchemaDoc;
}

return $this;
}
Expand Down
11 changes: 9 additions & 2 deletions src/Service/ControllerMethodParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
use Symfony\Component\Routing\Attribute\Route as RouteAttribute;
use Symfony\Component\Routing\RouterInterface;
use Valantic\PimcoreApiDocumentationBundle\Contract\Model\Component\Property\ComponentSchemaPropertyInterface;
use Valantic\PimcoreApiDocumentationBundle\Contract\Service\ControllerMethodParserInterface;
use Valantic\PimcoreApiDocumentationBundle\Contract\Service\DataTypeParserInterface;
use Valantic\PimcoreApiDocumentationBundle\Contract\Service\SchemaGeneratorInterface;
Expand Down Expand Up @@ -160,11 +161,11 @@ private function parseRequest(\ReflectionMethod $method): ?RequestDoc
if (!is_subclass_of($requestClass, ApiRequestInterface::class)) {
return null;
}

$requestDoc = new RequestDoc();

foreach ($requestParameter->getAttributes() as $attribute) {
if ($attribute->getName() === MapQueryString::class) {
// TODO: parse nested
$requestReflection = new \ReflectionClass($requestClass);

$requestProperties = $requestReflection->getProperties(\ReflectionProperty::IS_PUBLIC);
Expand All @@ -174,6 +175,12 @@ private function parseRequest(\ReflectionMethod $method): ?RequestDoc

$propertyDoc = $this->getDataTypeParser($property)->parse($property);

if ($propertyDoc instanceof ComponentSchemaPropertyInterface) {
foreach ($propertyDoc->getSchemas() as $schema) {
$requestDoc->addComponentSchemaDoc($schema);
}
}

$parameterDoc
->setName($property->getName())
->setIn(ParameterDoc::IN_QUERY)
Expand All @@ -187,7 +194,7 @@ private function parseRequest(\ReflectionMethod $method): ?RequestDoc
}

if (($attribute->getName() === MapRequestPayload::class) && is_subclass_of($requestClass, HasJsonPayload::class)) {
$requestDoc->setComponentSchemaDoc($this->schemaGenerator->generateForRequest($requestClass));
$requestDoc->addComponentSchemaDoc($this->schemaGenerator->generateForRequest($requestClass));
$requestDoc->setRequestBody([
'content' => [
'application/json' => [
Expand Down
5 changes: 3 additions & 2 deletions src/Service/DataTypeParser/ArrayParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function __construct(
public function parse(\ReflectionProperty $reflectionProperty): AbstractPropertyDoc
{
$typeHints = $this->determineTypeHints($reflectionProperty);

$arrayItems = [];

foreach ($typeHints as $typeHint) {
Expand Down Expand Up @@ -79,9 +78,11 @@ private function determineTypeHints(\ReflectionProperty $reflectionProperty): ar

$docBlocksTypeHints = [];
$docBlock = null;
$parameterName = null;

if ($reflectionProperty->getDocComment()) {
$docBlock = $reflectionProperty->getDocComment();
$parameterName = $propertyName;
} elseif (
$declaringClassReflection->hasMethod('__construct')
&& $declaringClassReflection->getMethod('__construct')->getDocComment() !== false
Expand All @@ -90,7 +91,7 @@ private function determineTypeHints(\ReflectionProperty $reflectionProperty): ar
}

if ($docBlock !== null) {
$docBlocksTypeHints = $this->docBlockParser->parseDocBlock($docBlock);
$docBlocksTypeHints = $this->docBlockParser->parseDocBlock($docBlock, $parameterName);
}

$typeHints = [];
Expand Down
12 changes: 7 additions & 5 deletions src/Service/DocBlockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,25 @@ public function __construct()
$this->phpDocParser = new PhpDocParser($typeParser, $constExprParser);
}

public function parseDocBlock(string $docBlock): array
public function parseDocBlock(string $docBlock, ?string $parameterName = null): array
{
$docBlockData = [];
$tokens = new TokenIterator($this->lexer->tokenize($docBlock));
$parsedDocBlock = $this->phpDocParser->parse($tokens);

foreach ($parsedDocBlock->children as $docBlockItem) {
$docBlockParamName = $parameterName;

if ($docBlockItem->value instanceof ParamTagValueNode) {
$parameterName = ltrim($docBlockItem->value->parameterName, '$');
$docBlockParamName = ltrim($docBlockItem->value->parameterName, '$');
}

if ($docBlockItem->value instanceof VarTagValueNode) {
$parameterName = ltrim($docBlockItem->value->variableName, '$');
$docBlockParamName = ltrim($docBlockItem->value->variableName, '$');
}

if (isset($parameterName)) {
$docBlockData[$parameterName] = $docBlockItem;
if (isset($docBlockParamName) && $docBlockParamName !== '') {
$docBlockData[$docBlockParamName] = $docBlockItem;
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/Service/DocsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ public function generate(string $docsPath): void
foreach ($controllerDoc->getMethodsDocs() as $methodDoc) {
$paths[$methodDoc->getRouteDoc()->getPath()][$methodDoc->getRouteDoc()->getMethod()] = $methodDoc;

if ($methodDoc->getRequestDoc()?->getComponentSchemaDoc() !== null) {
$requestSchema = $methodDoc->getRequestDoc()->getComponentSchemaDoc();
$schemas[$requestSchema->getName()] = $requestSchema;
}
$schemas = array_merge($schemas, $methodDoc->getRequestDoc()?->getComponentSchemaDocs() ?: []);

foreach ($methodDoc->getResponsesDoc() as $responseDoc) {
$schemas = array_merge($schemas, $responseDoc->getComponentSchemas());
Expand Down