Skip to content

Commit

Permalink
BUGFIX Fixed newlines working properly across different platforms - W…
Browse files Browse the repository at this point in the history
…indows, for example, won't work properly with just \n so use PHP_EOL for a cross-platform solution

MINOR Fixed appropriate failing tests to use PHP_EOL (from r92220)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@92460 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information
chillu committed Nov 21, 2009
1 parent a099f79 commit abb04b2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
15 changes: 9 additions & 6 deletions core/i18nTextCollector.php
Expand Up @@ -298,7 +298,8 @@ protected function entitySpecFromRegexMatches($regs, $_namespace = null) {


// remove starting comma and any newlines
$prio = ($regs[10]) ? trim(preg_replace('/\n/','',substr($regs[10],1))) : null;
$eol = PHP_EOL;
$prio = ($regs[10]) ? trim(preg_replace("/$eol/", '', substr($regs[10],1))) : null;

// remove wrapping quotes
$comment = ($regs[12]) ? substr($regs[12],1,-1) : null;
Expand All @@ -320,6 +321,7 @@ protected function entitySpecFromRegexMatches($regs, $_namespace = null) {
*/
public function langArrayCodeForEntitySpec($entityFullName, $entitySpec) {
$php = '';
$eol = PHP_EOL;

$entityParts = explode('.', $entityFullName);
if(count($entityParts) > 1) {
Expand All @@ -338,15 +340,15 @@ public function langArrayCodeForEntitySpec($entityFullName, $entitySpec) {

$php .= '$lang[\'' . $this->defaultLocale . '\'][\'' . $namespace . '\'][\'' . $entity . '\'] = ';
if ($prio) {
$php .= "array(\n\t'" . $value . "',\n\t" . $prio;
$php .= "array($eol\t'" . $value . "',$eol\t" . $prio;
if ($comment) {
$php .= ",\n\t'" . $comment . '\'';
$php .= ",$eol\t'" . $comment . '\'';
}
$php .= "\n);";
$php .= "$eol);";
} else {
$php .= '\'' . $value . '\';';
}
$php .= "\n";
$php .= "$eol";

return $php;
}
Expand All @@ -358,6 +360,7 @@ protected function writeMasterStringFile($entitiesByModule) {
// Write each module language file
if($entitiesByModule) foreach($entitiesByModule as $module => $entities) {
$php = '';
$eol = PHP_EOL;

// Create folder for lang files
$langFolder = $this->baseSavePath . '/' . $module . '/lang';
Expand All @@ -380,7 +383,7 @@ protected function writeMasterStringFile($entitiesByModule) {
user_error('i18nTextCollector->writeMasterStringFile(): Invalid PHP language file. Error: ' . $e->toString(), E_USER_ERROR);
}

fwrite($fh, "<"."?php\n\nglobal \$lang;\n\n" . $php . "\n?".">");
fwrite($fh, "<"."?php{$eol}{$eol}global \$lang;{$eol}{$eol}" . $php . "{$eol}?".">");
fclose($fh);

//Debug::message("Created file: $langFolder/" . $this->defaultLocale . ".php", false);
Expand Down
3 changes: 2 additions & 1 deletion parsers/SQLFormatter.php
Expand Up @@ -45,8 +45,9 @@ public function formatHTML($sql) {
* messing with possible content fragments in the query.
*/
protected function addNewlines($sql, $useHtmlFormatting = false) {
$eol = PHP_EOL;
foreach(self::$newline_before_tokens as $token) {
$breakToken = ($useHtmlFormatting) ? "<br />\n" : "\n";
$breakToken = ($useHtmlFormatting) ? "<br />$eol" : $eol;
$sql = preg_replace('/[^\n](' . $token . ')/', $breakToken . '$1', $sql);
}

Expand Down
1 change: 1 addition & 0 deletions tests/SQLFormatterTest.php
Expand Up @@ -16,6 +16,7 @@ function testNewlineHanding() {
FROM Test
WHERE 'From' = "Where"
SQL;

$this->assertEquals($formatter->formatPlain($sqlBefore), $sqlAfter,
'correct replacement of newlines and don\'t replace non-uppercase tokens'
);
Expand Down
12 changes: 7 additions & 5 deletions tests/i18n/i18nTextCollectorTest.php
Expand Up @@ -280,10 +280,12 @@ function testNewlinesInEntityValues() {
Line 2'
);
PHP;

$eol = PHP_EOL;
$this->assertEquals(
$c->collectFromCode($php, 'mymodule'),
array(
'Test.NEWLINESINGLEQUOTE' => array("Line 1\nLine 2",null,null)
'Test.NEWLINESINGLEQUOTE' => array("Line 1{$eol}Line 2",null,null)
)
);

Expand All @@ -297,7 +299,7 @@ function testNewlinesInEntityValues() {
$this->assertEquals(
$c->collectFromCode($php, 'mymodule'),
array(
'Test.NEWLINEDOUBLEQUOTE' => array("Line 1\nLine 2",null,null)
'Test.NEWLINEDOUBLEQUOTE' => array("Line 1{$eol}Line 2",null,null)
)
);
}
Expand All @@ -312,18 +314,18 @@ function testLangArrayCodeForEntity() {

$this->assertEquals(
$c->langArrayCodeForEntitySpec('Test.SIMPLE', array('Simple Value')),
"\$lang['{$locale}']['Test']['SIMPLE'] = 'Simple Value';\n"
"\$lang['{$locale}']['Test']['SIMPLE'] = 'Simple Value';" . PHP_EOL
);

$this->assertEquals(
// single quotes should be properly escaped by the parser already
$c->langArrayCodeForEntitySpec('Test.ESCAPEDSINGLEQUOTES', array("Value with \'Escaped Single Quotes\'")),
"\$lang['{$locale}']['Test']['ESCAPEDSINGLEQUOTES'] = 'Value with \'Escaped Single Quotes\'';\n"
"\$lang['{$locale}']['Test']['ESCAPEDSINGLEQUOTES'] = 'Value with \'Escaped Single Quotes\'';" . PHP_EOL
);

$this->assertEquals(
$c->langArrayCodeForEntitySpec('Test.DOUBLEQUOTES', array('Value with "Double Quotes"')),
"\$lang['{$locale}']['Test']['DOUBLEQUOTES'] = 'Value with \"Double Quotes\"';\n"
"\$lang['{$locale}']['Test']['DOUBLEQUOTES'] = 'Value with \"Double Quotes\"';" . PHP_EOL
);

$php = <<<PHP
Expand Down

0 comments on commit abb04b2

Please sign in to comment.