Skip to content

Commit db9a018

Browse files
committed
Implement for FractalAction
1 parent 963c7e2 commit db9a018

File tree

7 files changed

+159
-24
lines changed

7 files changed

+159
-24
lines changed

src/lib/generators/JsonActionGenerator.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,20 @@ protected function prepareAction(
7474
}
7575
}
7676

77+
$actionId = $routeData->isNonCrudAction() ? trim("{$actionType}-{$routeData->action}", '-')
78+
: "$actionType{$routeData->action}";
79+
if (!empty($customRoute)) {
80+
// $actionType = '';
81+
$parts = explode('/', $customRoute);
82+
$controllerId = $parts[count($parts) - 2];
83+
$actionId = $parts[count($parts) - 1];
84+
}
85+
7786
return Yii::createObject(FractalAction::class, [
7887
[
7988
'singularResourceKey' => $this->config->singularResourceKeys,
8089
'type' => $routeData->type,
81-
'id' => $routeData->isNonCrudAction() ? trim("{$actionType}-{$routeData->action}", '-')
82-
: "$actionType{$routeData->action}",
90+
'id' => $actionId,
8391
'controllerId' => $controllerId,
8492
'urlPath' => $routeData->path,
8593
'requestMethod' => strtoupper($method),
@@ -96,6 +104,8 @@ protected function prepareAction(
96104
'expectedRelations' => $expectedRelations,
97105
'prefix' => $routeData->getPrefix(),
98106
'prefixSettings' => $routeData->getPrefixSettings(),
107+
'xRoute' => $customRoute,
108+
'modulesList' => $routeData->listModules()
99109
],
100110
]);
101111
}

src/lib/items/ActionHelperTrait.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,28 @@
99

