Skip to content

Commit aaa0d31

Browse files
committed
refactor to track all generated files
1 parent 71dae37 commit aaa0d31

File tree

3 files changed

+51
-30
lines changed

3 files changed

+51
-30
lines changed

src/Command/MakerCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
102102
throw new \LogicException('Make sure to call the writeChanges() method on the generator.');
103103
}
104104

105-
$this->linter->lintPhpTemplate($this->generator->getGeneratedPhpFiles());
105+
$this->linter->lintFiles($this->generator->getGeneratedFiles());
106106

107107
return 0;
108108
}

src/Generator.php

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Generator
2626
private GeneratorTwigHelper $twigHelper;
2727
private array $pendingOperations = [];
2828
private ?TemplateComponentGenerator $templateComponentGenerator;
29-
private array $generatedPhpFiles = [];
29+
private array $generatedFiles = [];
3030

3131
public function __construct(
3232
private FileManager $fileManager,
@@ -70,13 +70,13 @@ public function generateClass(string $className, string $templateName, array $va
7070

7171
$this->addOperation($targetPath, $templateName, $variables);
7272

73-
$this->generatedPhpFiles[] = $targetPath;
74-
7573
return $targetPath;
7674
}
7775

7876
/**
7977
* Generate a normal file from a template.
78+
*
79+
* @return void
8080
*/
8181
public function generateFile(string $targetPath, string $templateName, array $variables = [])
8282
{
@@ -87,6 +87,9 @@ public function generateFile(string $targetPath, string $templateName, array $va
8787
$this->addOperation($targetPath, $templateName, $variables);
8888
}
8989

90+
/**
91+
* @return void
92+
*/
9093
public function dumpFile(string $targetPath, string $contents)
9194
{
9295
$this->pendingOperations[$targetPath] = [
@@ -163,29 +166,6 @@ public function getRootDirectory(): string
163166
return $this->fileManager->getRootDirectory();
164167
}
165168

166-
private function addOperation(string $targetPath, string $templateName, array $variables): void
167-
{
168-
if ($this->fileManager->fileExists($targetPath)) {
169-
throw new RuntimeCommandException(sprintf('The file "%s" can\'t be generated because it already exists.', $this->fileManager->relativizePath($targetPath)));
170-
}
171-
172-
$variables['relative_path'] = $this->fileManager->relativizePath($targetPath);
173-
174-
$templatePath = $templateName;
175-
if (!file_exists($templatePath)) {
176-
$templatePath = __DIR__.'/Resources/skeleton/'.$templateName;
177-
178-
if (!file_exists($templatePath)) {
179-
throw new \Exception(sprintf('Cannot find template "%s"', $templateName));
180-
}
181-
}
182-
183-
$this->pendingOperations[$targetPath] = [
184-
'template' => $templatePath,
185-
'variables' => $variables,
186-
];
187-
}
188-
189169
public function hasPendingOperations(): bool
190170
{
191171
return !empty($this->pendingOperations);
@@ -199,6 +179,8 @@ public function hasPendingOperations(): bool
199179
public function writeChanges()
200180
{
201181
foreach ($this->pendingOperations as $targetPath => $templateData) {
182+
$this->generatedFiles[] = $targetPath;
183+
202184
if (isset($templateData['contents'])) {
203185
$this->fileManager->dumpFile($targetPath, $templateData['contents']);
204186

@@ -207,7 +189,7 @@ public function writeChanges()
207189

208190
$this->fileManager->dumpFile(
209191
$targetPath,
210-
$this->getFileContentsForPendingOperation($targetPath, $templateData)
192+
$this->getFileContentsForPendingOperation($targetPath)
211193
);
212194
}
213195

@@ -245,9 +227,12 @@ public function generateTemplate(string $targetPath, string $templateName, array
245227
);
246228
}
247229

248-
public function getGeneratedPhpFiles(): array
230+
/**
231+
* Get the full path of each file created by the Generator.
232+
*/
233+
public function getGeneratedFiles(): array
249234
{
250-
return $this->generatedPhpFiles;
235+
return $this->generatedFiles;
251236
}
252237

253238
/**
@@ -259,4 +244,27 @@ public static function getControllerBaseClass(): ClassNameDetails
259244

260245
return new ClassNameDetails(AbstractController::class, '\\');
261246
}
247+
248+
private function addOperation(string $targetPath, string $templateName, array $variables): void
249+
{
250+
if ($this->fileManager->fileExists($targetPath)) {
251+
throw new RuntimeCommandException(sprintf('The file "%s" can\'t be generated because it already exists.', $this->fileManager->relativizePath($targetPath)));
252+
}
253+
254+
$variables['relative_path'] = $this->fileManager->relativizePath($targetPath);
255+
256+
$templatePath = $templateName;
257+
if (!file_exists($templatePath)) {
258+
$templatePath = __DIR__.'/Resources/skeleton/'.$templateName;
259+
260+
if (!file_exists($templatePath)) {
261+
throw new \Exception(sprintf('Cannot find template "%s"', $templateName));
262+
}
263+
}
264+
265+
$this->pendingOperations[$targetPath] = [
266+
'template' => $templatePath,
267+
'variables' => $variables,
268+
];
269+
}
262270
}

src/Util/TemplateLinter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ public function __construct(
3131
$this->setBinary();
3232
}
3333

34+
public function lintFiles(array $templateFilePaths): void
35+
{
36+
$phpFiles = [];
37+
38+
foreach ($templateFilePaths as $filePath) {
39+
if (str_ends_with($filePath, '.php')) {
40+
$phpFiles[] = $filePath;
41+
}
42+
}
43+
44+
$this->lintPhpTemplate($phpFiles);
45+
}
46+
3447
public function lintPhpTemplate(string|array $templateFilePath): void
3548
{
3649
if (\is_string($templateFilePath)) {

0 commit comments

Comments
 (0)