Skip to content

Commit 17a40fa

Browse files
committed
Add Module.php file generator - WIP
1 parent 5d2d63c commit 17a40fa

File tree

8 files changed

+56
-19
lines changed

8 files changed

+56
-19
lines changed

README.md

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

611611
### `x-route`
612612

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
614-
directly under the `paths` key of OpenAPI spec. Example:
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 `[<module ID>/<child module ID>/...]<controller ID>/<action ID>`. It can be used for non-crud paths. It must be used under HTTP method key but not
614+
directly under the `paths` key of OpenAPI spec. Providing `<controller ID>` and `<action ID>` is required. Example:
615615

616616
```yaml
617617
paths:
@@ -681,7 +681,6 @@ 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). // TODO support it
685684

686685
`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)
687686

@@ -690,14 +689,9 @@ Generated URL rules config for above is (in `urls.rest.php` or pertinent file):
690689
Route, path and namespace for controller/action will be resolved in following manner (from highest priority to lowest):
691690

692691
- [`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
692+
- [`urlPrefixes`](https://github.com/php-openapi/yii2-openapi/blob/649743cf0a78743f550edcbd4e93fdffc55c76fd/src/lib/Config.php#L51)
693+
- [`controllerNamespace`](https://github.com/php-openapi/yii2-openapi/blob/649743cf0a78743f550edcbd4e93fdffc55c76fd/src/lib/Config.php#L77) of this lib
694+
- [`controllerNamespace`](https://github.com/yiisoft/yii2/blob/16f50626e1aa81200f109c1a455a5c9b18acfdda/framework/base/Application.php#L93) of Yii app
701695

702696
### `x-description-is-comment`
703697

src/lib/generators/ControllersGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function generate():CodeFiles
6262
* @var RestAction|FractalAction $action
6363
**/
6464
$action = $actions[0];
65-
if ($action->prefix && !empty($action->prefixSettings)) {
65+
if (!empty($action->prefixSettings)) {
6666
$controllerNamespace = trim($action->prefixSettings['namespace'], '\\');
6767
$controllerPath = $action->prefixSettings['path']
6868
?? $this->config->getPathFromNamespace($controllerNamespace);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (c) 2018 Carsten Brandt <mail@cebe.cc> and contributors
5+
* @license https://github.com/cebe/yii2-openapi/blob/master/LICENSE
6+
*/
7+
8+
namespace cebe\yii2openapi\lib\generators;
9+
10+
use cebe\yii2openapi\lib\CodeFiles;
11+
use cebe\yii2openapi\lib\Config;
12+
use cebe\yii2openapi\lib\items\FractalAction;
13+
use cebe\yii2openapi\lib\items\RestAction;
14+
use Laminas\Code\Generator\AbstractMemberGenerator;
15+
use Laminas\Code\Generator\ClassGenerator;
16+
use Laminas\Code\Generator\FileGenerator;
17+
use Laminas\Code\Generator\ParameterGenerator;
18+
use Laminas\Code\Generator\ValueGenerator;
19+
use Yii;
20+
use yii\gii\CodeFile;
21+
use yii\helpers\Inflector;
22+
23+
class ModuleFileGenerator
24+
{
25+
public function __construct()
26+
{
27+
}
28+
}

src/lib/items/FractalAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private function templateFactory():FractalActionTemplates
9797

9898
public function getRoute():string
9999
{
100-
if ($this->prefix && !empty($this->prefixSettings)) {
100+
if (!empty($this->prefixSettings)) {
101101
$prefix = $this->prefixSettings['module'] ?? $this->prefix;
102102
return trim($prefix, '/').'/'.$this->controllerId.'/'.$this->id;
103103
}

src/lib/items/OptionsRoutesTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ trait OptionsRoutesTrait
1111
{
1212
public function getOptionsRoute():string
1313
{
14-
if ($this->prefix && !empty($this->prefixSettings)) {
14+
if (!empty($this->prefixSettings)) {
1515
if (isset($this->prefixSettings['module'])) {
1616
$prefix = $this->prefixSettings['module'];
1717
return static::finalOptionsRoute($prefix, $this->controllerId);

src/lib/items/RestAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function getRoute():string
100100
return $this->xRoute;
101101
}
102102

103-
if ($this->prefix && !empty($this->prefixSettings)) {
103+
if (!empty($this->prefixSettings)) {
104104
$prefix = $this->prefixSettings['module'] ?? $this->prefix;
105105
return trim($prefix, '/') . '/' . $this->controllerId . '/' . $this->id;
106106
}

src/lib/items/RouteData.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ final class RouteData extends BaseObject
176176
*/
177177
private $operation;
178178

179+
private $moduleList = [];
180+
179181
public function __construct(
180182
string $path,
181183
PathItem $pathItem,
@@ -196,16 +198,20 @@ public function __construct(
196198
$parts = explode('/', $customRoute);
197199
array_pop($parts);
198200
array_pop($parts);
199-
$this->prefix = implode('/', $parts); # add everything except controller ID and action ID
201+
// $this->prefix = implode('/', $parts); # add everything (modules) except controller ID and action ID
200202

201-
$modulesPath = [];
203+
$modulesPathSection = $modulesPath = [];
204+
$container = '';
202205
foreach ($parts as $module) {
203-
$modulesPath[] = 'modules/' . $module;
206+
$modulesPathSection[$module] = 'modules/' . $module;
207+
$container .= ($container !== '' ? '/' : '') . ('modules/' . $module);
208+
$modulesPath[$module] = '@app/'.$container;
204209
}
210+
$this->moduleList = $modulesPath;
205211

206212
$this->prefixSettings = [
207213
'namespace' => 'app\\' . implode('\\', $parts) . '\\controllers',
208-
'path' => '@app/' . implode('/', $modulesPath) . '/controllers'
214+
'path' => '@app/' . implode('/', $modulesPathSection) . '/controllers'
209215
];
210216
}
211217

@@ -471,4 +477,12 @@ public function isNonCrudAction():bool
471477
{
472478
return in_array($this->type, [self::TYPE_DEFAULT, self::TYPE_RESOURCE_OPERATION]);
473479
}
480+
481+
/**
482+
* Returns list of modules this action is part of
483+
*/
484+
public function listModules(): array
485+
{
486+
return $this->moduleList;
487+
}
474488
}

tests/specs/issue_fix/14_nested_module_in_x_route/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
'openApiPath' => '@specs/issue_fix/14_nested_module_in_x_route/index.yml',
55
'generateUrls' => true,
66
'generateModels' => true,
7+
// 'useJsonApi' => true, // TODO for FractalAction
78
'excludeModels' => [
89
'Error',
910
],

0 commit comments

Comments
 (0)