diff --git a/src/Symfony/Component/Translation/Loader/CsvFileLoader.php b/src/Symfony/Component/Translation/Loader/CsvFileLoader.php index ddcf595baf..b6f0966371 100644 --- a/src/Symfony/Component/Translation/Loader/CsvFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/CsvFileLoader.php @@ -11,9 +11,7 @@ namespace Symfony\Component\Translation\Loader; -use Symfony\Component\Translation\Exception\InvalidResourceException; use Symfony\Component\Translation\Exception\NotFoundResourceException; -use Symfony\Component\Config\Resource\FileResource; /** * CsvFileLoader loads translations from CSV files. @@ -22,7 +20,7 @@ use Symfony\Component\Config\Resource\FileResource; * * @api */ -class CsvFileLoader extends ArrayLoader +class CsvFileLoader extends FileLoader { private $delimiter = ';'; private $enclosure = '"'; @@ -30,19 +28,9 @@ class CsvFileLoader extends ArrayLoader /** * {@inheritdoc} - * - * @api */ - public function load($resource, $locale, $domain = 'messages') + protected function loadResource($resource) { - if (!stream_is_local($resource)) { - throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); - } - - if (!file_exists($resource)) { - throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); - } - $messages = array(); try { @@ -70,10 +58,7 @@ class CsvFileLoader extends ArrayLoader } } - $catalogue = parent::load($messages, $locale, $domain); - $catalogue->addResource(new FileResource($resource)); - - return $catalogue; + return $messages; } /** diff --git a/src/Symfony/Component/Translation/Loader/FileLoader.php b/src/Symfony/Component/Translation/Loader/FileLoader.php new file mode 100644 index 0000000000..a55dc185ac --- /dev/null +++ b/src/Symfony/Component/Translation/Loader/FileLoader.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Loader; + +use Symfony\Component\Translation\Exception\InvalidResourceException; +use Symfony\Component\Translation\Exception\NotFoundResourceException; +use Symfony\Component\Config\Resource\FileResource; + +/** + * @author Abdellatif Ait boudad + */ +abstract class FileLoader extends ArrayLoader +{ + /** + * {@inheritdoc} + */ + public function load($resource, $locale, $domain = 'messages') + { + if (!stream_is_local($resource)) { + throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); + } + + if (!file_exists($resource)) { + throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); + } + + $messages = $this->loadResource($resource); + + // empty resource + if (null === $messages) { + $messages = array(); + } + + // not an array + if (!is_array($messages)) { + throw new InvalidResourceException(sprintf('Unable to load file "%s".', $resource)); + } + + $catalogue = parent::load($messages, $locale, $domain); + $catalogue->addResource(new FileResource($resource)); + + return $catalogue; + } + + /* + * @param string $resource + * + * @return array + * + * @throws InvalidResourceException If stream content has an invalid format. + */ + abstract protected function loadResource($resource); +} diff --git a/src/Symfony/Component/Translation/Loader/IniFileLoader.php b/src/Symfony/Component/Translation/Loader/IniFileLoader.php index 3f01ab4e99..11d9b272e0 100644 --- a/src/Symfony/Component/Translation/Loader/IniFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/IniFileLoader.php @@ -11,35 +11,18 @@ namespace Symfony\Component\Translation\Loader; -use Symfony\Component\Translation\Exception\InvalidResourceException; -use Symfony\Component\Translation\Exception\NotFoundResourceException; -use Symfony\Component\Config\Resource\FileResource; - /** * IniFileLoader loads translations from an ini file. * * @author stealth35 */ -class IniFileLoader extends ArrayLoader +class IniFileLoader extends FileLoader { /** * {@inheritdoc} */ - public function load($resource, $locale, $domain = 'messages') + protected function loadResource($resource) { - if (!stream_is_local($resource)) { - throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); - } - - if (!file_exists($resource)) { - throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); - } - - $messages = parse_ini_file($resource, true); - - $catalogue = parent::load($messages, $locale, $domain); - $catalogue->addResource(new FileResource($resource)); - - return $catalogue; + return parse_ini_file($resource, true); } } diff --git a/src/Symfony/Component/Translation/Loader/JsonFileLoader.php b/src/Symfony/Component/Translation/Loader/JsonFileLoader.php index 8327c63b57..bd59659cf4 100644 --- a/src/Symfony/Component/Translation/Loader/JsonFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/JsonFileLoader.php @@ -12,43 +12,26 @@ namespace Symfony\Component\Translation\Loader; use Symfony\Component\Translation\Exception\InvalidResourceException; -use Symfony\Component\Translation\Exception\NotFoundResourceException; -use Symfony\Component\Config\Resource\FileResource; /** * JsonFileLoader loads translations from an json file. * * @author singles */ -class JsonFileLoader extends ArrayLoader implements LoaderInterface +class JsonFileLoader extends FileLoader { /** * {@inheritdoc} */ - public function load($resource, $locale, $domain = 'messages') + protected function loadResource($resource) { - if (!stream_is_local($resource)) { - throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); - } - - if (!file_exists($resource)) { - throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); - } - $messages = json_decode(file_get_contents($resource), true); if (0 < $errorCode = json_last_error()) { throw new InvalidResourceException(sprintf('Error parsing JSON - %s', $this->getJSONErrorMessage($errorCode))); } - if (null === $messages) { - $messages = array(); - } - - $catalogue = parent::load($messages, $locale, $domain); - $catalogue->addResource(new FileResource($resource)); - - return $catalogue; + return $messages; } /** diff --git a/src/Symfony/Component/Translation/Loader/MoFileLoader.php b/src/Symfony/Component/Translation/Loader/MoFileLoader.php index 9cab3f0d8d..2354c33860 100644 --- a/src/Symfony/Component/Translation/Loader/MoFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/MoFileLoader.php @@ -12,13 +12,11 @@ namespace Symfony\Component\Translation\Loader; use Symfony\Component\Translation\Exception\InvalidResourceException; -use Symfony\Component\Translation\Exception\NotFoundResourceException; -use Symfony\Component\Config\Resource\FileResource; /** * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/) */ -class MoFileLoader extends ArrayLoader +class MoFileLoader extends FileLoader { /** * Magic used for validating the format of a MO file as well as @@ -43,45 +41,13 @@ class MoFileLoader extends ArrayLoader */ const MO_HEADER_SIZE = 28; - public function load($resource, $locale, $domain = 'messages') - { - if (!stream_is_local($resource)) { - throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); - } - - if (!file_exists($resource)) { - throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); - } - - $messages = $this->parse($resource); - - // empty file - if (null === $messages) { - $messages = array(); - } - - // not an array - if (!is_array($messages)) { - throw new InvalidResourceException(sprintf('The file "%s" must contain a valid mo file.', $resource)); - } - - $catalogue = parent::load($messages, $locale, $domain); - $catalogue->addResource(new FileResource($resource)); - - return $catalogue; - } - /** * Parses machine object (MO) format, independent of the machine's endian it * was created on. Both 32bit and 64bit systems are supported. * - * @param resource $resource - * - * @return array - * - * @throws InvalidResourceException If stream content has an invalid format. + * {@inheritdoc} */ - private function parse($resource) + protected function loadResource($resource) { $stream = fopen($resource, 'r'); diff --git a/src/Symfony/Component/Translation/Loader/PhpFileLoader.php b/src/Symfony/Component/Translation/Loader/PhpFileLoader.php index 1cc9d06d9c..88f4cdb113 100644 --- a/src/Symfony/Component/Translation/Loader/PhpFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/PhpFileLoader.php @@ -11,10 +11,6 @@ namespace Symfony\Component\Translation\Loader; -use Symfony\Component\Translation\Exception\InvalidResourceException; -use Symfony\Component\Translation\Exception\NotFoundResourceException; -use Symfony\Component\Config\Resource\FileResource; - /** * PhpFileLoader loads translations from PHP files returning an array of translations. * @@ -22,28 +18,13 @@ use Symfony\Component\Config\Resource\FileResource; * * @api */ -class PhpFileLoader extends ArrayLoader +class PhpFileLoader extends FileLoader { /** * {@inheritdoc} - * - * @api */ - public function load($resource, $locale, $domain = 'messages') + protected function loadResource($resource) { - if (!stream_is_local($resource)) { - throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); - } - - if (!file_exists($resource)) { - throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); - } - - $messages = require $resource; - - $catalogue = parent::load($messages, $locale, $domain); - $catalogue->addResource(new FileResource($resource)); - - return $catalogue; + return require $resource; } } diff --git a/src/Symfony/Component/Translation/Loader/PoFileLoader.php b/src/Symfony/Component/Translation/Loader/PoFileLoader.php index 8c8f1a297a..84664d65a0 100644 --- a/src/Symfony/Component/Translation/Loader/PoFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/PoFileLoader.php @@ -11,44 +11,12 @@ namespace Symfony\Component\Translation\Loader; -use Symfony\Component\Translation\Exception\InvalidResourceException; -use Symfony\Component\Translation\Exception\NotFoundResourceException; -use Symfony\Component\Config\Resource\FileResource; - /** * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/) * @copyright Copyright (c) 2012, Clemens Tolboom */ -class PoFileLoader extends ArrayLoader +class PoFileLoader extends FileLoader { - public function load($resource, $locale, $domain = 'messages') - { - if (!stream_is_local($resource)) { - throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); - } - - if (!file_exists($resource)) { - throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); - } - - $messages = $this->parse($resource); - - // empty file - if (null === $messages) { - $messages = array(); - } - - // not an array - if (!is_array($messages)) { - throw new InvalidResourceException(sprintf('The file "%s" must contain a valid po file.', $resource)); - } - - $catalogue = parent::load($messages, $locale, $domain); - $catalogue->addResource(new FileResource($resource)); - - return $catalogue; - } - /** * Parses portable object (PO) format. * @@ -90,11 +58,9 @@ class PoFileLoader extends ArrayLoader * * Items with an empty id are ignored. * - * @param resource $resource - * - * @return array + * {@inheritdoc} */ - private function parse($resource) + protected function loadResource($resource) { $stream = fopen($resource, 'r'); diff --git a/src/Symfony/Component/Translation/Loader/YamlFileLoader.php b/src/Symfony/Component/Translation/Loader/YamlFileLoader.php index e50e0fa385..2a735f7989 100644 --- a/src/Symfony/Component/Translation/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/YamlFileLoader.php @@ -12,8 +12,6 @@ namespace Symfony\Component\Translation\Loader; use Symfony\Component\Translation\Exception\InvalidResourceException; -use Symfony\Component\Translation\Exception\NotFoundResourceException; -use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\Yaml\Parser as YamlParser; use Symfony\Component\Yaml\Exception\ParseException; @@ -24,25 +22,15 @@ use Symfony\Component\Yaml\Exception\ParseException; * * @api */ -class YamlFileLoader extends ArrayLoader +class YamlFileLoader extends FileLoader { private $yamlParser; /** * {@inheritdoc} - * - * @api */ - public function load($resource, $locale, $domain = 'messages') + protected function loadResource($resource) { - if (!stream_is_local($resource)) { - throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); - } - - if (!file_exists($resource)) { - throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); - } - if (null === $this->yamlParser) { $this->yamlParser = new YamlParser(); } @@ -53,19 +41,6 @@ class YamlFileLoader extends ArrayLoader throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s"', $resource), 0, $e); } - // empty file - if (null === $messages) { - $messages = array(); - } - - // not an array - if (!is_array($messages)) { - throw new InvalidResourceException(sprintf('The file "%s" must contain a YAML array.', $resource)); - } - - $catalogue = parent::load($messages, $locale, $domain); - $catalogue->addResource(new FileResource($resource)); - - return $catalogue; + return $messages; } }