From 3694e5ec35a8be6283134f4542b0f3efc26208d3 Mon Sep 17 00:00:00 2001 From: Abdellatif Ait boudad Date: Wed, 15 Apr 2015 01:53:15 +0100 Subject: [PATCH] [Translation] added FileLoader. --- .../Translation/Loader/CsvFileLoader.php | 21 +------ .../Translation/Loader/FileLoader.php | 62 +++++++++++++++++++ .../Translation/Loader/IniFileLoader.php | 23 +------ .../Translation/Loader/JsonFileLoader.php | 23 +------ .../Translation/Loader/MoFileLoader.php | 40 +----------- .../Translation/Loader/PhpFileLoader.php | 25 +------- .../Translation/Loader/PoFileLoader.php | 40 +----------- .../Translation/Loader/YamlFileLoader.php | 31 +--------- 8 files changed, 83 insertions(+), 182 deletions(-) create mode 100644 src/Symfony/Component/Translation/Loader/FileLoader.php diff --git a/src/Symfony/Component/Translation/Loader/CsvFileLoader.php b/src/Symfony/Component/Translation/Loader/CsvFileLoader.php index ddcf595baf721..b6f0966371df1 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 @@ * * @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 @@ public function load($resource, $locale, $domain = 'messages') } } - $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 0000000000000..a55dc185aca8f --- /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 3f01ab4e9989e..11d9b272e0a39 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 8327c63b57f10..bd59659cf43a1 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 9cab3f0d8da78..2354c33860cb2 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 1cc9d06d9c994..88f4cdb113a08 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 @@ * * @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 8c8f1a297a29b..84664d65a0acc 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 @@ public function load($resource, $locale, $domain = 'messages') * * 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 e50e0fa385419..2a735f7989621 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 @@ * * @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 @@ public function load($resource, $locale, $domain = 'messages') 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; } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy