TranslationLoader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\PlatformAdapter\Flysystem;
13
14
use League\Flysystem\Filesystem;
15
use Symfony\Component\Translation\MessageCatalogue;
16
use Symfony\Component\Translation\Loader\LoaderInterface;
17
18
/**
19
 * TranslationLoader loads translation messages from translation files.
20
 *
21
 * @author Tobias Nyholm <[email protected]>
22
 */
23
final class TranslationLoader implements \Translation\SymfonyStorage\TranslationLoader
24
{
25
    /**
26
     * Loaders used for import.
27
     *
28
     * @var LoaderInterface[]
29
     */
30
    private $loaders = [];
31
32
    /**
33
     * @var Filesystem
34
     */
35
    private $filesystem;
36
37
    /**
38
     * @param Filesystem $filesystem
39
     */
40 1
    public function __construct(Filesystem $filesystem)
41
    {
42 1
        $this->filesystem = $filesystem;
43 1
    }
44
45
    /**
46
     * Adds a loader to the translation extractor.
47
     *
48
     * @param string          $format The format of the loader
49
     * @param LoaderInterface $loader
50
     */
51 1
    public function addLoader($format, LoaderInterface $loader)
52
    {
53 1
        $this->loaders[$format] = $loader;
54 1
    }
55
56
    /**
57
     * Loads translation messages from a directory to the catalogue.
58
     *
59
     * @param string           $directory the directory to look into
60
     * @param MessageCatalogue $catalogue the catalogue
61
     */
62
    public function loadMessages($directory, MessageCatalogue $catalogue)
63
    {
64
        if (!$this->filesystem->has($directory)) {
65
            return;
66
        }
67
68
        foreach ($this->loaders as $format => $loader) {
69
            // load any existing translation files
70
            $extension = $catalogue->getLocale().'.'.$format;
71
            $files = $this->filesystem->listFiles($directory, true);
72
            foreach ($files as $file) {
73
                $path = $file['path'];
74
                $filename = $this->basename($path);
75
                $domain = substr($filename, 0, -1 * strlen($extension) - 1);
76
                $catalogue->addCatalogue($loader->load($path, $catalogue->getLocale(), $domain));
77
            }
78
        }
79
    }
80
81
    /**
82
     * Gets the filename from a given path.
83
     *
84
     * PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character.
85
     *
86
     * @author Drupal 8.2
87
     *
88
     * @param string $path
89
     *
90
     * @return string
91
     */
92
    private function basename($path)
93
    {
94
        $separators = '/';
95
        if (DIRECTORY_SEPARATOR != '/') {
96
            // For Windows OS add special separator.
97
            $separators .= DIRECTORY_SEPARATOR;
98
        }
99
100
        // Remove right-most slashes when $path points to directory.
101
        $path = rtrim($path, $separators);
102
103
        // Returns the trailing part of the $path starting after one of the directory separators.
104
        $filename = preg_match('@[^'.preg_quote($separators, '@').']+$@', $path, $matches) ? $matches[0] : '';
105
106
        return $filename;
107
    }
108
}
109