| 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 |
* Look at all (direct) interfaces for a schema and: |
| 15 |
* - merge interfaces annotations/methods into the schema if the interface does not have a schema itself |
| 16 |
* - inherit from the interface if it has a schema (allOf). |
| 17 |
*/ |
| 18 |
class ExpandInterfaces |
| 19 |
{ |
| 20 |
use Concerns\MergePropertiesTrait; |
| 21 |
|
| 22 |
public function __invoke(Analysis $analysis): void |
| 23 |
{ |
| 24 |
$schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true); |
| 25 |
|
| 26 |
foreach ($schemas as $schema) { |
| 27 |
if ($schema->_context->is('class')) { |
| 28 |
$className = $schema->_context->fullyQualifiedName($schema->_context->class); |
| 29 |
$interfaces = $analysis->getInterfacesOfClass($className, true); |
| 30 |
|
| 31 |
if (class_exists($className) && ($parent = get_parent_class($className)) && ($inherited = array_keys(class_implements($parent)))) { |
| 32 |
// strip interfaces we inherit from ancestor |
| 33 |
foreach (array_keys($interfaces) as $interface) { |
| 34 |
if (in_array(ltrim((string) $interface, '\\'), $inherited)) { |
| 35 |
unset($interfaces[$interface]); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
$existing = []; |
| 41 |
foreach ($interfaces as $interface) { |
| 42 |
$interfaceName = $interface['context']->fullyQualifiedName($interface['interface']); |
| 43 |
$interfaceSchema = $analysis->getAnnotationForSource($interfaceName); |
| 44 |
if ($interfaceSchema) { |
| 45 |
$refPath = Generator::isDefault($interfaceSchema->schema) ? $interface['interface'] : $interfaceSchema->schema; |
| 46 |
$this->inheritFrom($analysis, $schema, $interfaceSchema, $refPath, $interface['context']); |
| 47 |
} else { |
| 48 |
$this->mergeMethods($schema, $interface, $existing); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
} |