forked from cebe/yii2-openapi
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathActionHelperTrait.php
More file actions
97 lines (85 loc) · 3.69 KB
/
Copy pathActionHelperTrait.php
File metadata and controls
97 lines (85 loc) · 3.69 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
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
92
93
94
95
96
97
<?php
/**
* @copyright Copyright (c) 2018 Carsten Brandt <mail@cebe.cc> and contributors
* @license https://github.com/cebe/yii2-openapi/blob/master/LICENSE
*/
namespace cebe\yii2openapi\lib\items;
trait ActionHelperTrait
{
public ?string $xRoute = null;
# list of module this action is part of. 'key' is module ID and 'value' is path where Module.php file must be generated
public array $modulesList = [];
/**
* @see $isDuplicate
* https://github.com/cebe/yii2-openapi/issues/84
* see `x-route` in README.md
* Used for generating only one action for paths like: `GET /calendar/domains` and `GET /calendar/domains/{id}` given that they have same `x-route`.
* If duplicates routes have same params then `false`, else action is generated with no (0) params `true`
*/
public bool $zeroParams = false;
/**
* https://github.com/cebe/yii2-openapi/issues/84
* Generate only one action for paths like: `GET /calendar/domains` and `GET /calendar/domains/{id}` given that they have same `x-route`.
* @see $zeroParams
* see `x-route` in README.md
*/
public bool $isDuplicate = false;
public function getOptionsRoute():string
{
$r = $this->getRoute();
$r = explode('/', $r);
array_pop($r);
return implode('/', $r) . '/options';
}
/**
* @param string $separator
* @param string $entity path or namespace
* @return void
*/
public static function computeModule(string $separator, string $entity): ?string
{
$parts = explode($separator . 'modules' . $separator, $entity); # /app/modules/forum/controllers => /forum/controllers
if (empty($parts[1])) {
return null;
}
if (str_contains($parts[1], 'controller')) {
$result = explode($separator . 'controller', $parts[1]); // compute everything in between "modules" and "controllers" e.g. api/v1
$result = array_map(function ($val) {
return str_replace('\\', '/', $val);
}, $result);
} else {
$result = explode($separator, $parts[1]); # forum/controllers => forum
}
if (empty($result[0])) {
return null;
}
return $result[0];
}
public static function finalOptionsRoute(string $prefix, string $controllerId): string
{
return trim($prefix, '/') . '/' . $controllerId . '/options';
}
public function getRoute(): string
{
if ($this->xRoute) {
return $this->xRoute;
}
if (!empty($this->prefixSettings)) {
if (isset($this->prefixSettings['module'])) {
$prefix = $this->prefixSettings['module'];
return trim($prefix, '/') . '/' . $this->controllerId . ($this->id ? '/' . $this->id : '');
} elseif (isset($this->prefixSettings['namespace']) && str_contains($this->prefixSettings['namespace'], '\modules\\')) { # if `module` not present then check in namespace and then in path
$prefix = static::computeModule('\\', $this->prefixSettings['namespace']);
if ($prefix) {
return trim($prefix, '/') . '/' . $this->controllerId . ($this->id ? '/' . $this->id : '');
}
} elseif (isset($this->prefixSettings['path']) && str_contains($this->prefixSettings['path'], '/modules/')) {
$prefix = static::computeModule('/', $this->prefixSettings['path']);
if ($prefix) {
return trim($prefix, '/') . '/' . $this->controllerId . ($this->id ? '/' . $this->id : '');
}
}
}
return $this->controllerId . ($this->id ? '/' . $this->id : '');
}
}