1010
trait ActionHelperTrait
1111
{
12-
public ?string $xRoute;
12+
public ?string $xRoute = null;
1313

1414
# list of module this action is part of. 'key' is module ID and 'value' is path where Module.php file must be generated
1515
public array $modulesList = [];
1616

17+
/**
18+
* @see $isDuplicate
19+
* https://github.com/cebe/yii2-openapi/issues/84
20+
* see `x-route` in README.md
21+
* Used for generating only one action for paths like: `GET /calendar/domains` and `GET /calendar/domains/{id}` given that they have same `x-route`.
22+
* If duplicates routes have same params then `false`, else action is generated with no (0) params `true`
23+
*/
24+
public bool $zeroParams = false;
25+
26+
/**
27+
* https://github.com/cebe/yii2-openapi/issues/84
28+
* Generate only one action for paths like: `GET /calendar/domains` and `GET /calendar/domains/{id}` given that they have same `x-route`.
29+
* @see $zeroParams
30+
* see `x-route` in README.md
31+
*/
32+
public bool $isDuplicate = false;
33+
1734
public function getOptionsRoute():string
1835
{
1936
if (!empty($this->prefixSettings)) {

src/lib/items/FractalAction.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ private function templateFactory():FractalActionTemplates
9797

9898
public function getRoute():string
9999
{
100+
if ($this->xRoute) {
101+
return $this->xRoute;
102+
}
103+
100104
if (!empty($this->prefixSettings)) {
101105
$prefix = $this->prefixSettings['module'] ?? $this->prefix;
102106
return trim($prefix, '/').'/'.$this->controllerId.'/'.$this->id;

src/lib/items/RestAction.php

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

73-
/**
74-
* @var bool
75-
* @see $isDuplicate
76-
* https://github.com/cebe/yii2-openapi/issues/84
77-
* see `x-route` in README.md
78-
* Used for generating only one action for paths like: `GET /calendar/domains` and `GET /calendar/domains/{id}` given that they have same `x-route`.
79-
* If duplicates routes have same params then `false`, else action is generated with no (0) params `true`
80-
*/
81-
public $zeroParams = false;
82-
83-
/**
84-
* @var bool
85-
* https://github.com/cebe/yii2-openapi/issues/84
86-
* Generate only one action for paths like: `GET /calendar/domains` and `GET /calendar/domains/{id}` given that they have same `x-route`.
87-
* @see $zeroParams
88-
* see `x-route` in README.md
89-
*/
90-
public $isDuplicate = false;
91-
9273
public function getRoute():string
9374
{
9475
if ($this->xRoute) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
return [
44
'openApiPath' => '@specs/issue_fix/14_module_config_in_url_prefixes/index.yml',
55
'generateUrls' => true,
6-
'generateModels' => false,
6+
'generateModels' => true,
77
// 'useJsonApi' => true, // TODO for FractalAction
88
'excludeModels' => [
99
'Error',
@@ -13,5 +13,6 @@
1313
'generateModelFaker' => false,
1414
'urlPrefixes' => [
1515
'hi' => ['module' => 'greet', 'namespace' => 'app\greet'],
16+
'abc' => ['module' => 'abc', 'namespace' => 'app\abc'],
1617
]
1718
];

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,86 @@ paths:
1111
responses:
1212
'200':
1313
description: The information
14+
/bye:
15+
get:
16+
summary: List
17+
operationId: list
18+
responses:
19+
'200':
20+
description: The information
21+
post:
22+
summary: List
23+
operationId: list
24+
responses:
25+
'200':
26+
description: The information
27+
'/abc/task/{id}':
28+
parameters:
29+
- name: id
30+
in: path
31+
description: ID of the task.
32+
required: true
33+
schema:
34+
type: integer
35+
get:
36+
operationId: getTask
37+
summary: Get a Task by ID
38+
description: Returns a single task by its unique ID.
39+
responses:
40+
'200':
41+
$ref: '#/components/responses/Task'
42+
'403':
43+
description: Response if the currently authenticated user has no access to this task.
44+
'404':
45+
description: Response if the task does not exist.
46+
tags:
47+
- Tasks
48+
49+
components:
50+
schemas:
51+
_TaskResource:
52+
type: object
53+
properties:
54+
id:
55+
type: integer
56+
example: 1
57+
type:
58+
type: string
59+
enum:
60+
- tasks
61+
attributes:
62+
$ref: '#/components/schemas/Task'
63+
relationships:
64+
type: object
65+
JSONAPI_links:
66+
type: object
67+
example:
68+
self: 'https://api.example.com/...'
69+
additionalProperties:
70+
oneOf:
71+
- type: string
72+
- type: object
73+
properties:
74+
href:
75+
type: string
76+
meta:
77+
type: object
78+
Task:
79+
type: object
80+
properties:
81+
id:
82+
type: integer
83+
title:
84+
type: string
85+
responses:
86+
Task:
87+
description: Task
88+
content:
89+
application/vnd.api+json:
90+
schema:
91+
type: object
92+
properties:
93+
data:
94+
$ref: '#/components/schemas/_TaskResource'
95+
links:
96+
$ref: '#/components/schemas/JSONAPI_links'

tests/unit/issues/Issue14Test.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,26 @@ public function testNestedModuleInXRoute()
2222
$this->checkFiles($actualFiles, $expectedFiles);
2323
}
2424

25-
public function testModuleConfigInUrlPrefixes()
25+
public function testNestedModuleInXRouteFractalAction()
26+
{
27+
$config = [];
28+
$config = require Yii::getAlias("@specs/issue_fix/14_nested_module_in_x_route/index.php");
29+
$config['useJsonApi'] = true;
30+
$tmpConfigFile = Yii::getAlias("@runtime") . "/tmp-config-35.php";
31+
file_put_contents($tmpConfigFile, '<?php return ' . var_export($config, true) . ';');
32+
33+
$testFile = Yii::getAlias($tmpConfigFile);
34+
$this->runGenerator($testFile);
35+
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
36+
// 'recursive' => true,
37+
// ]);
38+
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/14_nested_module_in_x_route/mysql"), [
39+
// 'recursive' => true,
40+
// ]);
41+
// $this->checkFiles($actualFiles, $expectedFiles);
42+
}
43+
44+
public function testModuleConfigInUrlPrefixes() # RestAction
2645
{
2746
$testFile = Yii::getAlias("@specs/issue_fix/14_module_config_in_url_prefixes/index.php");
2847
$this->runGenerator($testFile);
@@ -34,4 +53,24 @@ public function testModuleConfigInUrlPrefixes()
3453
]);
3554
$this->checkFiles($actualFiles, $expectedFiles);
3655
}
56+
57+
public function testModuleConfigInUrlPrefixesFractalAction()
58+
{
59+
$config = [];
60+
$config = require Yii::getAlias("@specs/issue_fix/14_module_config_in_url_prefixes/index.php");
61+
$config['useJsonApi'] = true;
62+
$tmpConfigFile = Yii::getAlias("@runtime") . "/tmp-config-35.php";
63+
file_put_contents($tmpConfigFile, '<?php return ' . var_export($config, true) . ';');
64+
65+
66+
$testFile = Yii::getAlias($tmpConfigFile);
67+
$this->runGenerator($testFile);
68+
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
69+
// 'recursive' => true,
70+
// ]);
71+
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/14_module_config_in_url_prefixes/mysql"), [
72+
// 'recursive' => true,
73+
// ]);
74+
// $this->checkFiles($actualFiles, $expectedFiles);
75+
}
3776
}

0 commit comments

Comments
 (0)