Skip to content

Commit df315d4

Browse files
committed
Detect module from namespace or path
1 parent 4f9e8d1 commit df315d4

File tree

6 files changed

+80
-3
lines changed

6 files changed

+80
-3
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,4 +568,3 @@ Professional support, consulting as well as software development services are av
568568
https://www.cebe.cc/en/contact
569569

570570
Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud).
571-

TODO.taskpaper

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
TODO.taskpaper
2+
3+
### Add validation rules by attribute name or pattern #30
4+
☐ Re-check
5+
☐ create failing test
6+
☐ implement the solution
7+
☐ fix failing tests if any
8+
☐ resolve TODOs if any
9+
☐ review PR
10+
☐ delete this file and submit PR

src/lib/items/FractalAction.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,21 @@ public function getOptionsRoute():string
106106
{
107107
//@TODO: re-check
108108
if ($this->prefix && !empty($this->prefixSettings)) {
109-
$prefix = $this->prefixSettings['module'] ?? $this->prefix;
110-
return trim($prefix, '/').'/'.$this->controllerId.'/options';
109+
// $prefix = $this->prefixSettings['module'] ?? $this->prefix;
110+
if (isset($this->prefixSettings['module'])) {
111+
$prefix = $this->prefixSettings['module'];
112+
return trim($prefix, '/') . '/' . $this->controllerId . '/options';
113+
} elseif (isset($this->prefixSettings['namespace']) && str_contains($this->prefixSettings['namespace'], '\modules\\')) {
114+
$prefix = static::computeModule('\\', $this->prefixSettings['namespace']);
115+
if ($prefix) {
116+
return trim($prefix, '/') . '/' . $this->controllerId . '/options';
117+
}
118+
} elseif (isset($this->prefixSettings['path']) && str_contains($this->prefixSettings['path'], '/modules/')) {
119+
$prefix = static::computeModule('/', $this->prefixSettings['path']);
120+
if ($prefix) {
121+
return trim($prefix, '/') . '/' . $this->controllerId . '/options';
122+
}
123+
}
111124
}
112125
return $this->controllerId.'/options';
113126
}
@@ -251,4 +264,29 @@ public function getIdParamType(): string
251264
}
252265
return $this->params[$this->idParam]['type'] === 'integer' ? 'int' : 'string';
253266
}
267+
268+
/**
269+
* @param string $separator
270+
* @param string $entity path or namespace
271+
* @return void
272+
*/
273+
public static function computeModule(string $separator, string $entity): ?string
274+
{
275+
$parts = explode($separator . 'modules' . $separator, $entity);
276+
if (empty($parts[1])) {
277+
return null;
278+
}
279+
if (str_contains($parts[1], 'controller')) {
280+
$result = explode($separator . 'controller', $parts[1]);
281+
$result = array_map(function ($val) {
282+
return str_replace('\\', '/', $val);
283+
}, $result);
284+
} else {
285+
$result = explode($separator, $parts[1]);
286+
}
287+
if (empty($result[0])) {
288+
return null;
289+
}
290+
return $result[0];
291+
}
254292
}

tests/specs/petstore_urlprefixes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
],
1010
'generateControllers' => true,
1111
'generateMigrations' => false,
12+
'useJsonApi' => true,
1213
'urlPrefixes' => [
1314
'animals' => '',
1415
'/info' => ['module' =>'petinfo','namespace' => '\app\modules\petinfo\controllers'],
16+
'/fgh' => ['namespace' => '\app\modules\fgh\controllers'],
17+
'/fgh2' => ['path' => '@app/modules/fgh2/controllers', 'namespace' => '\app\fgh2\controllers'],
1518
'/api/v1' => ['path' => '@app/modules/api/v1/controllers', 'namespace' => '\app\api\v1\controllers']
1619
]
1720
];

tests/specs/petstore_urlprefixes.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ paths:
111111
responses:
112112
'200':
113113
description: list of details
114+
/fgh/pet-detail2s:
115+
get:
116+
description: list all pet details
117+
responses:
118+
'200':
119+
description: list of details
120+
/fgh2/pet-detail3s:
121+
get:
122+
description: list all pet details
123+
responses:
124+
'200':
125+
description: list of details
114126

115127
components:
116128
schemas:

tests/unit/IssueFixTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,19 @@ public function test158BugGiiapiGeneratedRulesEnumWithTrim()
360360
]);
361361
$this->checkFiles($actualFiles, $expectedFiles);
362362
}
363+
364+
365+
public function testRecheckFractalActionsOptionsRoute()
366+
{
367+
$this->changeDbToMariadb();
368+
$testFile = Yii::getAlias("@specs/petstore_urlprefixes.php");
369+
$this->runGenerator($testFile);
370+
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
371+
// 'recursive' => true,
372+
// ]);
373+
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/158_bug_giiapi_generated_rules_enum_with_trim/maria"), [
374+
// 'recursive' => true,
375+
// ]);
376+
// $this->checkFiles($actualFiles, $expectedFiles);
377+
}
363378
}

0 commit comments

Comments
 (0)