From 1bf40827aa35599d7801def9b22b34ee15e76c18 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Mon, 12 Oct 2026 13:37:39 +0800 Subject: [PATCH] fix: sort generator imports in templates with CRLF line endings --- system/CLI/GeneratorTrait.php | 7 ++++--- tests/_support/Commands/LanguageCommand.php | 5 ++++- .../Commands/ConfigurableSortImportsTest.php | 19 +++++++++++++++++++ user_guide_src/source/changelogs/v4.7.6.rst | 1 + 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/system/CLI/GeneratorTrait.php b/system/CLI/GeneratorTrait.php index 45e72c7bda71..c4b439cdc563 100644 --- a/system/CLI/GeneratorTrait.php +++ b/system/CLI/GeneratorTrait.php @@ -399,15 +399,16 @@ protected function buildContent(string $class): string if ( $this->sortImports && preg_match( - '/(?P(?:^use [^;]+;$\n?)+)/m', + '/(?P(?:^use [^;]+;\r?$\n?)+)/m', $template, $match, ) ) { - $imports = explode("\n", trim($match['imports'])); + $eol = str_contains($match['imports'], "\r\n") ? "\r\n" : "\n"; + $imports = explode("\n", str_replace("\r\n", "\n", trim($match['imports']))); sort($imports); - return str_replace(trim($match['imports']), implode("\n", $imports), $template); + return str_replace(trim($match['imports']), implode($eol, $imports), $template); } return $template; diff --git a/tests/_support/Commands/LanguageCommand.php b/tests/_support/Commands/LanguageCommand.php index 17fba1881c94..c8a48325ae41 100644 --- a/tests/_support/Commands/LanguageCommand.php +++ b/tests/_support/Commands/LanguageCommand.php @@ -27,6 +27,7 @@ class LanguageCommand extends BaseCommand protected $options = [ '--lang' => 'The language folder to save the file.', '--sort' => 'Turn on/off the sortImports flag.', + '--crlf' => 'Use CRLF line endings in the template.', ]; public function run(array $params): void @@ -46,6 +47,8 @@ public function run(array $params): void protected function prepare(string $class): string { - return file_get_contents(__DIR__ . '/Foobar.php') ?: ''; + $template = file_get_contents(__DIR__ . '/Foobar.php') ?: ''; + + return $this->getOption('crlf') === true ? str_replace("\n", "\r\n", $template) : $template; } } diff --git a/tests/system/Commands/ConfigurableSortImportsTest.php b/tests/system/Commands/ConfigurableSortImportsTest.php index 4f54390d7ffe..a86cd66a420a 100644 --- a/tests/system/Commands/ConfigurableSortImportsTest.php +++ b/tests/system/Commands/ConfigurableSortImportsTest.php @@ -55,6 +55,25 @@ public function testEnabledSortImportsWillDisruptLanguageFilePublish(): void } } + public function testSortImportsKeepsCrlfLineEndings(): void + { + command('publish:language --lang fr --crlf'); + + $file = APPPATH . 'Language/fr/Foobar.php'; + $this->assertFileExists($file); + $this->assertStringContainsString( + "\r\n\r\nuse CodeIgniter\\CLI\\CLI;\r\nuse Config\\App;\r\n\r\n", + (string) file_get_contents($file), + ); + if (is_file($file)) { + unlink($file); + } + $dir = dirname($file); + if (is_dir($dir)) { + rmdir($dir); + } + } + public function testDisabledSortImportsWillNotAffectLanguageFilesPublish(): void { command('publish:language --lang ar --sort off'); diff --git a/user_guide_src/source/changelogs/v4.7.6.rst b/user_guide_src/source/changelogs/v4.7.6.rst index 04eb1be1d52b..360221806c4f 100644 --- a/user_guide_src/source/changelogs/v4.7.6.rst +++ b/user_guide_src/source/changelogs/v4.7.6.rst @@ -30,6 +30,7 @@ Deprecations Bugs Fixed ********** +- **Commands:** Fixed a bug where generator commands did not sort the imports of a template with CRLF line endings. - **Security:** Fixed a bug where a valid JSON request body that is not an object (for example a JSON array) was corrupted after CSRF verification when the token was sent via the ``X-CSRF-TOKEN`` header.