| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
/** |
| 4 |
* @license Apache 2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace OpenApi\Processors; |
| 8 |
|
| 9 |
use OpenApi\Analysis; |
| 10 |
use OpenApi\Annotations as OA; |
| 11 |
use OpenApi\Generator; |
| 12 |
|
| 13 |
/** |
| 14 |
* Tracks the use of all <code>Components</code> and removed unused schemas. |
| 15 |
*/ |
| 16 |
class CleanUnusedComponents |
| 17 |
{ |
| 18 |
use Concerns\AnnotationTrait; |
| 19 |
|
| 20 |
protected bool $enabled; |
| 21 |
|
| 22 |
public function __construct(bool $enabled = false) |
| 23 |
{ |
| 24 |
$this->enabled = $enabled; |
| 25 |
} |
| 26 |
|
| 27 |
public function isEnabled(): bool |
| 28 |
{ |
| 29 |
return $this->enabled; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Enables/disables the <code>CleanUnusedComponents</code> processor. |
| 34 |
*/ |
| 35 |
public function setEnabled(bool $enabled): CleanUnusedComponents |
| 36 |
{ |
| 37 |
$this->enabled = $enabled; |
| 38 |
|
| 39 |
return $this; |
| 40 |
} |
| 41 |
|
| 42 |
public function __invoke(Analysis $analysis): void |
| 43 |
{ |
| 44 |
if (!$this->enabled || Generator::isDefault($analysis->openapi->components)) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
// allow multiple runs to catch nested dependencies |
| 49 |
for ($ii = 0; $ii < 10; ++$ii) { |
| 50 |
if (!$this->cleanup($analysis)) { |
| 51 |
break; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
protected function cleanup(Analysis $analysis): bool |
| 57 |
{ |
| 58 |
$usedRefs = []; |
| 59 |
foreach ($analysis->annotations as $annotation) { |
| 60 |
if (property_exists($annotation, 'ref') && !Generator::isDefault($annotation->ref) && $annotation->ref !== null) { |
| 61 |
$usedRefs[$annotation->ref] = true; |
| 62 |
} |
| 63 |
|
| 64 |
foreach (['allOf', 'anyOf', 'oneOf'] as $sub) { |
| 65 |
if (property_exists($annotation, $sub) && !Generator::isDefault($annotation->{$sub})) { |
| 66 |
foreach ($annotation->{$sub} as $subElem) { |
| 67 |
if (is_object($subElem) && property_exists($subElem, 'ref') && !Generator::isDefault($subElem->ref) && $subElem->ref !== null) { |
| 68 |
$usedRefs[$subElem->ref] = true; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
if ($annotation instanceof OA\OpenApi || $annotation instanceof OA\Operation) { |
| 75 |
if (!Generator::isDefault($annotation->security)) { |
| 76 |
foreach ($annotation->security as $security) { |
| 77 |
foreach (array_keys($security) as $securityName) { |
| 78 |
$usedRefs[OA\Components::COMPONENTS_PREFIX . 'securitySchemes/' . $securityName] = true; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
$unusedComponents = []; |
| 86 |
foreach (OA\Components::$_nested as $nested) { |
| 87 |
if (2 == count($nested)) { |
| 88 |
[$componentType, $nameProperty] = $nested; |
| 89 |
if (!Generator::isDefault($analysis->openapi->components->{$componentType})) { |
| 90 |
foreach ($analysis->openapi->components->{$componentType} as $ii => $component) { |
| 91 |
$ref = OA\Components::ref($component); |
| 92 |
if (!isset($usedRefs[$ref])) { |
| 93 |
$unusedComponents[] = [$componentType, $ii, $component]; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
foreach ($unusedComponents as [$componentType, $ii, $component]) { |
| 101 |
$this->removeAnnotationRecursive($analysis, $component); |
| 102 |
unset($analysis->openapi->components->{$componentType}[$ii]); |
| 103 |
|
| 104 |
if (!$analysis->openapi->components->{$componentType}) { |
| 105 |
$analysis->openapi->components->{$componentType} = Generator::UNDEFINED; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return [] !== $unusedComponents; |
| 110 |
} |
| 111 |
} |