Skip to content

Commit dcdebc5

Browse files
committed
fix: sort generator imports in templates with CRLF line endings
1 parent 9c62323 commit dcdebc5

4 files changed

Lines changed: 29 additions & 4 deletions

File tree

‎system/CLI/GeneratorTrait.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,15 +399,16 @@ protected function buildContent(string $class): string
399399
if (
400400
$this->sortImports
401401
&& preg_match(
402-
'/(?P<imports>(?:^use [^;]+;$\n?)+)/m',
402+
'/(?P<imports>(?:^use [^;]+;\r?$\n?)+)/m',
403403
$template,
404404
$match,
405405
)
406406
) {
407-
$imports = explode("\n", trim($match['imports']));
407+
$eol = str_contains($match['imports'], "\r\n") ? "\r\n" : "\n";
408+
$imports = explode("\n", str_replace("\r\n", "\n", trim($match['imports'])));
408409
sort($imports);
409410

410-
return str_replace(trim($match['imports']), implode("\n", $imports), $template);
411+
return str_replace(trim($match['imports']), implode($eol, $imports), $template);
411412
}
412413

413414
return $template;

‎tests/_support/Commands/LanguageCommand.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class LanguageCommand extends BaseCommand
2727
protected $options = [
2828
'--lang' => 'The language folder to save the file.',
2929
'--sort' => 'Turn on/off the sortImports flag.',
30+
'--crlf' => 'Use CRLF line endings in the template.',
3031
];
3132

3233
public function run(array $params): void
@@ -46,6 +47,8 @@ public function run(array $params): void
4647

4748
protected function prepare(string $class): string
4849
{
49-
return file_get_contents(__DIR__ . '/Foobar.php') ?: '';
50+
$template = file_get_contents(__DIR__ . '/Foobar.php') ?: '';
51+
52+
return $this->getOption('crlf') === true ? str_replace("\n", "\r\n", $template) : $template;
5053
}
5154
}

‎tests/system/Commands/ConfigurableSortImportsTest.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,25 @@ public function testEnabledSortImportsWillDisruptLanguageFilePublish(): void
5555
}
5656
}
5757

58+
public function testSortImportsKeepsCrlfLineEndings(): void
59+
{
60+
command('publish:language --lang fr --crlf');
61+
62+
$file = APPPATH . 'Language/fr/Foobar.php';
63+
$this->assertFileExists($file);
64+
$this->assertStringContainsString(
65+
"\r\n\r\nuse CodeIgniter\\CLI\\CLI;\r\nuse Config\\App;\r\n\r\n",
66+
(string) file_get_contents($file),
67+
);
68+
if (is_file($file)) {
69+
unlink($file);
70+
}
71+
$dir = dirname($file);
72+
if (is_dir($dir)) {
73+
rmdir($dir);
74+
}
75+
}
76+
5877
public function testDisabledSortImportsWillNotAffectLanguageFilesPublish(): void
5978
{
6079
command('publish:language --lang ar --sort off');

‎user_guide_src/source/changelogs/v4.7.6.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Deprecations
3030
Bugs Fixed
3131
**********
3232

33+
- **Commands:** Fixed a bug where generator commands did not sort the imports of a template with CRLF line endings.
34+
3335
See the repo's
3436
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
3537
for a complete list of bugs fixed.

0 commit comments

Comments
 (0)