From b955cfe226fcf25794fb14c41d921b15d1206ceb Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Thu, 6 Nov 2014 14:30:38 -0500 Subject: [PATCH] Refactored some .ini code for future reuse. --- .../I18n/Translator/Loader/ExtendedIni.php | 66 ++++---------- .../Translator/Loader/ExtendedIniReader.php | 87 +++++++++++++++++++ .../Loader/ExtendedIniReaderTest.php | 59 +++++++++++++ 3 files changed, 163 insertions(+), 49 deletions(-) create mode 100644 module/VuFind/src/VuFind/I18n/Translator/Loader/ExtendedIniReader.php create mode 100644 module/VuFind/tests/unit-tests/src/VuFindTest/I18n/Translator/Loader/ExtendedIniReaderTest.php diff --git a/module/VuFind/src/VuFind/I18n/Translator/Loader/ExtendedIni.php b/module/VuFind/src/VuFind/I18n/Translator/Loader/ExtendedIni.php index ad9e7f6c0fd..78ea2fbcdf4 100644 --- a/module/VuFind/src/VuFind/I18n/Translator/Loader/ExtendedIni.php +++ b/module/VuFind/src/VuFind/I18n/Translator/Loader/ExtendedIni.php @@ -63,21 +63,32 @@ class ExtendedIni implements FileLoaderInterface */ protected $loadedFiles = array(); + /** + * Helper for reading .ini files from disk. + * + * @var ExtendedIniReader + */ + protected $reader; + /** * Constructor * - * @param array $pathStack List of directories to search for + * @param array $pathStack List of directories to search for * language files. - * @param string|string[] $fallbackLocales Fallback locale(s) to use for language - * strings missing from selected file. + * @param string|string[] $fallbackLocales Fallback locale(s) to use for + * language strings missing from selected file. + * @param ExtendedIniReader $reader Helper for reading .ini files from + * disk. */ - public function __construct($pathStack = array(), $fallbackLocales = null) - { + public function __construct($pathStack = array(), $fallbackLocales = null, + ExtendedIniReader $reader = null + ) { $this->pathStack = $pathStack; $this->fallbackLocales = $fallbackLocales; if (!empty($this->fallbackLocales) && !is_array($this->fallbackLocales)) { $this->fallbackLocales = array($this->fallbackLocales); } + $this->reader = ($reader === null) ? new ExtendedIniReader() : $reader; } /** @@ -153,7 +164,7 @@ class ExtendedIni implements FileLoaderInterface $data = false; foreach ($this->pathStack as $path) { if (file_exists($path . '/' . $filename)) { - $current = $this->languageFileToTextDomain($path . '/' . $filename); + $current = $this->reader->getTextDomain($path . '/' . $filename); if ($data === false) { $data = $current; } else { @@ -185,47 +196,4 @@ class ExtendedIni implements FileLoaderInterface $parent->merge($data); return $parent; } - - /** - * Parse a language file. - * - * @param string $file Filename to load - * - * @return TextDomain - */ - protected function languageFileToTextDomain($file) - { - $data = new TextDomain(); - - // Manually parse the language file: - $contents = file($file); - if (is_array($contents)) { - foreach ($contents as $current) { - // Split the string on the equals sign, keeping a max of two chunks: - $parts = explode('=', $current, 2); - $key = trim($parts[0]); - if ($key != "" && substr($key, 0, 1) != ';') { - // Trim outermost double quotes off the value if present: - if (isset($parts[1])) { - $value = preg_replace( - '/^\"?(.*?)\"?$/', '$1', trim($parts[1]) - ); - - // Store the key/value pair (allow empty values -- sometimes - // we want to replace a language token with a blank string, - // but Zend translator doesn't support them so replace with - // a zero-width non-joiner): - if ($value === '') { - $value = html_entity_decode( - '‌', ENT_NOQUOTES, 'UTF-8' - ); - } - $data[$key] = $value; - } - } - } - } - - return $data; - } } diff --git a/module/VuFind/src/VuFind/I18n/Translator/Loader/ExtendedIniReader.php b/module/VuFind/src/VuFind/I18n/Translator/Loader/ExtendedIniReader.php new file mode 100644 index 00000000000..9e246371412 --- /dev/null +++ b/module/VuFind/src/VuFind/I18n/Translator/Loader/ExtendedIniReader.php @@ -0,0 +1,87 @@ +<?php +/** + * Helper class to load .ini files from disk. + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Translator + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org Main Site + */ +namespace VuFind\I18n\Translator\Loader; +use Zend\I18n\Translator\TextDomain; + +/** + * Helper class to load .ini files from disk. + * + * @category VuFind2 + * @package Translator + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org Main Site + */ +class ExtendedIniReader +{ + /** + * Parse a language file. + * + * @param string|array $input Either a filename to read (passed as a + * string) or a set of data to convert into a TextDomain (passed as an array) + * @param bool $convertBlanks Should we convert blank strings to + * zero-width non-joiners? + * + * @return TextDomain + */ + public function getTextDomain($input, $convertBlanks = true) + { + $data = new TextDomain(); + + // Manually parse the language file: + $contents = is_array($input) ? $input : file($input); + if (is_array($contents)) { + foreach ($contents as $current) { + // Split the string on the equals sign, keeping a max of two chunks: + $parts = explode('=', $current, 2); + $key = trim($parts[0]); + if ($key != "" && substr($key, 0, 1) != ';') { + // Trim outermost double quotes off the value if present: + if (isset($parts[1])) { + $value = preg_replace( + '/^\"?(.*?)\"?$/', '$1', trim($parts[1]) + ); + + // Store the key/value pair (allow empty values -- sometimes + // we want to replace a language token with a blank string, + // but Zend translator doesn't support them so replace with + // a zero-width non-joiner): + if ($convertBlanks && $value === '') { + $value = html_entity_decode( + '‌', ENT_NOQUOTES, 'UTF-8' + ); + } + $data[$key] = $value; + } + } + } + } + + return $data; + } +} \ No newline at end of file diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/I18n/Translator/Loader/ExtendedIniReaderTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/I18n/Translator/Loader/ExtendedIniReaderTest.php new file mode 100644 index 00000000000..dc1e0fc0205 --- /dev/null +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/I18n/Translator/Loader/ExtendedIniReaderTest.php @@ -0,0 +1,59 @@ +<?php +/** + * ExtendedIniReader Test Class + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/vufind2:unit_tests Wiki + */ +namespace VuFindTest\I18n\Translator\Loader; +use VuFind\I18n\Translator\Loader\ExtendedIniReader; + +/** + * ExtendedIniReader Test Class + * + * @category VuFind2 + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @author Chris Hallberg <challber@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/vufind2:unit_tests Wiki + */ +class ExtendedIniReaderTest extends \VuFindTest\Unit\TestCase +{ + /** + * Test non-joiner functionality. + * + * @return void + */ + public function testNonJoinerOptions() + { + $reader = new ExtendedIniReader(); + $input = array('foo="bar"', 'baz=""'); + $output = array('foo' => 'bar', 'baz' => ''); + $nonJoiner = html_entity_decode('‌', ENT_NOQUOTES, 'UTF-8'); + $nonJoinerOutput = array('foo' => 'bar', 'baz' => $nonJoiner); + // Test behavior with and without the $convertBlanks switch: + $this->assertEquals($output, (array)$reader->getTextDomain($input, false)); + $this->assertEquals($nonJoinerOutput, (array)$reader->getTextDomain($input)); + } +} \ No newline at end of file -- GitLab