Skip to content

Commit 3694e5e

Browse files
committed
[Translation] added FileLoader.
1 parent 222701f commit 3694e5e

File tree

8 files changed

+83
-182
lines changed

8 files changed

+83
-182
lines changed

src/Symfony/Component/Translation/Loader/CsvFileLoader.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
namespace Symfony\Component\Translation\Loader;
1313

14-
use Symfony\Component\Translation\Exception\InvalidResourceException;
1514
use Symfony\Component\Translation\Exception\NotFoundResourceException;
16-
use Symfony\Component\Config\Resource\FileResource;
1715

1816
/**
1917
* CsvFileLoader loads translations from CSV files.
@@ -22,27 +20,17 @@
2220
*
2321
* @api
2422
*/
25-
class CsvFileLoader extends ArrayLoader
23+
class CsvFileLoader extends FileLoader
2624
{
2725
private $delimiter = ';';
2826
private $enclosure = '"';
2927
private $escape = '\\';
3028

3129
/**
3230
* {@inheritdoc}
33-
*
34-
* @api
3531
*/
36-
public function load($resource, $locale, $domain = 'messages')
32+
protected function loadResource($resource)
3733
{
38-
if (!stream_is_local($resource)) {
39-
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
40-
}
41-
42-
if (!file_exists($resource)) {
43-
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
44-
}
45-
4634
$messages = array();
4735

4836
try {
@@ -70,10 +58,7 @@ public function load($resource, $locale, $domain = 'messages')
7058
}
7159
}
7260

73-
$catalogue = parent::load($messages, $locale, $domain);
74-
$catalogue->addResource(new FileResource($resource));
75-
76-
return $catalogue;
61+
return $messages;
7762
}
7863

7964
/**
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Loader;
13+
14+
use Symfony\Component\Translation\Exception\InvalidResourceException;
15+
use Symfony\Component\Translation\Exception\NotFoundResourceException;
16+
use Symfony\Component\Config\Resource\FileResource;
17+
18+
/**
19+
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
20+
*/
21+
abstract class FileLoader extends ArrayLoader
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function load($resource, $locale, $domain = 'messages')
27+
{
28+
if (!stream_is_local($resource)) {
29+
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
30+
}
31+
32+
if (!file_exists($resource)) {
33+
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
34+
}
35+
36+
$messages = $this->loadResource($resource);
37+
38+
// empty resource
39+
if (null === $messages) {
40+
$messages = array();
41+
}
42+
43+
// not an array
44+
if (!is_array($messages)) {
45+
throw new InvalidResourceException(sprintf('Unable to load file "%s".', $resource));
46+
}
47+
48+
$catalogue = parent::load($messages, $locale, $domain);
49+
$catalogue->addResource(new FileResource($resource));
50+
51+
return $catalogue;
52+
}
53+
54+
/*
55+
* @param string $resource
56+
*
57+
* @return array
58+
*
59+
* @throws InvalidResourceException If stream content has an invalid format.
60+
*/
61+
abstract protected function loadResource($resource);
62+
}

src/Symfony/Component/Translation/Loader/IniFileLoader.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,18 @@
1111

1212
namespace Symfony\Component\Translation\Loader;
1313

14-
use Symfony\Component\Translation\Exception\InvalidResourceException;
15-
use Symfony\Component\Translation\Exception\NotFoundResourceException;
16-
use Symfony\Component\Config\Resource\FileResource;
17-
1814
/**
1915
* IniFileLoader loads translations from an ini file.
2016
*
2117
* @author stealth35
2218
*/
23-
class IniFileLoader extends ArrayLoader
19+
class IniFileLoader extends FileLoader
2420
{
2521
/**
2622
* {@inheritdoc}
2723
*/
28-
public function load($resource, $locale, $domain = 'messages')
24+
protected function loadResource($resource)
2925
{
30-
if (!stream_is_local($resource)) {
31-
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
32-
}
33-
34-
if (!file_exists($resource)) {
35-
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
36-
}
37-
38-
$messages = parse_ini_file($resource, true);
39-
40-
$catalogue = parent::load($messages, $locale, $domain);
41-
$catalogue->addResource(new FileResource($resource));
42-
43-
return $catalogue;
26+
return parse_ini_file($resource, true);
4427
}
4528
}

src/Symfony/Component/Translation/Loader/JsonFileLoader.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,26 @@
1212
namespace Symfony\Component\Translation\Loader;
1313

1414
use Symfony\Component\Translation\Exception\InvalidResourceException;
15-
use Symfony\Component\Translation\Exception\NotFoundResourceException;
16-
use Symfony\Component\Config\Resource\FileResource;
1715

