BUGFIX Fixed newlines working properly across different platforms - Windows, 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

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@92220 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2009-11-19 09:39:05 +00:00 committed by Sam Minnee
parent 047b3deb41
commit 71fb665ed3
4 changed files with 19 additions and 12 deletions

View File

@ -298,7 +298,8 @@ class i18nTextCollector extends Object {
// remove starting comma and any newlines // 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 // remove wrapping quotes
$comment = ($regs[12]) ? substr($regs[12],1,-1) : null; $comment = ($regs[12]) ? substr($regs[12],1,-1) : null;
@ -320,6 +321,7 @@ class i18nTextCollector extends Object {
*/ */
public function langArrayCodeForEntitySpec($entityFullName, $entitySpec) { public function langArrayCodeForEntitySpec($entityFullName, $entitySpec) {
$php = ''; $php = '';
$eol = PHP_EOL;
$entityParts = explode('.', $entityFullName); $entityParts = explode('.', $entityFullName);
if(count($entityParts) > 1) { if(count($entityParts) > 1) {
@ -338,15 +340,15 @@ class i18nTextCollector extends Object {
$php .= '$lang[\'' . $this->defaultLocale . '\'][\'' . $namespace . '\'][\'' . $entity . '\'] = '; $php .= '$lang[\'' . $this->defaultLocale . '\'][\'' . $namespace . '\'][\'' . $entity . '\'] = ';
if ($prio) { if ($prio) {
$php .= "array(\n\t'" . $value . "',\n\t" . $prio; $php .= "array($eol\t'" . $value . "',$eol\t" . $prio;
if ($comment) { if ($comment) {
$php .= ",\n\t'" . $comment . '\''; $php .= ",$eol\t'" . $comment . '\'';
} }
$php .= "\n);"; $php .= "$eol);";
} else { } else {
$php .= '\'' . $value . '\';'; $php .= '\'' . $value . '\';';
} }
$php .= "\n"; $php .= "$eol";
return $php; return $php;
} }
@ -358,6 +360,7 @@ class i18nTextCollector extends Object {
// Write each module language file // Write each module language file
if($entitiesByModule) foreach($entitiesByModule as $module => $entities) { if($entitiesByModule) foreach($entitiesByModule as $module => $entities) {
$php = ''; $php = '';
$eol = PHP_EOL;
// Create folder for lang files // Create folder for lang files
$langFolder = $this->baseSavePath . '/' . $module . '/lang'; $langFolder = $this->baseSavePath . '/' . $module . '/lang';
@ -380,7 +383,7 @@ class i18nTextCollector extends Object {
user_error('i18nTextCollector->writeMasterStringFile(): Invalid PHP language file. Error: ' . $e->toString(), E_USER_ERROR); 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); fclose($fh);
//Debug::message("Created file: $langFolder/" . $this->defaultLocale . ".php", false); //Debug::message("Created file: $langFolder/" . $this->defaultLocale . ".php", false);

View File

@ -45,8 +45,9 @@ class SQLFormatter extends Object {
* messing with possible content fragments in the query. * messing with possible content fragments in the query.
*/ */
protected function addNewlines($sql, $useHtmlFormatting = false) { protected function addNewlines($sql, $useHtmlFormatting = false) {
$eol = PHP_EOL;
foreach(self::$newline_before_tokens as $token) { 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); $sql = preg_replace('/[^\n](' . $token . ')/', $breakToken . '$1', $sql);
} }

View File

@ -16,6 +16,7 @@ SELECT Test.Foo, Test.Bar
FROM Test FROM Test
WHERE 'From' = "Where" WHERE 'From' = "Where"
SQL; SQL;
$this->assertEquals($formatter->formatPlain($sqlBefore), $sqlAfter, $this->assertEquals($formatter->formatPlain($sqlBefore), $sqlAfter,
'correct replacement of newlines and don\'t replace non-uppercase tokens' 'correct replacement of newlines and don\'t replace non-uppercase tokens'
); );

View File

@ -280,10 +280,12 @@ _t(
Line 2' Line 2'
); );
PHP; PHP;
$eol = PHP_EOL;
$this->assertEquals( $this->assertEquals(
$c->collectFromCode($php, 'mymodule'), $c->collectFromCode($php, 'mymodule'),
array( array(
'Test.NEWLINESINGLEQUOTE' => array("Line 1\nLine 2",null,null) 'Test.NEWLINESINGLEQUOTE' => array("Line 1{$eol}Line 2",null,null)
) )
); );
@ -297,7 +299,7 @@ PHP;
$this->assertEquals( $this->assertEquals(
$c->collectFromCode($php, 'mymodule'), $c->collectFromCode($php, 'mymodule'),
array( array(
'Test.NEWLINEDOUBLEQUOTE' => array("Line 1\nLine 2",null,null) 'Test.NEWLINEDOUBLEQUOTE' => array("Line 1{$eol}Line 2",null,null)
) )
); );
} }
@ -312,18 +314,18 @@ PHP;
$this->assertEquals( $this->assertEquals(
$c->langArrayCodeForEntitySpec('Test.SIMPLE', array('Simple Value')), $c->langArrayCodeForEntitySpec('Test.SIMPLE', array('Simple Value')),
"\$lang['{$locale}']['Test']['SIMPLE'] = 'Simple Value';\n" "\$lang['{$locale}']['Test']['SIMPLE'] = 'Simple Value';" . PHP_EOL
); );
$this->assertEquals( $this->assertEquals(
// single quotes should be properly escaped by the parser already // single quotes should be properly escaped by the parser already
$c->langArrayCodeForEntitySpec('Test.ESCAPEDSINGLEQUOTES', array("Value with \'Escaped Single Quotes\'")), $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( $this->assertEquals(
$c->langArrayCodeForEntitySpec('Test.DOUBLEQUOTES', array('Value with "Double Quotes"')), $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 $php = <<<PHP