Permalink
Browse files

[Translation] uniformized the way exception are thrown in LoaderInter…

…face::load()
  • Loading branch information...
1 parent 92a6f43 commit 534f46b57f677055ac3e3b2ebb7d7ab1846c3111 @fabpot fabpot committed Dec 12, 2012
View
@@ -1,6 +1,15 @@
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
-----
@@ -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
+{
+}
View
@@ -11,6 +11,8 @@
namespace Symfony\Component\Translation\Loader;
+use Symfony\Component\Translation\Exception\InvalidResourceException;
+use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
/**
@@ -33,16 +35,20 @@ class CsvFileLoader extends ArrayLoader implements LoaderInterface
*/
public function load($resource, $locale, $domain = 'messages')
{
- $messages = array();
-
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 {
$file = new \SplFileObject($resource, 'rb');
} 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);
@@ -12,6 +12,8 @@
namespace Symfony\Component\Translation\Loader;
use Symfony\Component\Translation\MessageCatalogue;
+use Symfony\Component\Translation\Exception\InvalidResourceException;
+use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
/**
@@ -26,12 +28,20 @@ class IcuDatFileLoader extends IcuResFileLoader
*/
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);
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())) {
- throw new \RuntimeException($rb->getErrorMessage(), $rb->getErrorCode());
+ throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
}
$messages = $this->flatten($rb);
@@ -12,6 +12,8 @@
namespace Symfony\Component\Translation\Loader;
use Symfony\Component\Translation\MessageCatalogue;
+use Symfony\Component\Translation\Exception\InvalidResourceException;
+use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\DirectoryResource;
/**
@@ -26,12 +28,20 @@ class IcuResFileLoader implements LoaderInterface
*/
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);
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())) {
- throw new \RuntimeException($rb->getErrorMessage(), $rb->getErrorCode());
+ throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
}
$messages = $this->flatten($rb);
View
@@ -11,6 +11,8 @@
namespace Symfony\Component\Translation\Loader;
+use Symfony\Component\Translation\Exception\InvalidResourceException;
+use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
/**
@@ -25,8 +27,12 @@ class IniFileLoader extends ArrayLoader implements LoaderInterface
*/
public function load($resource, $locale, $domain = 'messages')
{
- if (!is_file($resource)) {
- throw new \InvalidArgumentException(sprintf('Error opening file "%s".', $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);
@@ -12,6 +12,7 @@
namespace Symfony\Component\Translation\Loader;
use Symfony\Component\Translation\MessageCatalogue;
+use Symfony\Component\Translation\Exception\InvalidResourceException;
/**
* LoaderInterface is the interface implemented by all translation loaders.
@@ -32,6 +33,9 @@
* @return MessageCatalogue A MessageCatalogue instance
*
* @api
+ *
+ * @throws NotFoundResourceException when the resource cannot be found
+ * @throws InvalidResourceException when the resource cannot be loaded
*/
public function load($resource, $locale, $domain = 'messages');
}
View
@@ -11,6 +11,8 @@
namespace Symfony\Component\Translation\Loader;
+use Symfony\Component\Translation\Exception\InvalidResourceException;
+use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
/**
@@ -43,8 +45,12 @@ class MoFileLoader extends ArrayLoader implements LoaderInterface
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 \InvalidArgumentException(sprintf('File "%s" not found.', $resource));
+ throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
$messages = $this->parse($resource);
@@ -56,7 +62,7 @@ public function load($resource, $locale, $domain = 'messages')
// not an array
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);
@@ -72,7 +78,7 @@ public function load($resource, $locale, $domain = 'messages')
* @param resource $resource
*
* @return array
- * @throws \InvalidArgumentException If stream content has an invalid format.
+ * @throws InvalidResourceException If stream content has an invalid format.
*/
private function parse($resource)
{
@@ -81,7 +87,7 @@ private function parse($resource)
$stat = fstat($stream);
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 = hexdec(substr(dechex(current($magic)), -8));
@@ -91,7 +97,7 @@ private function parse($resource)
} elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) {
$isBigEndian = true;
} 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);
View
@@ -11,6 +11,8 @@
namespace Symfony\Component\Translation\Loader;
+use Symfony\Component\Translation\Exception\InvalidResourceException;
+use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
/**
@@ -29,12 +31,12 @@ class PhpFileLoader extends ArrayLoader implements LoaderInterface
*/
public function load($resource, $locale, $domain = 'messages')
{
- if (!file_exists($resource)) {
- throw new \InvalidArgumentException(sprintf('File "%s" not found.', $resource));
+ if (!stream_is_local($resource)) {
+ throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
- if (!stream_is_local($resource)) {
- throw new \InvalidArgumentException(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);
View
@@ -11,6 +11,8 @@
namespace Symfony\Component\Translation\Loader;
+use Symfony\Component\Translation\Exception\InvalidResourceException;
+use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
/**
@@ -21,8 +23,12 @@ class PoFileLoader extends ArrayLoader implements LoaderInterface
{
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 \InvalidArgumentException(sprintf('File "%s" not found.', $resource));
+ throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
$messages = $this->parse($resource);
@@ -34,7 +40,7 @@ public function load($resource, $locale, $domain = 'messages')
// not an array
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);
@@ -11,8 +11,10 @@
namespace Symfony\Component\Translation\Loader;
-use Symfony\Component\Config\Resource\FileResource;
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.
@@ -30,10 +32,18 @@ class QtTranslationsLoader implements LoaderInterface
*/
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();
$current = libxml_use_internal_errors(true);
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);
Oops, something went wrong.

0 comments on commit 534f46b

Please sign in to comment.