1816
/**
1917
* JsonFileLoader loads translations from an json file.
2018
*
2119
* @author singles
2220
*/
23-
class JsonFileLoader extends ArrayLoader implements LoaderInterface
21+
class JsonFileLoader extends FileLoader
2422
{
2523
/**
2624
* {@inheritdoc}
2725
*/
28-
public function load($resource, $locale, $domain = 'messages')
26+
protected function loadResource($resource)
2927
{
30-
if (!stream_is_local($resource)) {
31-
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
32-
}
33-
34-
if (!file_exists($resource)) {
35-
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
36-
}
37-
3828
$messages = json_decode(file_get_contents($resource), true);
3929

4030
if (0 < $errorCode = json_last_error()) {
4131
throw new InvalidResourceException(sprintf('Error parsing JSON - %s', $this->getJSONErrorMessage($errorCode)));
4232
}
4333

44-
if (null === $messages) {
45-
$messages = array();
46-
}
47-
48-
$catalogue = parent::load($messages, $locale, $domain);
49-
$catalogue->addResource(new FileResource($resource));
50-
51-
return $catalogue;
34+
return $messages;
5235
}
5336

5437
/**

src/Symfony/Component/Translation/Loader/MoFileLoader.php

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212
namespace Symfony\Component\Translation\Loader;
1313

1414
use Symfony\Component\Translation\Exception\InvalidResourceException;
15-
use Symfony\Component\Translation\Exception\NotFoundResourceException;
16-
use Symfony\Component\Config\Resource\FileResource;
1715

1816
/**
1917
* @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
2018
*/
21-
class MoFileLoader extends ArrayLoader
19+
class MoFileLoader extends FileLoader
2220
{
2321
/**
2422
* Magic used for validating the format of a MO file as well as
@@ -43,45 +41,13 @@ class MoFileLoader extends ArrayLoader
4341
*/
4442
const MO_HEADER_SIZE = 28;
4543

46-
public function load($resource, $locale, $domain = 'messages')
47-
{
48-
if (!stream_is_local($resource)) {
49-
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
50-
}
51-
52-
if (!file_exists($resource)) {
53-
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
54-
}
55-
56-
$messages = $this->parse($resource);
57-
58-
// empty file
59-
if (null === $messages) {
60-
$messages = array();
61-
}
62-
63-
// not an array
64-
if (!is_array($messages)) {
65-
throw new InvalidResourceException(sprintf('The file "%s" must contain a valid mo file.', $resource));
66-
}
67-
68-
$catalogue = parent::load($messages, $locale, $domain);
69-
$catalogue->addResource(new FileResource($resource));
70-
71-
return $catalogue;
72-
}
73-
7444
/**
7545
* Parses machine object (MO) format, independent of the machine's endian it
7646
* was created on. Both 32bit and 64bit systems are supported.
7747
*
78-
* @param resource $resource
79-
*
80-
* @return array
81-
*
82-
* @throws InvalidResourceException If stream content has an invalid format.
48+
* {@inheritdoc}
8349
*/
84-
private function parse($resource)
50+
protected function loadResource($resource)
8551
{
8652
$stream = fopen($resource, 'r');
8753

src/Symfony/Component/Translation/Loader/PhpFileLoader.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,20 @@
1111

1212
namespace Symfony\Component\Translation\Loader;
1313

14-
use Symfony\Component\Translation\Exception\InvalidResourceException;
15-
use Symfony\Component\Translation\Exception\NotFoundResourceException;
16-
use Symfony\Component\Config\Resource\FileResource;
17-
1814
/**
1915
* PhpFileLoader loads translations from PHP files returning an array of translations.
2016
*
2117
* @author Fabien Potencier <fabien@symfony.com>
2218
*
2319
* @api
2420
*/
25-
class PhpFileLoader extends ArrayLoader
21+
class PhpFileLoader extends FileLoader
2622
{
2723
/**
2824
* {@inheritdoc}
29-
*
30-
* @api
3125
*/
32-
public function load($resource, $locale, $domain = 'messages')
26+
protected function loadResource($resource)
3327
{
34-
if (!stream_is_local($resource)) {
35-
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
36-
}
37-
38-
if (!file_exists($resource)) {
39-
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
40-
}
41-
42-
$messages = require $resource;
43-
44-
$catalogue = parent::load($messages, $locale, $domain);
45-
$catalogue->addResource(new FileResource($resource));
46-
47-
return $catalogue;
28+
return require $resource;
4829
}
4930
}

src/Symfony/Component/Translation/Loader/PoFileLoader.php

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,12 @@
1111

1212
namespace Symfony\Component\Translation\Loader;
1313

14-
use Symfony\Component\Translation\Exception\InvalidResourceException;
15-
use Symfony\Component\Translation\Exception\NotFoundResourceException;
16-
use Symfony\Component\Config\Resource\FileResource;
17-
1814
/**
1915
* @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
2016
* @copyright Copyright (c) 2012, Clemens Tolboom
2117
*/
22-
class PoFileLoader extends ArrayLoader
18+
class PoFileLoader extends FileLoader
2319
{
24-
public function load($resource, $locale, $domain = 'messages')
25-
{
26-
if (!stream_is_local($resource)) {
27-
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
28-
}
29-
30-
if (!file_exists($resource)) {
31-
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
32-
}
33-
34-
$messages = $this->parse($resource);
35-
36-
// empty file
37-
if (null === $messages) {
38-
$messages = array();
39-
}
40-
41-
// not an array
42-
if (!is_array($messages)) {
43-
throw new InvalidResourceException(sprintf('The file "%s" must contain a valid po file.', $resource));
44-
}
45-
46-
$catalogue = parent::load($messages, $locale, $domain);
47-
$catalogue->addResource(new FileResource($resource));
48-
49-
return $catalogue;
50-
}
51-
5220
/**
5321
* Parses portable object (PO) format.
5422
*
@@ -90,11 +58,9 @@ public function load($resource, $locale, $domain = 'messages')
9058
*
9159
* Items with an empty id are ignored.
9260
*
93-
* @param resource $resource
94-
*
95-
* @return array
61+
* {@inheritdoc}
9662
*/
97-
private function parse($resource)
63+
protected function loadResource($resource)
9864
{
9965
$stream = fopen($resource, 'r');
10066

0 commit comments

Comments
 (0)
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