Skip to content

Commit 417bfc4

Browse files
committed
Implement Module.php file generation for urlPrefixes.module config + fix issues + fix failing tests + refactor
1 parent ef2536a commit 417bfc4

File tree

8 files changed

+67
-18
lines changed

8 files changed

+67
-18
lines changed

src/lib/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function getOpenApi():OpenApi
183183
return $this->openApi;
184184
}
185185

186-
public function getPathFromNamespace(string $namespace):string
186+
public static function getPathFromNamespace(string $namespace): string
187187
{
188188
return Yii::getAlias('@' . str_replace('\\', '/', ltrim($namespace, '\\')));
189189
}

src/lib/generators/ControllersGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function generate():CodeFiles
5252
return new CodeFiles([]);
5353
}
5454
$namespace = $this->config->controllerNamespace ?? Yii::$app->controllerNamespace;
55-
$path = $this->config->getPathFromNamespace($namespace);
55+
$path = Config::getPathFromNamespace($namespace);
5656
$templateName = $this->config->useJsonApi ? 'controller_jsonapi.php' : 'controller.php';
5757

5858
foreach ($this->controllers as $controllerWithPrefix => $actions) {
@@ -65,7 +65,7 @@ public function generate():CodeFiles
6565
if (!empty($action->prefixSettings)) {
6666
$controllerNamespace = trim($action->prefixSettings['namespace'], '\\');
6767
$controllerPath = $action->prefixSettings['path']
68-
?? $this->config->getPathFromNamespace($controllerNamespace);
68+
?? Config::getPathFromNamespace($controllerNamespace);
6969
}
7070

7171
$routeParts = explode('/', $controllerWithPrefix);

src/lib/generators/ModelsGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public function generate():CodeFiles
4646
if (!$this->config->generateModels) {
4747
return $this->files;
4848
}
49-
$modelPath = $this->config->getPathFromNamespace($this->config->modelNamespace);
50-
$fakerPath = $this->config->getPathFromNamespace($this->config->fakerNamespace);
49+
$modelPath = Config::getPathFromNamespace($this->config->modelNamespace);
50+
$fakerPath = Config::getPathFromNamespace($this->config->fakerNamespace);
5151
if ($this->config->generateModelFaker) {
5252
$this->files->add(new CodeFile(
5353
Yii::getAlias("$fakerPath/BaseModelFaker.php"),

src/lib/generators/TransformersGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function generate():CodeFiles
4848
if (!$this->config->generateControllers || !$this->config->useJsonApi) {
4949
return $this->files;
5050
}
51-
$transformerPath = $this->config->getPathFromNamespace($this->config->transformerNamespace);
51+
$transformerPath = Config::getPathFromNamespace($this->config->transformerNamespace);
5252
foreach ($this->models as $model) {
5353
$transformer = Yii::createObject(Transformer::class, [
5454
$model,

src/lib/items/RouteData.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use cebe\openapi\spec\Operation;
1111
use cebe\openapi\spec\PathItem;
12+
use cebe\yii2openapi\lib\Config;
1213
use cebe\yii2openapi\lib\CustomSpecAttr;
1314
use yii\base\BaseObject;
1415
use yii\base\InvalidCallException;
@@ -324,12 +325,18 @@ protected function detectUrlPattern():void
324325
$this->prefixSettings = is_array($rule) ? $rule : [];
325326

326327
$modulesPath = [];
327-
if(isset($rule['module'])) {
328-
// var_dump($rule['module'], $prefix);
329-
$modulesPath[$modulesPath] = ['path' => '@app/TODO' , 'namespace' => 'app\\TODO' . ];
330-
328+
if (isset($rule['module'])) {
329+
$modPath = $rule['path'] ?? Config::getPathFromNamespace($rule['namespace']);
330+
if (str_ends_with($modPath, '/controllers')) {
331+
$modPath = substr($modPath, 0, -12);
332+
}
333+
$modNs = $rule['namespace'];
334+
if (str_ends_with($modNs, '\controllers')) {
335+
$modNs = substr($modNs, 0, -12);
336+
}
337+
$modulesPath[$rule['module']] = ['path' => $modPath, 'namespace' => $modNs];
331338
}
332-
$this->moduleList = [...$this->moduleList, ...$modulesPath];
339+
$this->moduleList = array_merge($this->moduleList, $modulesPath);
333340
}
334341
foreach (self::$patternMap as $type => $pattern) {
335342
if (preg_match($pattern, $this->unprefixedPath, $matches)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace \app\modules\petinfo;
4+
5+
class Module extends \yii\base\Module
6+
{
7+
8+
public function init()
9+
{
10+
parent::init();
11+
}
12+
13+
14+
}
15+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace \app\modules\petinfo;
4+
5+
class Module extends \yii\base\Module
6+
{
7+
8+
public function init()
9+
{
10+
parent::init();
11+
}
12+
13+
14+
}
15+

tests/unit/RestActionGeneratorTest.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ public function dataProviderWithUrlPrefixes():array
379379
'namespace' => '\app\api\v1\controllers',
380380
],
381381
'responseWrapper' => ['item' => '', 'list' => '', 'type' => 'array'],
382-
'xRoute' => null
382+
'xRoute' => null,
383+
'modulesList' => []
383384
]),
384385
new RestAction([
385386
'id' => 'create',
@@ -397,7 +398,8 @@ public function dataProviderWithUrlPrefixes():array
397398
'namespace' => '\app\api\v1\controllers',
398399
],
399400
'responseWrapper' => null,
400-
'xRoute' => null
401+
'xRoute' => null,
402+
'modulesList' => []
401403
]),
402404
new RestAction([
403405
'id' => 'view',
@@ -412,7 +414,8 @@ public function dataProviderWithUrlPrefixes():array
412414
'prefix' => 'animals',
413415
'prefixSettings' => [],
414416
'responseWrapper' => ['item' => '', 'list' => '', 'type' => 'object'],
415-
'xRoute' => null
417+
'xRoute' => null,
418+
'modulesList' => []
416419
]),
417420
new RestAction([
418421
'id' => 'delete',
@@ -427,7 +430,8 @@ public function dataProviderWithUrlPrefixes():array
427430
'prefix' => 'animals',
428431
'prefixSettings' => [],
429432
'responseWrapper' => null,
430-
'xRoute' => null
433+
'xRoute' => null,
434+
'modulesList' => []
431435
]),
432436
new RestAction([
433437
'id' => 'update',
@@ -442,7 +446,8 @@ public function dataProviderWithUrlPrefixes():array
442446
'prefix' => 'animals',
443447
'prefixSettings' => [],
444448
'responseWrapper' => ['item' => '', 'list' => '', 'type' => 'object'],
445-
'xRoute' => null
449+
'xRoute' => null,
450+
'modulesList' => []
446451
]),
447452
new RestAction([
448453
'id' => 'list',
@@ -455,7 +460,8 @@ public function dataProviderWithUrlPrefixes():array
455460
'modelName' => null,
456461
'modelFqn' => null,
457462
'responseWrapper' => null,
458-
'xRoute' => null
463+
'xRoute' => null,
464+
'modulesList' => []
459465
]),
460466
new RestAction([
461467
'id' => 'list',
@@ -474,7 +480,13 @@ public function dataProviderWithUrlPrefixes():array
474480
'namespace' => '\app\modules\petinfo\controllers',
475481
],
476482
'responseWrapper' => null,
477-
'xRoute' => null
483+
'xRoute' => null,
484+
'modulesList' => [
485+
'pet-info' => [
486+
'path' => '@app/modules/petinfo',
487+
'namespace' => '\app\modules\petinfo',
488+
]
489+
]
478490
]),
479491
],
480492
],

0 commit comments

Comments
 (0)