Skip to content

Commit d372c76

Browse files
committed
generate the controller from the class data method
1 parent 4ac2b4b commit d372c76

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

src/Generator.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,15 @@ public function generateClass(string $className, string $templateName, array $va
8383
*
8484
* @param string $templateName Template name in Resources/skeleton to use
8585
* @param array $variables Array of variables to pass to the template
86+
* @param bool $isController Set to true if generating a Controller that needs
87+
* access to the TemplateComponentGenerator ("generator") in
88+
* the twig template. e.g. to create route attributes on the index method
8689
*
8790
* @return string The path where the file will be created
8891
*
8992
* @throws \Exception
9093
*/
91-
final public function generateClassFromClassData(ClassData $classData, string $templateName, array $variables = []): string
94+
final public function generateClassFromClassData(ClassData $classData, string $templateName, array $variables = [], bool $isController = false): string
9295
{
9396
$classData = $this->templateComponentGenerator->configureClass($classData);
9497
$targetPath = $this->fileManager->getRelativePathForFutureClass($classData->getFullClassName());
@@ -97,11 +100,13 @@ final public function generateClassFromClassData(ClassData $classData, string $t
97100
throw new \LogicException(\sprintf('Could not determine where to locate the new class "%s", maybe try with a full namespace like "\\My\\Full\\Namespace\\%s"', $classData->getFullClassName(), $classData->getClassName()));
98101
}
99102

100-
$variables = array_merge($variables, [
101-
'class_data' => $classData,
102-
]);
103+
$globalTemplateVars = ['class_data' => $classData];
103104

104-
$this->addOperation($targetPath, $templateName, $variables);
105+
if ($isController) {
106+
$globalTemplateVars['generator'] = $this->templateComponentGenerator;
107+
}
108+
109+
$this->addOperation($targetPath, $templateName, array_merge($variables, $globalTemplateVars));
105110

106111
return $targetPath;
107112
}

src/Maker/MakeController.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,29 @@ class: $isAbsolute ? substr($controllerClass, 1) : \sprintf('Controller\%s', $in
9393
.($isInvokable ? '.html.twig' : '/index.html.twig')
9494
;
9595

96-
$controllerPath = $generator->generateController(
97-
$controllerClassData->getFullClassName(),
98-
'controller/Controller.tpl.php',
99-
[
100-
'class_data' => $controllerClassData,
101-
// 'use_statements' => $useStatements,
102-
// 'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
103-
'route_path' => Str::asRoutePath($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
104-
'route_name' => Str::AsRouteName($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
105-
// 'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
106-
'method_name' => $isInvokable ? '__invoke' : 'index',
107-
'with_template' => $withTemplate,
108-
'template_name' => $templateName,
109-
]
110-
);
96+
$controllerPath = $generator->generateClassFromClassData($controllerClassData, 'controller/Controller.tpl.php', [
97+
'route_path' => Str::asRoutePath($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
98+
'route_name' => Str::AsRouteName($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
99+
'method_name' => $isInvokable ? '__invoke' : 'index',
100+
'with_template' => $withTemplate,
101+
'template_name' => $templateName,
102+
], true);
103+
104+
// $controllerPath = $generator->generateController(
105+
// $controllerClassData->getFullClassName(),
106+
// 'controller/Controller.tpl.php',
107+
// [
108+
// 'class_data' => $controllerClassData,
109+
// // 'use_statements' => $useStatements,
110+
// // 'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
111+
// 'route_path' => Str::asRoutePath($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
112+
// 'route_name' => Str::AsRouteName($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
113+
// // 'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
114+
// 'method_name' => $isInvokable ? '__invoke' : 'index',
115+
// 'with_template' => $withTemplate,
116+
// 'template_name' => $templateName,
117+
// ]
118+
// );
111119

112120
if ($withTemplate) {
113121
$generator->generateTemplate(

src/Resources/skeleton/controller/Controller.tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function <?= $method_name ?>(): <?php if ($with_template) { ?>Response<?p
1212
{
1313
<?php if ($with_template) { ?>
1414
return $this->render('<?= $template_name ?>', [
15-
'controller_name' => '<?= $class_name ?>',
15+
'controller_name' => '<?= $class_data->getClassName() ?>',
1616
]);
1717
<?php } else { ?>
1818
return $this->json([

0 commit comments

Comments
 (0)