forked from zircote/swagger-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpandInterfaces.php
More file actions
55 lines (47 loc) · 2.09 KB
/
ExpandInterfaces.php
File metadata and controls
55 lines (47 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php declare(strict_types=1);
/**
* @license Apache 2.0
*/
namespace OpenApi\Processors;
use OpenApi\Analysis;
use OpenApi\Annotations as OA;
use OpenApi\Generator;
/**
* Look at all (direct) interfaces for a schema and:
* - merge interfaces annotations/methods into the schema if the interface does not have a schema itself
* - inherit from the interface if it has a schema (allOf).
*/
class ExpandInterfaces
{
use Concerns\MergePropertiesTrait;
public function __invoke(Analysis $analysis)
{
/** @var OA\Schema[] $schemas */
$schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true);
foreach ($schemas as $schema) {
if ($schema->_context->is('class')) {
$className = $schema->_context->fullyQualifiedName($schema->_context->class);
$interfaces = $analysis->getInterfacesOfClass($className, true);
if (class_exists($className) && ($parent = get_parent_class($className)) && ($inherited = array_keys(class_implements($parent)))) {
// strip interfaces we inherit from ancestor
foreach (array_keys($interfaces) as $interface) {
if (in_array(ltrim($interface, '\\'), $inherited)) {
unset($interfaces[$interface]);
}
}
}
$existing = [];
foreach ($interfaces as $interface) {
$interfaceName = $interface['context']->fullyQualifiedName($interface['interface']);
$interfaceSchema = $analysis->getSchemaForSource($interfaceName);
if ($interfaceSchema) {
$refPath = !Generator::isDefault($interfaceSchema->schema) ? $interfaceSchema->schema : $interface['interface'];
$this->inheritFrom($analysis, $schema, $interfaceSchema, $refPath, $interface['context']);
} else {
$this->mergeMethods($schema, $interface, $existing);
}
}
}
}
}
}