Skip to content

Commit

Permalink
feature #20012 [Translation] added Base Exception for the component. …
Browse files Browse the repository at this point in the history
…(aitboudad)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[Translation] added Base Exception for the component.

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | ~
| License       | MIT
| Doc PR        | ~

Commits
-------

0fa4397 [Translation] added Base Exception for the component.
  • Loading branch information
fabpot committed Sep 21, 2016
2 parents 3cf664f + 0fa4397 commit 7f1f232
Show file tree
Hide file tree
Showing 24 changed files with 136 additions and 51 deletions.
5 changes: 3 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Translation\Translator as BaseTranslator;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Translation\Exception\InvalidArgumentException;

/**
* Translator.
Expand Down Expand Up @@ -51,7 +52,7 @@ class Translator extends BaseTranslator implements WarmableInterface
* @param array $loaderIds An array of loader Ids
* @param array $options An array of options
*
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array())
{
Expand All @@ -60,7 +61,7 @@ public function __construct(ContainerInterface $container, MessageSelector $sele

// check option names
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
throw new \InvalidArgumentException(sprintf('The Translator does not support the following options: \'%s\'.', implode('\', \'', $diff)));
throw new InvalidArgumentException(sprintf('The Translator does not support the following options: \'%s\'.', implode('\', \'', $diff)));
}

$this->options = array_merge($this->options, $options);
Expand Down
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Translation\Exception\LogicException;

/**
* Base catalogues binary operation class.
Expand Down Expand Up @@ -72,12 +74,12 @@ abstract class AbstractOperation implements OperationInterface
* @param MessageCatalogueInterface $source The source catalogue
* @param MessageCatalogueInterface $target The target catalogue
*
* @throws \LogicException
* @throws LogicException
*/
public function __construct(MessageCatalogueInterface $source, MessageCatalogueInterface $target)
{
if ($source->getLocale() !== $target->getLocale()) {
throw new \LogicException('Operated catalogues must belong to the same locale.');
throw new LogicException('Operated catalogues must belong to the same locale.');
}

$this->source = $source;
Expand Down Expand Up @@ -105,7 +107,7 @@ public function getDomains()
public function getMessages($domain)
{
if (!in_array($domain, $this->getDomains())) {
throw new \InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
}

if (!isset($this->messages[$domain]['all'])) {
Expand All @@ -121,7 +123,7 @@ public function getMessages($domain)
public function getNewMessages($domain)
{
if (!in_array($domain, $this->getDomains())) {
throw new \InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
}

if (!isset($this->messages[$domain]['new'])) {
Expand All @@ -137,7 +139,7 @@ public function getNewMessages($domain)
public function getObsoleteMessages($domain)
{
if (!in_array($domain, $this->getDomains())) {
throw new \InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
}

if (!isset($this->messages[$domain]['obsolete'])) {
Expand Down
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Translation;

use Symfony\Component\Translation\Exception\InvalidArgumentException;

/**
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
Expand All @@ -36,7 +38,7 @@ class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInter
public function __construct(TranslatorInterface $translator)
{
if (!$translator instanceof TranslatorBagInterface) {
throw new \InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', get_class($translator)));
throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', get_class($translator)));
}

$this->translator = $translator;
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/Translation/Dumper/FileDumper.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Translation\Dumper;

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Exception\InvalidArgumentException;

/**
* FileDumper is an implementation of DumperInterface that dump a message catalogue to file(s).
Expand Down Expand Up @@ -64,7 +65,7 @@ public function setBackup($backup)
public function dump(MessageCatalogue $messages, $options = array())
{
if (!array_key_exists('path', $options)) {
throw new \InvalidArgumentException('The file dumper needs a path option.');
throw new InvalidArgumentException('The file dumper needs a path option.');
}

// save a file for each domain
Expand All @@ -79,7 +80,7 @@ public function dump(MessageCatalogue $messages, $options = array())
} else {
$directory = dirname($fullpath);
if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
throw new \RuntimeException(sprintf('Unable to create directory "%s".', $directory));
throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory));
}
}
// save file
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Translation/Dumper/XliffFileDumper.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Translation\Dumper;

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Exception\InvalidArgumentException;

/**
* XliffFileDumper generates xliff files from a message catalogue.
Expand Down Expand Up @@ -43,7 +44,7 @@ public function formatCatalogue(MessageCatalogue $messages, $domain, array $opti
return $this->dumpXliff2($defaultLocale, $messages, $domain, $options);
}

throw new \InvalidArgumentException(sprintf('No support implemented for dumping XLIFF version "%s".', $xliffVersion));
throw new InvalidArgumentException(sprintf('No support implemented for dumping XLIFF version "%s".', $xliffVersion));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Translation/Dumper/YamlFileDumper.php
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Util\ArrayConverter;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Translation\Exception\LogicException;

/**
* YamlFileDumper generates yaml files from a message catalogue.
Expand All @@ -28,7 +29,7 @@ class YamlFileDumper extends FileDumper
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
if (!class_exists('Symfony\Component\Yaml\Yaml')) {
throw new \LogicException('Dumping translations in the YAML format requires the Symfony Yaml component.');
throw new LogicException('Dumping translations in the YAML format requires the Symfony Yaml component.');
}

$data = $messages->all($domain);
Expand Down
@@ -0,0 +1,21 @@
<?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;

/**
* Base InvalidArgumentException for the Translation component.
*
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{
}
21 changes: 21 additions & 0 deletions src/Symfony/Component/Translation/Exception/LogicException.php
@@ -0,0 +1,21 @@
<?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;

/**
* Base LogicException for Translation component.
*
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
class LogicException extends \LogicException implements ExceptionInterface
{
}
21 changes: 21 additions & 0 deletions src/Symfony/Component/Translation/Exception/RuntimeException.php
@@ -0,0 +1,21 @@
<?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;

/**
* Base RuntimeException for the Translation component.
*
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
class RuntimeException extends \RuntimeException implements ExceptionInterface
{
}
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Translation\Extractor;

use Symfony\Component\Translation\Exception\InvalidArgumentException;

/**
* Base class used by classes that extract translation messages from files.
*
Expand Down Expand Up @@ -56,12 +58,12 @@ private function toSplFileInfo($file)
*
* @return bool
*
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
protected function isFile($file)
{
if (!is_file($file)) {
throw new \InvalidArgumentException(sprintf('The "%s" file does not exist.', $file));
throw new InvalidArgumentException(sprintf('The "%s" file does not exist.', $file));
}

return true;
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/Translation/Interval.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Translation;

use Symfony\Component\Translation\Exception\InvalidArgumentException;

/**
* Tests if a given number belongs to a given math interval.
*
Expand Down Expand Up @@ -41,14 +43,14 @@ class Interval
*
* @return bool
*
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public static function test($number, $interval)
{
$interval = trim($interval);

if (!preg_match('/^'.self::getIntervalRegexp().'$/x', $interval, $matches)) {
throw new \InvalidArgumentException(sprintf('"%s" is not a valid interval.', $interval));
throw new InvalidArgumentException(sprintf('"%s" is not a valid interval.', $interval));
}

if ($matches[1]) {
Expand Down
7 changes: 4 additions & 3 deletions src/Symfony/Component/Translation/Loader/XliffFileLoader.php
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Config\Resource\FileResource;

/**
Expand Down Expand Up @@ -202,7 +203,7 @@ private function getSchema($xliffVersion)
$schemaSource = file_get_contents(__DIR__.'/schema/dic/xliff-core/xliff-core-2.0.xsd');
$xmlUri = 'informativeCopiesOf3rdPartySchemas/w3c/xml.xsd';
} else {
throw new \InvalidArgumentException(sprintf('No support implemented for loading XLIFF version "%s".', $xliffVersion));
throw new InvalidArgumentException(sprintf('No support implemented for loading XLIFF version "%s".', $xliffVersion));
}

return $this->fixXmlLocation($schemaSource, $xmlUri);
Expand Down Expand Up @@ -267,7 +268,7 @@ private function getXmlErrors($internalErrors)
*
* @param \DOMDocument $dom
*
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*
* @return string
*/
Expand All @@ -283,7 +284,7 @@ private function getVersionNumber(\DOMDocument $dom)
$namespace = $xliff->attributes->getNamedItem('xmlns');
if ($namespace) {
if (substr_compare('urn:oasis:names:tc:xliff:document:', $namespace->nodeValue, 0, 34) !== 0) {
throw new \InvalidArgumentException(sprintf('Not a valid XLIFF namespace "%s"', $namespace));
throw new InvalidArgumentException(sprintf('Not a valid XLIFF namespace "%s"', $namespace));
}

return substr($namespace, 34);
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Translation/Loader/YamlFileLoader.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Translation\Loader;

use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\LogicException;
use Symfony\Component\Yaml\Parser as YamlParser;
use Symfony\Component\Yaml\Exception\ParseException;

Expand All @@ -31,7 +32,7 @@ protected function loadResource($resource)
{
if (null === $this->yamlParser) {
if (!class_exists('Symfony\Component\Yaml\Parser')) {
throw new \LogicException('Loading translations from the YAML format requires the Symfony Yaml component.');
throw new LogicException('Loading translations from the YAML format requires the Symfony Yaml component.');
}

$this->yamlParser = new YamlParser();
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Translation/LoggingTranslator.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Translation;

use Psr\Log\LoggerInterface;
use Symfony\Component\Translation\Exception\InvalidArgumentException;

/**
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
Expand All @@ -35,7 +36,7 @@ class LoggingTranslator implements TranslatorInterface, TranslatorBagInterface
public function __construct(TranslatorInterface $translator, LoggerInterface $logger)
{
if (!$translator instanceof TranslatorBagInterface) {
throw new \InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', get_class($translator)));
throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', get_class($translator)));
}

$this->translator = $translator;
Expand Down
7 changes: 4 additions & 3 deletions src/Symfony/Component/Translation/MessageCatalogue.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Translation;

use Symfony\Component\Config\Resource\ResourceInterface;
use Symfony\Component\Translation\Exception\LogicException;

/**
* MessageCatalogue.
Expand Down Expand Up @@ -143,7 +144,7 @@ public function add($messages, $domain = 'messages')
public function addCatalogue(MessageCatalogueInterface $catalogue)
{
if ($catalogue->getLocale() !== $this->locale) {
throw new \LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale));
throw new LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale));
}

foreach ($catalogue->all() as $domain => $messages) {
Expand All @@ -169,14 +170,14 @@ public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
$c = $catalogue;
while ($c = $c->getFallbackCatalogue()) {
if ($c->getLocale() === $this->getLocale()) {
throw new \LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
}
}

$c = $this;
do {
if ($c->getLocale() === $catalogue->getLocale()) {
throw new \LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
}
} while ($c = $c->parent);

Expand Down

0 comments on commit 7f1f232

Please sign in to comment.