NelmioApiDocBundle/RouteDescriber/PhpDocDescriber.php

68 lines
2.1 KiB
PHP
Raw Normal View History

2016-07-13 23:05:14 +02:00
<?php
/*
2016-12-29 12:09:26 +01:00
* This file is part of the NelmioApiDocBundle package.
2016-07-13 23:05:14 +02:00
*
2016-12-29 12:09:26 +01:00
* (c) Nelmio
2016-07-13 23:05:14 +02:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2016-12-29 12:09:26 +01:00
namespace Nelmio\ApiDocBundle\RouteDescriber;
2016-07-13 23:05:14 +02:00
use OpenApi\Annotations as OA;
2016-07-13 23:05:14 +02:00
use phpDocumentor\Reflection\DocBlockFactory;
use phpDocumentor\Reflection\DocBlockFactoryInterface;
use Symfony\Component\Routing\Route;
2016-11-30 15:04:53 +01:00
final class PhpDocDescriber implements RouteDescriberInterface
2016-07-13 23:05:14 +02:00
{
2016-07-15 00:40:30 +02:00
use RouteDescriberTrait;
2016-07-13 23:05:14 +02:00
private $docBlockFactory;
public function __construct(DocBlockFactoryInterface $docBlockFactory = null)
{
if (null === $docBlockFactory) {
$docBlockFactory = DocBlockFactory::createInstance();
}
$this->docBlockFactory = $docBlockFactory;
}
public function describe(OA\OpenApi $api, Route $route, \ReflectionMethod $reflectionMethod)
2016-07-13 23:05:14 +02:00
{
$classDocBlock = null;
$docBlock = null;
try {
$classDocBlock = $this->docBlockFactory->create($reflectionMethod->getDeclaringClass());
} catch (\Exception $e) {
}
2017-12-22 17:42:18 +00:00
2016-07-13 23:05:14 +02:00
try {
$docBlock = $this->docBlockFactory->create($reflectionMethod);
} catch (\Exception $e) {
}
foreach ($this->getOperations($api, $route) as $operation) {
if (null !== $docBlock) {
if (OA\UNDEFINED === $operation->summary && '' !== $docBlock->getSummary()) {
$operation->summary = $docBlock->getSummary();
2016-08-04 22:27:10 +02:00
}
if (OA\UNDEFINED === $operation->description && '' !== (string) $docBlock->getDescription()) {
$operation->description = (string) $docBlock->getDescription();
2016-08-04 22:27:10 +02:00
}
2016-08-01 19:58:57 +02:00
if ($docBlock->hasTag('deprecated')) {
$operation->deprecated = true;
2016-08-01 19:58:57 +02:00
}
2016-07-13 23:05:14 +02:00
}
if (null !== $classDocBlock) {
2016-08-01 19:58:57 +02:00
if ($classDocBlock->hasTag('deprecated')) {
$operation->deprecated = true;
2016-08-01 19:58:57 +02:00
}
2016-07-13 23:05:14 +02:00
}
}
}
}