diff --git a/system/CLI/AbstractGeneratorCommand.php b/system/CLI/AbstractGeneratorCommand.php index a5ff8e081a8d..b42f7fb90aae 100644 --- a/system/CLI/AbstractGeneratorCommand.php +++ b/system/CLI/AbstractGeneratorCommand.php @@ -255,12 +255,13 @@ protected function buildContent(string $class): string if ( $this->sortImports - && preg_match('/(?P(?:^use [^;]+;$\n?)+)/m', $template, $match) === 1 + && preg_match('/(?P(?:^use [^;]+;\r?$\n?)+)/m', $template, $match) === 1 ) { - $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/Modern/ImportSortingGeneratorCommand.php b/tests/_support/Commands/Modern/ImportSortingGeneratorCommand.php index 2c6bf2b0b0ab..8eee7294a550 100644 --- a/tests/_support/Commands/Modern/ImportSortingGeneratorCommand.php +++ b/tests/_support/Commands/Modern/ImportSortingGeneratorCommand.php @@ -21,18 +21,10 @@ #[GeneratorCommand(component: 'Widget', template: 'config.tpl.php', directory: 'Widgets')] class ImportSortingGeneratorCommand extends AbstractGeneratorCommand { + public string $renderedTemplate = "renderedTemplate; } } diff --git a/tests/system/CLI/AbstractGeneratorCommandTest.php b/tests/system/CLI/AbstractGeneratorCommandTest.php index 25577ef5ded7..fe94ecfd979b 100644 --- a/tests/system/CLI/AbstractGeneratorCommandTest.php +++ b/tests/system/CLI/AbstractGeneratorCommandTest.php @@ -23,6 +23,7 @@ use PHPUnit\Framework\Attributes\After; use PHPUnit\Framework\Attributes\Before; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use Tests\Support\Commands\Modern\GeneratorFixtureCommand; use Tests\Support\Commands\Modern\ImportSortingGeneratorCommand; @@ -498,16 +499,37 @@ protected function execute(array $arguments, array $options): int }; } - public function testImportsAreSortedInGeneratedContent(): void + #[DataProvider('provideImportsAreSortedInGeneratedContent')] + public function testImportsAreSortedInGeneratedContent(string $template, string $expected): void { - $command = new ImportSortingGeneratorCommand(new Commands()); + $command = new ImportSortingGeneratorCommand(new Commands()); + $command->renderedTemplate = $template; $command->setInteractive(false); $command->run(['foo'], []); - $content = file_get_contents(APPPATH . 'Widgets' . DIRECTORY_SEPARATOR . 'Foo.php'); - $this->assertIsString($content); - $this->assertStringContainsString("use App\\Alpha;\nuse App\\Zebra;", $content); + $this->assertSame($expected, file_get_contents(APPPATH . 'Widgets' . DIRECTORY_SEPARATOR . 'Foo.php')); + } + + /** + * @return iterable + */ + public static function provideImportsAreSortedInGeneratedContent(): iterable + { + yield 'LF' => [ + " [ + " [ + "