Skip to content

Commit 232627a

Browse files
committed
Implement nested modules in x-route - WIP
1 parent 7649d0f commit 232627a

File tree

8 files changed

+121
-11
lines changed

8 files changed

+121
-11
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -689,10 +689,15 @@ Generated URL rules config for above is (in `urls.rest.php` or pertinent file):
689689

690690
Route, path and namespace for controller/action will be resolved in following manner (from highest priority to lowest):
691691

692-
- `x-route`
693-
- `urlPrefixes`
694-
- `controllerNamespace` of this lib
695-
- `controllerNamespace` of Yii app
692+
- [`x-route`](#x-route)
693+
- [
694+
`urlPrefixes`](https://github.com/php-openapi/yii2-openapi/blob/649743cf0a78743f550edcbd4e93fdffc55c76fd/src/lib/Config.php#L51)
695+
- [
696+
`controllerNamespace`](https://github.com/php-openapi/yii2-openapi/blob/649743cf0a78743f550edcbd4e93fdffc55c76fd/src/lib/Config.php#L77)
697+
of this lib
698+
- [
699+
`controllerNamespace`](https://github.com/yiisoft/yii2/blob/16f50626e1aa81200f109c1a455a5c9b18acfdda/framework/base/Application.php#L93)
700+
of Yii app
696701

697702
### `x-description-is-comment`
698703

TODO.taskpaper

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
TODO.taskpaper
22

3-
fix failing tests
3+
fix failing tests @done (25-03-21 16:20)
44
☐ resolve all TODOs
55
☐ resolve all sub tasks of issue - Support for Yii Modules in x-route and pertinent places #14
6+
☐ Implement code generation for nested module in `x-route` and other pertinent place
67
☐ delete this file

src/lib/generators/RestActionGenerator.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ protected function resolvePath(string $path, PathItem $pathItem):array
7171
{
7272
$actions = [];
7373

74-
$routeData = Yii::createObject(RouteData::class, [$pathItem, $path, $this->config->urlPrefixes]);
7574
foreach ($pathItem->getOperations() as $method => $operation) {
75+
$routeData = Yii::createObject(RouteData::class, [$path, $pathItem, $method, $operation, $this->config->urlPrefixes]);
7676
$customRoute = null;
7777
if (isset($operation->{CustomSpecAttr::ROUTE})) { # https://github.com/cebe/yii2-openapi/issues/144
7878
$customRoute = $operation->{CustomSpecAttr::ROUTE};
@@ -130,14 +130,16 @@ protected function prepareAction(
130130
? Inflector::camel2id($this->config->controllerModelMap[$modelClass])
131131
: Inflector::camel2id($modelClass);
132132
} elseif (!empty($customRoute)) {
133-
$controllerId = explode('/', $customRoute)[0];
133+
$parts = explode('/', $customRoute);
134+
$controllerId = $parts[count($parts) - 2];
134135
} else {
135136
$controllerId = $routeData->controller;
136137
}
137138
$action = Inflector::camel2id($routeData->action);
138139
if (!empty($customRoute)) {
139140
$actionType = '';
140-
$action = explode('/', $customRoute)[1];
141+
$parts = explode('/', $customRoute);
142+
$action = $parts[count($parts) - 1];
141143
}
142144
return Yii::createObject(RestAction::class, [
143145
[
@@ -154,7 +156,8 @@ protected function prepareAction(
154156
: null,
155157
'responseWrapper' => $responseWrapper,
156158
'prefix' => $routeData->getPrefix(),
157-
'prefixSettings' => $routeData->getPrefixSettings()
159+
'prefixSettings' => $routeData->getPrefixSettings(),
160+
'xRoute' => $customRoute
158161
],
159162
]);
160163
}

src/lib/items/RestAction.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ final class RestAction extends BaseObject
7070
*/
7171
public $responseWrapper;
7272

73+
/**
74+
* @var ?string
75+
*/
76+
public $xRoute;
77+
7378
/**
7479
* @var bool
7580
* @see $isDuplicate
@@ -91,10 +96,15 @@ final class RestAction extends BaseObject
9196

9297
public function getRoute():string
9398
{
99+
if ($this->xRoute) {
100+
return $this->xRoute;
101+
}
102+
94103
if ($this->prefix && !empty($this->prefixSettings)) {
95104
$prefix = $this->prefixSettings['module'] ?? $this->prefix;
96105
return trim($prefix, '/') . '/' . $this->controllerId . '/' . $this->id;
97106
}
107+
98108
return $this->controllerId . '/' . $this->id;
99109
}
100110

src/lib/items/RouteData.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
namespace cebe\yii2openapi\lib\items;
99

10+
use cebe\openapi\spec\Operation;
1011
use cebe\openapi\spec\PathItem;
12+
use cebe\yii2openapi\lib\CustomSpecAttr;
1113
use yii\base\BaseObject;
1214
use yii\base\InvalidCallException;
1315
use yii\helpers\ArrayHelper;
@@ -164,12 +166,49 @@ final class RouteData extends BaseObject
164166
*/
165167
private $urlPrefixes;
166168

167-
public function __construct(PathItem $pathItem, string $path, array $urlPrefixes = [], $config = [])
168-
{
169+
/**
170+
* @var string
171+
*/
172+
private $method;
173+
174+
/**
175+
* @var Operation
176+
*/
177+
private $operation;
178+
179+
public function __construct(
180+
string $path,
181+
PathItem $pathItem,
182+
string $method,
183+
Operation $operation,
184+
array $urlPrefixes = [],
185+
$config = []
186+
) {
169187
$this->path = $this->unprefixedPath = $path;
170188
$this->parts = explode('/', trim($path, '/'));
171189
$this->pathItem = $pathItem;
190+
$this->method = $method;
191+
$this->operation = $operation;
172192
$this->urlPrefixes = $urlPrefixes;
193+
194+
if (isset($operation->{CustomSpecAttr::ROUTE})) { # https://github.com/cebe/yii2-openapi/issues/144
195+
$customRoute = $operation->{CustomSpecAttr::ROUTE};
196+
$parts = explode('/', $customRoute);
197+
array_pop($parts);
198+
array_pop($parts);
199+
$this->prefix = implode('/', $parts); # add everything except controller ID and action ID
200+
201+
$modulesPath = [];
202+
foreach ($parts as $module) {
203+
$modulesPath[] = 'modules/' . $module;
204+
}
205+
206+
$this->prefixSettings = [
207+
'namespace' => 'app\\' . implode('\\', $parts) . '\\controllers',
208+
'path' => 'app/' . implode('/', $modulesPath) . '/controllers'
209+
];
210+
}
211+
173212
parent::__construct($config);
174213
}
175214

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/14_nested_module_in_x_route/index.yml',
5+
'generateUrls' => true,
6+
'generateModels' => true,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => true,
11+
'generateMigrations' => false,
12+
'generateModelFaker' => false,
13+
];
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Issue14Test testNestedModuleInXRoute \#14
5+
6+
paths:
7+
/:
8+
get:
9+
x-route: fruit/mango/alphonso/view
10+
summary: List
11+
operationId: list
12+
responses:
13+
'200':
14+
description: The information

tests/unit/issues/Issue14Test.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace tests\unit\issues;
4+
5+
use tests\DbTestCase;
6+
use Yii;
7+
use yii\base\InvalidArgumentException;
8+
use yii\helpers\FileHelper;
9+
10+
# https://github.com/php-openapi/yii2-openapi/issues/14
11+
class Issue14Test extends DbTestCase
12+
{
13+
public function testNestedModuleInXRoute()
14+
{
15+
$testFile = Yii::getAlias("@specs/issue_fix/14_nested_module_in_x_route/index.php");
16+
$this->runGenerator($testFile);
17+
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
18+
// 'recursive' => true,
19+
// ]);
20+
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/14_nested_module_in_x_route/mysql"), [
21+
// 'recursive' => true,
22+
// ]);
23+
// $this->checkFiles($actualFiles, $expectedFiles);
24+
}
25+
}

0 commit comments

Comments
 (0)