Skip to content

Commit

Permalink
[BUGFIX] Append content to existing file (#4226)
Browse files Browse the repository at this point in the history
Resolves: #4225
  • Loading branch information
sabbelasichon committed Apr 11, 2024
1 parent ac2a10b commit bc09faa
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
Expand Up @@ -119,12 +119,16 @@ private function writeConfigurationToFile(string $newConfigurationFile, string $
'$GLOBALS[\'TCA\'][\'%s\'][\'ctrl\'][\'security\'][\'ignorePageTypeRestriction\'] = true;',
$tableName
);
$this->filesystem->write($newConfigurationFile, <<<CODE
if ($this->filesystem->fileExists($newConfigurationFile)) {
$this->filesystem->appendToFile($newConfigurationFile, $content . PHP_EOL);
} else {
$this->filesystem->write($newConfigurationFile, <<<CODE
<?php
{$content}
CODE
);
);
}
}
}
2 changes: 2 additions & 0 deletions src/Contract/FilesystemInterface.php
Expand Up @@ -11,4 +11,6 @@ public function write(string $location, string $contents): void;
public function fileExists(string $location): bool;

public function read(string $location): string;

public function appendToFile(string $location, string $content): void;
}
9 changes: 9 additions & 0 deletions src/Filesystem/FlysystemFilesystem.php
Expand Up @@ -33,4 +33,13 @@ public function read(string $location): string
{
return $this->filesystemOperator->read($location);
}

public function appendToFile(string $location, string $content): void
{
$existingContent = $this->read($location);

$existingContent .= PHP_EOL . $content;

$this->write($location, $existingContent);
}
}
@@ -0,0 +1,5 @@
<?php

$GLOBALS['TCA']['other_table']['ctrl']['security']['ignorePageTypeRestriction'] = true;

$GLOBALS['TCA']['tx_table_with_existing_tca_configuration_file']['ctrl']['security']['ignorePageTypeRestriction'] = true;
Expand Up @@ -5,7 +5,6 @@ namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\MoveAllowTableOnStandardPagesToTC
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

ExtensionManagementUtility::allowTableOnStandardPages('tx_table_with_existing_tca_configuration_file');
ExtensionManagementUtility::allowTableOnStandardPages('tx_table_with_existing_tca_configuration_file_and_security_key');
ExtensionManagementUtility::allowTableOnStandardPages('tx_table_without_existing_tca_configuration_file');

?>
Expand Down
Expand Up @@ -19,12 +19,26 @@ protected function setUp(): void

public function test(): void
{
$this->filesystem->write(
__DIR__ . '/Fixture/Configuration/TCA/Overrides/tx_table_with_existing_tca_configuration_file.php',
<<<'CODE'
<?php
$GLOBALS['TCA']['other_table']['ctrl']['security']['ignorePageTypeRestriction'] = true;
CODE
);

$this->doTestFile(__DIR__ . '/Fixture/ext_tables.php.inc');

$this->assertThatConfigurationFileHasNewIgnorePageTypeRestriction(
__DIR__ . '/Fixture/Configuration/TCA/Overrides/%s.php',
'tx_table_without_existing_tca_configuration_file'
);
$this->assertThatConfigurationFileHasNewIgnorePageTypeRestriction(
__DIR__ . '/Fixture/Configuration/TCA/Overrides/%s.php',
'tx_table_with_existing_tca_configuration_file'
);
}

public function provideConfigFilePath(): string
Expand Down

0 comments on commit bc09faa

Please sign in to comment.