Completed
Push — master ( 8e6191...34d733 )
by Tobias
08:15
created

TranslationLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
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 Translation\Converter\Loader;
13
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\Translation\Loader\LoaderInterface;
16
use Symfony\Component\Translation\MessageCatalogue;
17
use Translation\SymfonyStorage\TranslationLoader as TranslationLoaderInterface;
18
19
class TranslationLoader implements TranslationLoaderInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Translation\SymfonyStorage\TranslationLoader has been deprecated with message: Will be removed in 2.0. Please use Symfony\Component\Translation\Reader\TranslationReaderInterface.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
20
{
21
    /**
22
     * @var LoaderInterface
23
     */
24
    private $loader;
25
26
    /**
27
     * @var string
28
     */
29
    private $format;
30
31
    /**
32
     * @param LoaderInterface $loader
33
     * @param string          $format
34
     */
35 2
    public function __construct(LoaderInterface $loader, $format)
36
    {
37 2
        $this->loader = $loader;
38 2
        $this->format = $format;
39 2
    }
40
41
    /**
42
     * Loads translation messages from a directory to the catalogue.
43
     *
44
     * @param string           $directory the directory to look into
45
     * @param MessageCatalogue $catalogue the catalogue
46
     *
47 2
     * @deprecated Use read instead.
48
     */
49 2
    public function loadMessages($directory, MessageCatalogue $catalogue)
50
    {
51
        return $this->read($directory, $catalogue);
52
    }
53
54 2
    /**
55 2
     * Loads translation messages from a directory to the catalogue.
56 2
     *
57 2
     * @param string           $directory the directory to look into
58 2
     * @param MessageCatalogue $catalogue the catalogue
59 2
     */
60 2
    public function read($directory, MessageCatalogue $catalogue)
61 2
    {
62
        if (!is_dir($directory)) {
63
            return;
64
        }
65
66
        // load any existing translation files
67
        $finder = new Finder();
68
        $extension = $catalogue->getLocale().'.'.$this->format;
69
        $files = $finder->files()->name('*.'.$extension)->in($directory);
70
        foreach ($files as $file) {
71
            $domain = substr($file->getFilename(), 0, -1 * strlen($extension) - 1);
72
            $catalogue->addCatalogue($this->loader->load($file->getPathname(), $catalogue->getLocale(), $domain));
73
        }
74
    }
75
}
76