WIP parse phpdoc annotations in jms models

This commit is contained in:
David Buchmann 2017-12-18 17:32:00 +01:00
parent f03e33f551
commit fe3629cdeb
3 changed files with 64 additions and 1 deletions

View File

@ -103,6 +103,8 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
// read property options from Swagger Property annotation if it exists
if (null !== $item->reflection) {
$phpdocReader = new PhpdocAnnotationReader();
$phpdocReader->updateWithPhpdoc($item->reflection, $realProp);
$this->swaggerPropertyAnnotationReader->updateWithSwaggerPropertyAnnotation($item->reflection, $realProp);
}
}

View File

@ -0,0 +1,61 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\ModelDescriber;
use EXSyst\Component\Swagger\Schema;
use EXSyst\Component\Swagger\Items;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use phpDocumentor\Reflection\DocBlockFactory;
use phpDocumentor\Reflection\DocBlockFactoryInterface;
/**
* @internal
*/
class PhpdocAnnotationReader
{
private $docBlockFactory;
public function __construct(DocBlockFactoryInterface $docBlockFactory = null)
{
if (null === $docBlockFactory) {
$docBlockFactory = DocBlockFactory::createInstance();
}
$this->docBlockFactory = $docBlockFactory;
}
/**
* @param \ReflectionProperty $reflectionProperty
* @param Items|Schema $property
*/
public function updateWithPhpdoc(\ReflectionProperty $reflectionProperty, $property)
{
try {
$docBlock = $this->docBlockFactory->create($reflectionProperty);
if (!$title = $docBlock->getSummary()) {
/** @var Var_ $var */
foreach ($docBlock->getTagsByName('var') as $var) {
if (null === $description = $var->getDescription()) continue;
$title = $description->render();
if ($title) break;
}
}
if ($property->getTitle() === null && $title) {
$property->setTitle($title);
}
if ($property->getDescription() === null && $docBlock->getDescription()) {
$property->setDescription($docBlock->getDescription()->render());
}
} catch (\Exception $e) {
// ignore
}
}
}

View File

@ -151,7 +151,7 @@ serialization groups when using the Symfony serializer.
### If you're using the JMS Serializer
The metadata of the JMS serializer are used by default to describe your
models. Note that PHP doc blocks aren't supported in this case.
models.
In case you prefer using the [Symfony PropertyInfo component](https://symfony.com/doc/current/components/property_info.html) (you
won't be able to use JMS serialization groups), you can disable JMS serializer