Skip to content

Commit d0f86ce

Browse files
committed
Support for nested modules
1 parent 1bc56b8 commit d0f86ce

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

src/lib/generators/ControllersGenerator.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ public function generate():CodeFiles
9090
$classFileGenerator->generate()
9191
));
9292
}
93+
94+
foreach ($action->modulesList as $moduleId => $moduleDetail) {
95+
// only generate Module.php file if they do not exist, do not override
96+
if (!file_exists(Yii::getAlias($moduleDetail['path'] . "/Module.php"))) {
97+
// $moduleNamespace = str_replace('\controllers', '', $action->prefixSettings['namespace']);
98+
$moduleFileGenerator = $this->makeModuleFile('Module', $moduleDetail['namespace'], $moduleId, $action);
99+
$this->files->add(new CodeFile(
100+
Yii::getAlias($moduleDetail['path'] . "/Module.php"),
101+
$moduleFileGenerator->generate()
102+
));
103+
}
104+
}
93105
}
94106
return $this->files;
95107
}
@@ -160,4 +172,38 @@ protected function makeCustomController(
160172
$classFileGenerator->setClasses([$reflection]);
161173
return $classFileGenerator;
162174
}
175+
176+
/**
177+
* @param RestAction|FractalAction $action
178+
*/
179+
public function makeModuleFile(string $class, string $namespace, $moduleId, $action): FileGenerator
180+
{
181+
$file = new FileGenerator;
182+
$reflection = new ClassGenerator(
183+
$class,
184+
$namespace,
185+
null,
186+
'yii\base\Module'
187+
);
188+
189+
$moduleIds = array_keys($action->modulesList);
190+
$position = array_search($moduleId, $moduleIds);
191+
$childModuleId = $childModuleCode = null;
192+
if (array_key_exists($position + 1, $moduleIds)) { # if `true`, child module exists
193+
$childModuleId = $moduleIds[$position + 1];
194+
$childModuleNamespace = $action->modulesList[$childModuleId]['namespace'];
195+
$childModuleCode = <<<PHP
196+
\$this->modules = [
197+
'{$childModuleId}' => [
198+
// you should consider using a shorter namespace here!
199+
'class' => \\{$childModuleNamespace}\Module::class,
200+
],
201+
];
202+
PHP;
203+
}
204+
205+
$reflection->addMethod('init', [], AbstractMemberGenerator::FLAG_PUBLIC, 'parent::init();' . PHP_EOL . $childModuleCode);
206+
$file->setClasses([$reflection]);
207+
return $file;
208+
}
163209
}

src/lib/generators/RestActionGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ protected function prepareAction(
157157
'responseWrapper' => $responseWrapper,
158158
'prefix' => $routeData->getPrefix(),
159159
'prefixSettings' => $routeData->getPrefixSettings(),
160-
'xRoute' => $customRoute
160+
'xRoute' => $customRoute,
161+
'modulesList' => $routeData->listModules()
161162
],
162163
]);
163164
}

src/lib/items/RestAction.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ final class RestAction extends BaseObject
7575
*/
7676
public $xRoute;
7777

78+
/**
79+
* @var array list of module this action is part of. 'key' is module ID and 'value' is path where Module.php file must be generated
80+
*/
81+
public $modulesList = [];
82+
7883
/**
7984
* @var bool
8085
* @see $isDuplicate

src/lib/items/RouteData.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,12 @@ public function __construct(
201201
// $this->prefix = implode('/', $parts); # add everything (modules) except controller ID and action ID
202202

203203
$modulesPathSection = $modulesPath = [];
204-
$container = '';
205-
foreach ($parts as $module) {
206-
$modulesPathSection[$module] = 'modules/' . $module;
207-
$container .= ($container !== '' ? '/' : '') . ('modules/' . $module);
208-
$modulesPath[$module] = '@app/'.$container;
204+
$container = $modulesNamespaceSection = '';
205+
foreach ($parts as $moduleId) {
206+
$modulesPathSection[$moduleId] = 'modules/' . $moduleId;
207+
$container .= ($container !== '' ? '/' : '') . ('modules/' . $moduleId);
208+
$modulesNamespaceSection .= ($modulesNamespaceSection ? '\\' : '') . ($moduleId);
209+
$modulesPath[$moduleId] = ['path' => '@app/' . $container, 'namespace' => 'app\\' . $modulesNamespaceSection];
209210
}
210211
$this->moduleList = $modulesPath;
211212

0 commit comments

Comments
 (0)