-
Notifications
You must be signed in to change notification settings - Fork 1
/
SwaggerBakeListener.php
91 lines (79 loc) · 2.46 KB
/
SwaggerBakeListener.php
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace App\Event;
use Cake\Event\Event;
use Cake\Event\EventManager;
use SwaggerBake\Lib\OpenApi\Operation;
use SwaggerBake\Lib\OpenApi\Schema;
use SwaggerBake\Lib\Swagger;
class SwaggerBakeListener
{
/**
* Listens for SwaggerBake events
*
* @return void
*/
public function listen(): void
{
EventManager::instance()
->on('SwaggerBake.Schema.created', function (Event $event) {
return $this->schemaCreated($event);
});
EventManager::instance()
->on('SwaggerBake.Operation.created', function (Event $event) {
return $this->operationCreated($event);
});
EventManager::instance()
->on('SwaggerBake.beforeRender', function (Event $event) {
return $this->beforeRender($event);
});
}
/**
* @param Event $event
* @return \SwaggerBake\Lib\OpenApi\Schema
*/
private function schemaCreated(Event $event): Schema
{
/** @var Schema $schema */
$schema = $event->getSubject();
if ($schema->getName() === 'Actor') {
$schema->setDescription(
sprintf(
'This description was modified by a SwaggerBake.Schema.created event listener. See %s.',
self::class . '::schemaCreated'
)
);
}
return $schema;
}
/**
* @param Event $event
* @return \SwaggerBake\Lib\OpenApi\Operation
*/
private function operationCreated(Event $event): Operation
{
/** @var Operation $operation */
$operation = $event->getSubject();
if ($operation->getOperationId() === 'actors:add:post') {
$operation->setDescription(
sprintf(
'This description was modified by a SwaggerBake.Operation.created event listener. See %s.',
self::class . '::operationCreated'
)
);
}
return $operation;
}
/**
* @param Event $event
* @return \SwaggerBake\Lib\Swagger
*/
private function beforeRender(Event $event): Swagger
{
/** @var Swagger $swagger */
$swagger = $event->getSubject();
$openApi = $swagger->getArray();
$openApi['info']['description'].= ' This is sentence was added via the SwaggerBake.beforeRender event';
$swagger->setArray($openApi);
return $swagger;
}
}