merged branch fabpot/translator-loaders (PR #6308)
This PR was merged into the master branch.
Commits
-------
34d0c6b
[Translation] uniformized the way exception are thrown in LoaderInterface::load()
Discussion
----------
[Translation] uniformized the way exception are thrown in LoaderInterface::load()
---------------------------------------------------------------------------
by fabpot at 2012-12-12T19:44:36Z
followup for #6301 (cc @umpirsky)
---------------------------------------------------------------------------
by umpirsky at 2012-12-12T19:59:05Z
Does this means that we will catch this exception in `Translator` in order to fix #5797?
If yes, maybe we should add new `TranslationResourceNotFoundException` or something similar?
---------------------------------------------------------------------------
by fabpot at 2012-12-13T10:01:23Z
I've created two new exception classes: `NotFoundResourceException` for when the resource does not exist and `InvalidResourceException` when a problem occurs when loading the resource. That should give us everything we need to fix #5797.
---------------------------------------------------------------------------
by umpirsky at 2012-12-13T10:05:08Z
Perfect, thanks.
I'll give it a try later today.
---------------------------------------------------------------------------
by umpirsky at 2012-12-13T11:58:37Z
@fabpot Will you merge or I should fork your branch?
This commit is contained in:
commit
142d3389ef
@ -1,6 +1,15 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
2.2.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* [BC BREAK] uniformized the exception thrown by the load() method when an error occurs. The load() method now
|
||||||
|
throws Symfony\Component\Translation\Exception\InvalidResourceException when a resource cannot be found
|
||||||
|
and Symfony\Component\Translation\Exception\InvalidResourceException when a resource is invalid.
|
||||||
|
* changed the exception class thrown by some load() methods from \RuntimeException to \InvalidArgumentException
|
||||||
|
(IcuDatFileLoader, IcuResFileLoader, and QtTranslationsLoader)
|
||||||
|
|
||||||
2.1.0
|
2.1.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Translation\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception interface for all exceptions thrown by the component.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
interface ExceptionInterface
|
||||||
|
{
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Translation\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thrown when a resource cannot be loaded.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
class InvalidResourceException extends \InvalidArgumentException implements ExceptionInterface
|
||||||
|
{
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Translation\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thrown when a resource does not exist.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
class NotFoundResourceException extends \InvalidArgumentException implements ExceptionInterface
|
||||||
|
{
|
||||||
|
}
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Translation\Loader;
|
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\Config\Resource\FileResource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -33,16 +35,20 @@ class CsvFileLoader extends ArrayLoader implements LoaderInterface
|
|||||||
*/
|
*/
|
||||||
public function load($resource, $locale, $domain = 'messages')
|
public function load($resource, $locale, $domain = 'messages')
|
||||||
{
|
{
|
||||||
$messages = array();
|
|
||||||
|
|
||||||
if (!stream_is_local($resource)) {
|
if (!stream_is_local($resource)) {
|
||||||
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $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 {
|
try {
|
||||||
$file = new \SplFileObject($resource, 'rb');
|
$file = new \SplFileObject($resource, 'rb');
|
||||||
} catch (\RuntimeException $e) {
|
} catch (\RuntimeException $e) {
|
||||||
throw new \InvalidArgumentException(sprintf('Error opening file "%s".', $resource));
|
throw new NotFoundResourceException(sprintf('Error opening file "%s".', $resource), 0, $e);
|
||||||
}
|
}
|
||||||
|
|
||||||
$file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY);
|
$file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY);
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
namespace Symfony\Component\Translation\Loader;
|
namespace Symfony\Component\Translation\Loader;
|
||||||
|
|
||||||
use Symfony\Component\Translation\MessageCatalogue;
|
use Symfony\Component\Translation\MessageCatalogue;
|
||||||
|
use Symfony\Component\Translation\Exception\InvalidResourceException;
|
||||||
|
use Symfony\Component\Translation\Exception\NotFoundResourceException;
|
||||||
use Symfony\Component\Config\Resource\FileResource;
|
use Symfony\Component\Config\Resource\FileResource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,12 +28,20 @@ class IcuDatFileLoader extends IcuResFileLoader
|
|||||||
*/
|
*/
|
||||||
public function load($resource, $locale, $domain = 'messages')
|
public function load($resource, $locale, $domain = 'messages')
|
||||||
{
|
{
|
||||||
|
if (!stream_is_local($resource.'.dat')) {
|
||||||
|
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file_exists($resource.'.dat')) {
|
||||||
|
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
|
||||||
|
}
|
||||||
|
|
||||||
$rb = new \ResourceBundle($locale, $resource);
|
$rb = new \ResourceBundle($locale, $resource);
|
||||||
|
|
||||||
if (!$rb) {
|
if (!$rb) {
|
||||||
throw new \RuntimeException("cannot load this resource : $resource");
|
throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
|
||||||
} elseif (intl_is_failure($rb->getErrorCode())) {
|
} elseif (intl_is_failure($rb->getErrorCode())) {
|
||||||
throw new \RuntimeException($rb->getErrorMessage(), $rb->getErrorCode());
|
throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
$messages = $this->flatten($rb);
|
$messages = $this->flatten($rb);
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
namespace Symfony\Component\Translation\Loader;
|
namespace Symfony\Component\Translation\Loader;
|
||||||
|
|
||||||
use Symfony\Component\Translation\MessageCatalogue;
|
use Symfony\Component\Translation\MessageCatalogue;
|
||||||
|
use Symfony\Component\Translation\Exception\InvalidResourceException;
|
||||||
|
use Symfony\Component\Translation\Exception\NotFoundResourceException;
|
||||||
use Symfony\Component\Config\Resource\DirectoryResource;
|
use Symfony\Component\Config\Resource\DirectoryResource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,12 +28,20 @@ class IcuResFileLoader implements LoaderInterface
|
|||||||
*/
|
*/
|
||||||
public function load($resource, $locale, $domain = 'messages')
|
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 (!is_dir($resource)) {
|
||||||
|
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
|
||||||
|
}
|
||||||
|
|
||||||
$rb = new \ResourceBundle($locale, $resource);
|
$rb = new \ResourceBundle($locale, $resource);
|
||||||
|
|
||||||
if (!$rb) {
|
if (!$rb) {
|
||||||
throw new \RuntimeException("cannot load this resource : $resource");
|
throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
|
||||||
} elseif (intl_is_failure($rb->getErrorCode())) {
|
} elseif (intl_is_failure($rb->getErrorCode())) {
|
||||||
throw new \RuntimeException($rb->getErrorMessage(), $rb->getErrorCode());
|
throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
$messages = $this->flatten($rb);
|
$messages = $this->flatten($rb);
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Translation\Loader;
|
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\Config\Resource\FileResource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,8 +27,12 @@ class IniFileLoader extends ArrayLoader implements LoaderInterface
|
|||||||
*/
|
*/
|
||||||
public function load($resource, $locale, $domain = 'messages')
|
public function load($resource, $locale, $domain = 'messages')
|
||||||
{
|
{
|
||||||
if (!is_file($resource)) {
|
if (!stream_is_local($resource)) {
|
||||||
throw new \InvalidArgumentException(sprintf('Error opening file "%s".', $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);
|
$messages = parse_ini_file($resource, true);
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
namespace Symfony\Component\Translation\Loader;
|
namespace Symfony\Component\Translation\Loader;
|
||||||
|
|
||||||
use Symfony\Component\Translation\MessageCatalogue;
|
use Symfony\Component\Translation\MessageCatalogue;
|
||||||
|
use Symfony\Component\Translation\Exception\InvalidResourceException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* LoaderInterface is the interface implemented by all translation loaders.
|
* LoaderInterface is the interface implemented by all translation loaders.
|
||||||
@ -32,6 +33,9 @@ interface LoaderInterface
|
|||||||
* @return MessageCatalogue A MessageCatalogue instance
|
* @return MessageCatalogue A MessageCatalogue instance
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
|
*
|
||||||
|
* @throws NotFoundResourceException when the resource cannot be found
|
||||||
|
* @throws InvalidResourceException when the resource cannot be loaded
|
||||||
*/
|
*/
|
||||||
public function load($resource, $locale, $domain = 'messages');
|
public function load($resource, $locale, $domain = 'messages');
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Translation\Loader;
|
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\Config\Resource\FileResource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -43,8 +45,12 @@ class MoFileLoader extends ArrayLoader implements LoaderInterface
|
|||||||
|
|
||||||
public function load($resource, $locale, $domain = 'messages')
|
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)) {
|
if (!file_exists($resource)) {
|
||||||
throw new \InvalidArgumentException(sprintf('File "%s" not found.', $resource));
|
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
|
||||||
}
|
}
|
||||||
|
|
||||||
$messages = $this->parse($resource);
|
$messages = $this->parse($resource);
|
||||||
@ -56,7 +62,7 @@ class MoFileLoader extends ArrayLoader implements LoaderInterface
|
|||||||
|
|
||||||
// not an array
|
// not an array
|
||||||
if (!is_array($messages)) {
|
if (!is_array($messages)) {
|
||||||
throw new \InvalidArgumentException(sprintf('The file "%s" must contain a valid mo file.', $resource));
|
throw new InvalidResourceException(sprintf('The file "%s" must contain a valid mo file.', $resource));
|
||||||
}
|
}
|
||||||
|
|
||||||
$catalogue = parent::load($messages, $locale, $domain);
|
$catalogue = parent::load($messages, $locale, $domain);
|
||||||
@ -72,7 +78,7 @@ class MoFileLoader extends ArrayLoader implements LoaderInterface
|
|||||||
* @param resource $resource
|
* @param resource $resource
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* @throws \InvalidArgumentException If stream content has an invalid format.
|
* @throws InvalidResourceException If stream content has an invalid format.
|
||||||
*/
|
*/
|
||||||
private function parse($resource)
|
private function parse($resource)
|
||||||
{
|
{
|
||||||
@ -81,7 +87,7 @@ class MoFileLoader extends ArrayLoader implements LoaderInterface
|
|||||||
$stat = fstat($stream);
|
$stat = fstat($stream);
|
||||||
|
|
||||||
if ($stat['size'] < self::MO_HEADER_SIZE) {
|
if ($stat['size'] < self::MO_HEADER_SIZE) {
|
||||||
throw new \InvalidArgumentException("MO stream content has an invalid format.");
|
throw new InvalidResourceException("MO stream content has an invalid format.");
|
||||||
}
|
}
|
||||||
$magic = unpack('V1', fread($stream, 4));
|
$magic = unpack('V1', fread($stream, 4));
|
||||||
$magic = hexdec(substr(dechex(current($magic)), -8));
|
$magic = hexdec(substr(dechex(current($magic)), -8));
|
||||||
@ -91,7 +97,7 @@ class MoFileLoader extends ArrayLoader implements LoaderInterface
|
|||||||
} elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) {
|
} elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) {
|
||||||
$isBigEndian = true;
|
$isBigEndian = true;
|
||||||
} else {
|
} else {
|
||||||
throw new \InvalidArgumentException("MO stream content has an invalid format.");
|
throw new InvalidResourceException("MO stream content has an invalid format.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$formatRevision = $this->readLong($stream, $isBigEndian);
|
$formatRevision = $this->readLong($stream, $isBigEndian);
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Translation\Loader;
|
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\Config\Resource\FileResource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,12 +31,12 @@ class PhpFileLoader extends ArrayLoader implements LoaderInterface
|
|||||||
*/
|
*/
|
||||||
public function load($resource, $locale, $domain = 'messages')
|
public function load($resource, $locale, $domain = 'messages')
|
||||||
{
|
{
|
||||||
if (!file_exists($resource)) {
|
if (!stream_is_local($resource)) {
|
||||||
throw new \InvalidArgumentException(sprintf('File "%s" not found.', $resource));
|
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stream_is_local($resource)) {
|
if (!file_exists($resource)) {
|
||||||
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $resource));
|
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
|
||||||
}
|
}
|
||||||
|
|
||||||
$messages = require($resource);
|
$messages = require($resource);
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Translation\Loader;
|
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\Config\Resource\FileResource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,8 +23,12 @@ class PoFileLoader extends ArrayLoader implements LoaderInterface
|
|||||||
{
|
{
|
||||||
public function load($resource, $locale, $domain = 'messages')
|
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)) {
|
if (!file_exists($resource)) {
|
||||||
throw new \InvalidArgumentException(sprintf('File "%s" not found.', $resource));
|
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
|
||||||
}
|
}
|
||||||
|
|
||||||
$messages = $this->parse($resource);
|
$messages = $this->parse($resource);
|
||||||
@ -34,7 +40,7 @@ class PoFileLoader extends ArrayLoader implements LoaderInterface
|
|||||||
|
|
||||||
// not an array
|
// not an array
|
||||||
if (!is_array($messages)) {
|
if (!is_array($messages)) {
|
||||||
throw new \InvalidArgumentException(sprintf('The file "%s" must contain a valid po file.', $resource));
|
throw new InvalidResourceException(sprintf('The file "%s" must contain a valid po file.', $resource));
|
||||||
}
|
}
|
||||||
|
|
||||||
$catalogue = parent::load($messages, $locale, $domain);
|
$catalogue = parent::load($messages, $locale, $domain);
|
||||||
|
@ -11,8 +11,10 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Translation\Loader;
|
namespace Symfony\Component\Translation\Loader;
|
||||||
|
|
||||||
use Symfony\Component\Config\Resource\FileResource;
|
|
||||||
use Symfony\Component\Translation\MessageCatalogue;
|
use Symfony\Component\Translation\MessageCatalogue;
|
||||||
|
use Symfony\Component\Translation\Exception\InvalidResourceException;
|
||||||
|
use Symfony\Component\Translation\Exception\NotFoundResourceException;
|
||||||
|
use Symfony\Component\Config\Resource\FileResource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* QtTranslationsLoader loads translations from QT Translations XML files.
|
* QtTranslationsLoader loads translations from QT Translations XML files.
|
||||||
@ -30,10 +32,18 @@ class QtTranslationsLoader implements LoaderInterface
|
|||||||
*/
|
*/
|
||||||
public function load($resource, $locale, $domain = 'messages')
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
$dom = new \DOMDocument();
|
$dom = new \DOMDocument();
|
||||||
$current = libxml_use_internal_errors(true);
|
$current = libxml_use_internal_errors(true);
|
||||||
if (!@$dom->load($resource, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) {
|
if (!@$dom->load($resource, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) {
|
||||||
throw new \RuntimeException(implode("\n", $this->getXmlErrors()));
|
throw new InvalidResourceException(implode("\n", $this->getXmlErrors()));
|
||||||
}
|
}
|
||||||
|
|
||||||
$xpath = new \DOMXPath($dom);
|
$xpath = new \DOMXPath($dom);
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
namespace Symfony\Component\Translation\Loader;
|
namespace Symfony\Component\Translation\Loader;
|
||||||
|
|
||||||
use Symfony\Component\Translation\MessageCatalogue;
|
use Symfony\Component\Translation\MessageCatalogue;
|
||||||
|
use Symfony\Component\Translation\Exception\InvalidResourceException;
|
||||||
|
use Symfony\Component\Translation\Exception\NotFoundResourceException;
|
||||||
use Symfony\Component\Config\Resource\FileResource;
|
use Symfony\Component\Config\Resource\FileResource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,12 +32,12 @@ class XliffFileLoader implements LoaderInterface
|
|||||||
*/
|
*/
|
||||||
public function load($resource, $locale, $domain = 'messages')
|
public function load($resource, $locale, $domain = 'messages')
|
||||||
{
|
{
|
||||||
if (!file_exists($resource)) {
|
if (!stream_is_local($resource)) {
|
||||||
throw new \InvalidArgumentException(sprintf('File "%s" not found.', $resource));
|
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stream_is_local($resource)) {
|
if (!file_exists($resource)) {
|
||||||
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $resource));
|
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
|
||||||
}
|
}
|
||||||
|
|
||||||
$xml = $this->parseFile($resource);
|
$xml = $this->parseFile($resource);
|
||||||
@ -71,7 +73,7 @@ class XliffFileLoader implements LoaderInterface
|
|||||||
if (!@$dom->loadXML(file_get_contents($file), LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) {
|
if (!@$dom->loadXML(file_get_contents($file), LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) {
|
||||||
libxml_disable_entity_loader($disableEntities);
|
libxml_disable_entity_loader($disableEntities);
|
||||||
|
|
||||||
throw new \RuntimeException(implode("\n", $this->getXmlErrors($internalErrors)));
|
throw new InvalidResourceException(implode("\n", $this->getXmlErrors($internalErrors)));
|
||||||
}
|
}
|
||||||
|
|
||||||
libxml_disable_entity_loader($disableEntities);
|
libxml_disable_entity_loader($disableEntities);
|
||||||
@ -80,7 +82,7 @@ class XliffFileLoader implements LoaderInterface
|
|||||||
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
|
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
|
||||||
libxml_use_internal_errors($internalErrors);
|
libxml_use_internal_errors($internalErrors);
|
||||||
|
|
||||||
throw new \RuntimeException('Document types are not allowed.');
|
throw new InvalidResourceException('Document types are not allowed.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,7 +102,7 @@ class XliffFileLoader implements LoaderInterface
|
|||||||
$source = str_replace('http://www.w3.org/2001/xml.xsd', $location, $source);
|
$source = str_replace('http://www.w3.org/2001/xml.xsd', $location, $source);
|
||||||
|
|
||||||
if (!@$dom->schemaValidateSource($source)) {
|
if (!@$dom->schemaValidateSource($source)) {
|
||||||
throw new \RuntimeException(implode("\n", $this->getXmlErrors($internalErrors)));
|
throw new InvalidResourceException(implode("\n", $this->getXmlErrors($internalErrors)));
|
||||||
}
|
}
|
||||||
|
|
||||||
$dom->normalizeDocument();
|
$dom->normalizeDocument();
|
||||||
|
@ -11,8 +11,11 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Translation\Loader;
|
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\Config\Resource\FileResource;
|
||||||
use Symfony\Component\Yaml\Yaml;
|
use Symfony\Component\Yaml\Yaml;
|
||||||
|
use Symfony\Component\Yaml\Exception\ParseException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* YamlFileLoader loads translations from Yaml files.
|
* YamlFileLoader loads translations from Yaml files.
|
||||||
@ -30,7 +33,19 @@ class YamlFileLoader extends ArrayLoader implements LoaderInterface
|
|||||||
*/
|
*/
|
||||||
public function load($resource, $locale, $domain = 'messages')
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
$messages = Yaml::parse($resource);
|
$messages = Yaml::parse($resource);
|
||||||
|
} catch (ParseException $e) {
|
||||||
|
throw new InvalidResourceException('Error parsing YAML.', 0, $e);
|
||||||
|
}
|
||||||
|
|
||||||
// empty file
|
// empty file
|
||||||
if (null === $messages) {
|
if (null === $messages) {
|
||||||
@ -39,7 +54,7 @@ class YamlFileLoader extends ArrayLoader implements LoaderInterface
|
|||||||
|
|
||||||
// not an array
|
// not an array
|
||||||
if (!is_array($messages)) {
|
if (!is_array($messages)) {
|
||||||
throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $resource));
|
throw new InvalidResourceException(sprintf('The file "%s" must contain a YAML array.', $resource));
|
||||||
}
|
}
|
||||||
|
|
||||||
$catalogue = parent::load($messages, $locale, $domain);
|
$catalogue = parent::load($messages, $locale, $domain);
|
||||||
|
@ -46,7 +46,7 @@ class CsvFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadNonExistingResource()
|
public function testLoadNonExistingResource()
|
||||||
{
|
{
|
||||||
@ -56,7 +56,7 @@ class CsvFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException Symfony\Component\Translation\Exception\InvalidResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadNonLocalResource()
|
public function testLoadNonLocalResource()
|
||||||
{
|
{
|
||||||
|
@ -25,12 +25,20 @@ class IcuDatFileLoaderTest extends LocalizedTestCase
|
|||||||
if (!extension_loaded('intl')) {
|
if (!extension_loaded('intl')) {
|
||||||
$this->markTestSkipped('This test requires intl extension to work.');
|
$this->markTestSkipped('This test requires intl extension to work.');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Symfony\Component\Translation\Exception\InvalidResourceException
|
||||||
|
*/
|
||||||
|
public function testLoadInvalidResource()
|
||||||
|
{
|
||||||
|
$loader = new IcuDatFileLoader();
|
||||||
|
$loader->load(__DIR__.'/../fixtures/resourcebundle/corrupted/resources', 'es', 'domain2');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDatEnglishLoad()
|
public function testDatEnglishLoad()
|
||||||
{
|
{
|
||||||
// bundled resource is build using pkgdata command which at leas in ICU 4.2 comes in extremely! buggy form
|
// bundled resource is build using pkgdata command which at least in ICU 4.2 comes in extremely! buggy form
|
||||||
// you must specify an temporary build directory which is not the same as current directory and
|
// you must specify an temporary build directory which is not the same as current directory and
|
||||||
// MUST reside on the same partition. pkgdata -p resources -T /srv -d . packagelist.txt
|
// MUST reside on the same partition. pkgdata -p resources -T /srv -d . packagelist.txt
|
||||||
$loader = new IcuDatFileLoader();
|
$loader = new IcuDatFileLoader();
|
||||||
@ -54,21 +62,11 @@ class IcuDatFileLoaderTest extends LocalizedTestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \RuntimeException
|
* @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadNonExistingResource()
|
public function testLoadNonExistingResource()
|
||||||
{
|
{
|
||||||
$loader = new IcuDatFileLoader();
|
$loader = new IcuDatFileLoader();
|
||||||
$resource = __DIR__.'/../fixtures/non-existing.txt';
|
$loader->load(__DIR__.'/../fixtures/non-existing.txt', 'en', 'domain1');
|
||||||
$loader->load($resource, 'en', 'domain1');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException \RuntimeException
|
|
||||||
*/
|
|
||||||
public function testLoadInvalidResource()
|
|
||||||
{
|
|
||||||
$loader = new IcuDatFileLoader();
|
|
||||||
$loader->load(__DIR__.'/../fixtures/resourcebundle/res/en.txt', 'en', 'domain1');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,6 @@ class IcuResFileLoaderTest extends LocalizedTestCase
|
|||||||
if (!extension_loaded('intl')) {
|
if (!extension_loaded('intl')) {
|
||||||
$this->markTestSkipped('This test requires intl extension to work.');
|
$this->markTestSkipped('This test requires intl extension to work.');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testLoad()
|
public function testLoad()
|
||||||
@ -41,21 +40,20 @@ class IcuResFileLoaderTest extends LocalizedTestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \RuntimeException
|
* @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadNonExistingResource()
|
public function testLoadNonExistingResource()
|
||||||
{
|
{
|
||||||
$loader = new IcuResFileLoader();
|
$loader = new IcuResFileLoader();
|
||||||
$resource = __DIR__.'/../fixtures/non-existing.txt';
|
$loader->load(__DIR__.'/../fixtures/non-existing.txt', 'en', 'domain1');
|
||||||
$loader->load($resource, 'en', 'domain1');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \RuntimeException
|
* @expectedException Symfony\Component\Translation\Exception\InvalidResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadInvalidResource()
|
public function testLoadInvalidResource()
|
||||||
{
|
{
|
||||||
$loader = new IcuResFileLoader();
|
$loader = new IcuResFileLoader();
|
||||||
$loader->load(__DIR__.'/../fixtures/resourcebundle/res/en.txt', 'en', 'domain1');
|
$loader->load(__DIR__.'/../fixtures/resourcebundle/corrupted', 'en', 'domain1');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ class IniFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadNonExistingResource()
|
public function testLoadNonExistingResource()
|
||||||
{
|
{
|
||||||
@ -54,14 +54,4 @@ class IniFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
$resource = __DIR__.'/../fixtures/non-existing.ini';
|
$resource = __DIR__.'/../fixtures/non-existing.ini';
|
||||||
$loader->load($resource, 'en', 'domain1');
|
$loader->load($resource, 'en', 'domain1');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException \InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function testLoadThrowsAnExceptionIfFileNotExists()
|
|
||||||
{
|
|
||||||
$loader = new IniFileLoader();
|
|
||||||
$resource = __DIR__.'/../fixtures/not-exists.ini';
|
|
||||||
$loader->load($resource, 'en', 'domain1');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ class MoFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadNonExistingResource()
|
public function testLoadNonExistingResource()
|
||||||
{
|
{
|
||||||
@ -56,7 +56,7 @@ class MoFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException Symfony\Component\Translation\Exception\InvalidResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadInvalidResource()
|
public function testLoadInvalidResource()
|
||||||
{
|
{
|
||||||
|
@ -35,7 +35,7 @@ class PhpFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadNonExistingResource()
|
public function testLoadNonExistingResource()
|
||||||
{
|
{
|
||||||
@ -45,7 +45,7 @@ class PhpFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException Symfony\Component\Translation\Exception\InvalidResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadThrowsAnExceptionIfFileNotLocal()
|
public function testLoadThrowsAnExceptionIfFileNotLocal()
|
||||||
{
|
{
|
||||||
|
@ -57,7 +57,7 @@ class PoFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadNonExistingResource()
|
public function testLoadNonExistingResource()
|
||||||
{
|
{
|
||||||
|
@ -35,7 +35,7 @@ class QtTranslationsLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \RuntimeException
|
* @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadNonExistingResource()
|
public function testLoadNonExistingResource()
|
||||||
{
|
{
|
||||||
|
@ -43,7 +43,7 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \RuntimeException
|
* @expectedException Symfony\Component\Translation\Exception\InvalidResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadInvalidResource()
|
public function testLoadInvalidResource()
|
||||||
{
|
{
|
||||||
@ -52,7 +52,7 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \RuntimeException
|
* @expectedException Symfony\Component\Translation\Exception\InvalidResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadResourceDoesNotValidate()
|
public function testLoadResourceDoesNotValidate()
|
||||||
{
|
{
|
||||||
@ -61,7 +61,7 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadNonExistingResource()
|
public function testLoadNonExistingResource()
|
||||||
{
|
{
|
||||||
@ -71,7 +71,7 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException Symfony\Component\Translation\Exception\InvalidResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadThrowsAnExceptionIfFileNotLocal()
|
public function testLoadThrowsAnExceptionIfFileNotLocal()
|
||||||
{
|
{
|
||||||
@ -81,7 +81,7 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \RuntimeException
|
* @expectedException Symfony\Component\Translation\Exception\InvalidResourceException
|
||||||
* @expectedExceptionMessage Document types are not allowed.
|
* @expectedExceptionMessage Document types are not allowed.
|
||||||
*/
|
*/
|
||||||
public function testDocTypeIsNotAllowed()
|
public function testDocTypeIsNotAllowed()
|
||||||
|
@ -50,7 +50,7 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadNonExistingResource()
|
public function testLoadNonExistingResource()
|
||||||
{
|
{
|
||||||
@ -60,7 +60,17 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException Symfony\Component\Translation\Exception\InvalidResourceException
|
||||||
|
*/
|
||||||
|
public function testLoadThrowsAnExceptionIfFileNotLocal()
|
||||||
|
{
|
||||||
|
$loader = new YamlFileLoader();
|
||||||
|
$resource = 'http://example.com/resources.yml';
|
||||||
|
$loader->load($resource, 'en', 'domain1');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Symfony\Component\Translation\Exception\InvalidResourceException
|
||||||
*/
|
*/
|
||||||
public function testLoadThrowsAnExceptionIfNotAnArray()
|
public function testLoadThrowsAnExceptionIfNotAnArray()
|
||||||
{
|
{
|
||||||
|
1
src/Symfony/Component/Translation/Tests/fixtures/resourcebundle/corrupted/resources.dat
vendored
Normal file
1
src/Symfony/Component/Translation/Tests/fixtures/resourcebundle/corrupted/resources.dat
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
XXX
|
@ -1,3 +0,0 @@
|
|||||||
en {
|
|
||||||
foo { "bar" }
|
|
||||||
}
|
|
Reference in New Issue
Block a user