Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions system/CLI/GeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,16 @@ protected function buildContent(string $class): string
if (
$this->sortImports
&& preg_match(
'/(?P<imports>(?:^use [^;]+;$\n?)+)/m',
'/(?P<imports>(?:^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;
Expand Down
5 changes: 4 additions & 1 deletion tests/_support/Commands/LanguageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
19 changes: 19 additions & 0 deletions tests/system/Commands/ConfigurableSortImportsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.7.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading