Skip to content

Commit 0abba24

Browse files
committed
Stub
1 parent 1657e95 commit 0abba24

File tree

6 files changed

+60
-10
lines changed

6 files changed

+60
-10
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ then the value for `comments` can be
610610

611611
### `x-route`
612612

613-
To customize route (controller ID/action ID) for a path, use custom key `x-route` with value `<controller ID>/<action ID>`. It can be used for non-crud paths. It must be used under HTTP method key but not
613+
To customize [route](https://www.yiiframework.com/doc/guide/2.0/en/runtime-routing) (controller ID/action ID) for a path, use custom key `x-route` with value `<controller ID>/<action ID>`. It can be used for non-crud paths. It must be used under HTTP method key but not
614614
directly under the `paths` key of OpenAPI spec. Example:
615615

616616
```yaml
@@ -681,7 +681,18 @@ Generated URL rules config for above is (in `urls.rest.php` or pertinent file):
681681
'POST a1/b1' => 'abc/xyz',
682682
'a1/b1' => 'abc/options',
683683
```
684-
`x-route` does not support [Yii Modules](https://www.yiiframework.com/doc/guide/2.0/en/structure-modules).
684+
`x-route` does not support [Yii Modules](https://www.yiiframework.com/doc/guide/2.0/en/structure-modules). // TODO support it
685+
686+
`x-route` must not start with slash `/`. For example `x-route: /user/posts` is incorrect. It must start with [module ID](https://www.yiiframework.com/doc/guide/2.0/en/structure-modules) or [controller ID](https://www.yiiframework.com/doc/guide/2.0/en/structure-controllers#controller-ids)
687+
688+
#### Route, path and namespace TODO
689+
690+
Route, path and namespace for controller/action will be resolved in following manner (from highest priority to lowest):
691+
692+
- `x-route`
693+
- `urlPrefixes`
694+
- `controllerNamespace` of this lib
695+
- `controllerNamespace` of Yii app
685696

686697
### `x-description-is-comment`
687698

src/lib/generators/RestActionGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ protected function prepareAction(
154154
: null,
155155
'responseWrapper' => $responseWrapper,
156156
'prefix' => $routeData->getPrefix(),
157-
'prefixSettings' => $routeData->getPrefixSettings()
157+
'prefixSettings' => $routeData->getPrefixSettings(),
158+
'xRoute' => $customRoute # TODO
158159
],
159160
]);
160161
}

src/lib/items/OptionsRoutesTrait.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,41 @@ public static function finalOptionsRoute(string $prefix, string $controllerId):
5959
{
6060
return trim($prefix, '/') . '/' . $controllerId . '/options';
6161
}
62+
63+
public function getRouteInfo(): array
64+
{
65+
/** @var ?array $modules */
66+
$modules = $controllerId = $path = $namespace = null;
67+
68+
if ($this->xRoute) {
69+
$routeParts = explode('/', $this->xRoute);
70+
$controllerId = $routeParts[count($routeParts)-2]; # last second part is controller ID
71+
// $actionId = $routeParts[count($routeParts)-1];
72+
unset($routeParts[count($routeParts)-1], $routeParts[count($routeParts)-2]);
73+
$modules = $routeParts;
74+
}
75+
76+
return [
77+
'modules' => $modules,
78+
'controller_id' => $controllerId,
79+
// 'action_id' => $actionId,
80+
'path' => $path,
81+
'namespace' => $namespace,
82+
];
83+
}
84+
85+
// public function getRoute(): string
86+
// {
87+
//
88+
// }
89+
//
90+
// public function getPath(): string
91+
// {
92+
//
93+
// }
94+
//
95+
// public function getNamespace(): string
96+
// {
97+
//
98+
// }
6299
}

tests/specs/issue_fix/controller_namespace_issue_for_modules_in_urlprefixes/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
],
1010
'generateControllers' => true,
1111
'generateMigrations' => false,
12-
'useJsonApi' => true,
12+
'useJsonApi' => true, # TODO run same tests with `false`
1313
'urlPrefixes' => [
1414
'animals' => '',
1515
'/info' => ['module' =>'petinfo','namespace' => '\app\modules\petinfo\controllers'],

tests/specs/issue_fix/controller_namespace_issue_for_modules_in_urlprefixes/index.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ paths:
113113
description: list of details
114114
/fgh/pet-detail2s:
115115
get:
116+
# x-route: f/l789898
116117
description: list all pet details
117118
responses:
118119
'200':

tests/unit/IssueFixTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -549,12 +549,12 @@ public function testControllerNamespaceIssueForModulesInUrlPrefixes()
549549
{
550550
$testFile = Yii::getAlias("@specs/issue_fix/controller_namespace_issue_for_modules_in_urlprefixes/index.php");
551551
$this->runGenerator($testFile);
552-
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
553-
'recursive' => true,
554-
]);
555-
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/controller_namespace_issue_for_modules_in_urlprefixes/mysql"), [
556-
'recursive' => true,
557-
]);
552+
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
553+
// 'recursive' => true,
554+
// ]);
555+
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/controller_namespace_issue_for_modules_in_urlprefixes/mysql"), [
556+
// 'recursive' => true,
557+
// ]);
558558
// $this->checkFiles($actualFiles, $expectedFiles);
559559
}
560560

0 commit comments

Comments
 (0)