Skip to content

Commit 4329aa7

Browse files
committed
simplify class generation
1 parent f3bd102 commit 4329aa7

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

src/Generator.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,36 @@ public function generateClass(string $className, string $templateName, array $va
7676
return $targetPath;
7777
}
7878

79+
/**
80+
* Future replacement for generateClass().
81+
*
82+
* @internal
83+
*
84+
* @param string $templateName Template name in Resources/skeleton to use
85+
* @param array $variables Array of variables to pass to the template
86+
*
87+
* @return string The path where the file will be created
88+
*
89+
* @throws \Exception
90+
*/
91+
final public function generateClassFromClassData(ClassData $classData, string $templateName, array $variables = []): string
92+
{
93+
$classData = $this->templateComponentGenerator->configureClass($classData);
94+
$targetPath = $this->fileManager->getRelativePathForFutureClass($classData->getFullClassName());
95+
96+
if (null === $targetPath) {
97+
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()));
98+
}
99+
100+
$variables = array_merge($variables, [
101+
'class_data' => $classData,
102+
]);
103+
104+
$this->addOperation($targetPath, $templateName, $variables);
105+
106+
return $targetPath;
107+
}
108+
79109
/**
80110
* Generate a normal file from a template.
81111
*

src/Maker/MakeValidator.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,17 @@ class: \sprintf('Validator\\%s', Str::removeSuffix($validatorClassData->getClass
6666
extendsClass: Constraint::class,
6767
);
6868

69-
$generator->generateClass(
70-
$validatorClassData->getFullClassName(),
69+
$generator->generateClassFromClassData(
70+
$validatorClassData,
7171
'validator/Validator.tpl.php',
7272
[
73-
'class_data' => $validatorClassData,
7473
'constraint_class_name' => $constraintDataClass->getClassName(),
7574
]
7675
);
7776

78-
$generator->generateClass(
79-
$constraintDataClass->getFullClassName(),
77+
$generator->generateClassFromClassData(
78+
$constraintDataClass,
8079
'validator/Constraint.tpl.php',
81-
[
82-
'class_data' => $constraintDataClass,
83-
]
8480
);
8581

8682
$generator->writeChanges();

src/Maker/MakeVoter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,9 @@ class: \sprintf('Security\Voter\%s', $input->getArgument('name')),
6060
]
6161
);
6262

63-
$generator->generateClass(
64-
$voterClassData->getFullClassName(),
63+
$generator->generateClassFromClassData(
64+
$voterClassData,
6565
'security/Voter.tpl.php',
66-
['class_data' => $voterClassData]
6766
);
6867

6968
$generator->writeChanges();

0 commit comments

Comments
 (0)