Skip to content

Commit 55d5cba

Browse files
committed
Implement for both action type, fix bugs, fix failing tests - WIP
1 parent fe97ab4 commit 55d5cba

File tree

6 files changed

+58
-32
lines changed

6 files changed

+58
-32
lines changed

src/lib/generators/RestActionGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ protected function prepareAction(
119119
$this->knownModelClasses[$routeData->path] = $modelClass;
120120
}
121121

122-
if ($routeData->isRelationship()) {
122+
if (!empty($customRoute)) {
123+
$parts = explode('/', $customRoute);
124+
$controllerId = $parts[count($parts) - 2];
125+
} elseif ($routeData->isRelationship()) {
123126
$controllerId = $routeData->controller;
124127
$modelClass = Inflector::id2camel(Inflector::singularize($controllerId));
125128
$controllerId = isset($this->config->controllerModelMap[$modelClass])
@@ -129,9 +132,6 @@ protected function prepareAction(
129132
$controllerId = isset($this->config->controllerModelMap[$modelClass])
130133
? Inflector::camel2id($this->config->controllerModelMap[$modelClass])
131134
: Inflector::camel2id($modelClass);
132-
} elseif (!empty($customRoute)) {
133-
$parts = explode('/', $customRoute);
134-
$controllerId = $parts[count($parts) - 2];
135135
} else {
136136
$controllerId = $routeData->controller;
137137
}

src/lib/items/ActionHelperTrait.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ trait ActionHelperTrait
3333

3434
public function getOptionsRoute():string
3535
{
36+
$r = $this->getRoute();
37+
$r = explode('/', $r);
38+
array_pop($r);
39+
return implode('/', $r) . '/options';
40+
3641
if (!empty($this->prefixSettings)) {
3742
if (isset($this->prefixSettings['module'])) {
3843
$prefix = $this->prefixSettings['module'];
@@ -81,4 +86,38 @@ public static function finalOptionsRoute(string $prefix, string $controllerId):
8186
{
8287
return trim($prefix, '/') . '/' . $controllerId . '/options';
8388
}
89+
90+
public function getRoute(): string
91+
{
92+
if ($this->xRoute) {
93+
return $this->xRoute;
94+
}
95+
96+
if (!empty($this->prefixSettings)) {
97+
if (isset($this->prefixSettings['module'])) {
98+
$prefix = $this->prefixSettings['module'];
99+
// return static::finalOptionsRoute($prefix, $this->controllerId);
100+
return trim($prefix, '/') . '/' . $this->controllerId . '/' . $this->id;
101+
} elseif (isset($this->prefixSettings['namespace']) && str_contains($this->prefixSettings['namespace'], '\modules\\')) { # if `module` not present then check in namespace and then in path
102+
$prefix = static::computeModule('\\', $this->prefixSettings['namespace']);
103+
if ($prefix) {
104+
// return static::finalOptionsRoute($prefix, $this->controllerId);
105+
return trim($prefix, '/') . '/' . $this->controllerId . '/' . $this->id;
106+
}
107+
} elseif (isset($this->prefixSettings['path']) && str_contains($this->prefixSettings['path'], '/modules/')) {
108+
$prefix = static::computeModule('/', $this->prefixSettings['path']);
109+
if ($prefix) {
110+
// return static::finalOptionsRoute($prefix, $this->controllerId);
111+
return trim($prefix, '/') . '/' . $this->controllerId . '/' . $this->id;
112+
}
113+
}
114+
}
115+
116+
// if (!empty($this->prefixSettings)) {
117+
// $prefix = $this->prefixSettings['module'] ?? $this->prefix;
118+
// return trim($prefix, '/') . '/' . $this->controllerId . '/' . $this->id;
119+
// }
120+
121+
return $this->controllerId . '/' . $this->id;
122+
}
84123
}

src/lib/items/FractalAction.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,18 @@ private function templateFactory():FractalActionTemplates
9595
return $this->templateFactory;
9696
}
9797

98-
public function getRoute():string
99-
{
100-
if ($this->xRoute) {
101-
return $this->xRoute;
102-
}
103-
104-
if (!empty($this->prefixSettings)) {
105-
$prefix = $this->prefixSettings['module'] ?? $this->prefix;
106-
return trim($prefix, '/').'/'.$this->controllerId.'/'.$this->id;
107-
}
108-
return $this->controllerId.'/'.$this->id;
109-
}
98+
// public function getRoute():string
99+
// {
100+
// if ($this->xRoute) {
101+
// return $this->xRoute;
102+
// }
103+
//
104+
// if (!empty($this->prefixSettings)) {
105+
// $prefix = $this->prefixSettings['module'] ?? $this->prefix;
106+
// return trim($prefix, '/').'/'.$this->controllerId.'/'.$this->id;
107+
// }
108+
// return $this->controllerId.'/'.$this->id;
109+
// }
110110

111111
public function getBaseModelName():string
112112
{

src/lib/items/RestAction.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use function array_map;
1515
use function implode;
1616
use function strtr;
17-
use function trim;
1817
use function var_export;
1918

2019
/**
@@ -70,19 +69,7 @@ final class RestAction extends BaseObject
7069
*/
7170
public $responseWrapper;
7271

73-
public function getRoute():string
74-
{
75-
if ($this->xRoute) {
76-
return $this->xRoute;
77-
}
7872

79-
if (!empty($this->prefixSettings)) {
80-
$prefix = $this->prefixSettings['module'] ?? $this->prefix;
81-
return trim($prefix, '/') . '/' . $this->controllerId . '/' . $this->id;
82-
}
83-
84-
return $this->controllerId . '/' . $this->id;
85-
}
8673

8774
public function getBaseModelName():string
8875
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
'generateMigrations' => false,
1313
'generateModelFaker' => false,
1414
'urlPrefixes' => [
15-
'hi' => ['module' => 'greet', 'namespace' => 'app\greet'],
15+
'hi' => ['module' => 'greet', 'namespace' => 'app\greet'], // TODO `hi/` trailing slash in generated URL rules config
1616
'abc' => ['module' => 'abc', 'namespace' => 'app\abc'],
1717
]
1818
];

tests/unit/issues/Issue14Test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testNestedModuleInXRouteFractalAction()
3232

3333
$testFile = Yii::getAlias($tmpConfigFile);
3434
$this->runGenerator($testFile);
35-
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
35+
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ TODO
3636
// 'recursive' => true,
3737
// ]);
3838
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/14_nested_module_in_x_route/mysql"), [
@@ -65,7 +65,7 @@ public function testModuleConfigInUrlPrefixesFractalAction()
6565

6666
$testFile = Yii::getAlias($tmpConfigFile);
6767
$this->runGenerator($testFile);
68-
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
68+
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ TODO
6969
// 'recursive' => true,
7070
// ]);
7171
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/14_module_config_in_url_prefixes/mysql"), [

0 commit comments

Comments
 (0)