Completed
Branch feature/pre-split (42159e)
by Anton
05:36
created

TranslationLocator::loadLocale()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 1
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework, SpiralScout LLC.
4
 *
5
 * @package   spiralFramework
6
 * @author    Anton Titov (Wolfy-J)
7
 * @copyright ©2009-2011
8
 */
9
10
namespace Spiral\Translator;
11
12
use Spiral\Core\Component;
13
use Spiral\Debug\Traits\LoggerTrait;
14
use Spiral\Files\FilesInterface;
15
use Spiral\Translator\Configs\TranslatorConfig;
16
use Symfony\Component\Finder\Finder;
17
use Symfony\Component\Finder\SplFileInfo;
18
use Symfony\Component\Translation\Loader\LoaderInterface;
19
use Symfony\Component\Translation\MessageCatalogue;
20
21
/**
22
 * Can load translation data from multiple different formats.
23
 */
24
class TranslationLocator extends Component implements LocatorInterface
25
{
26
    use LoggerTrait;
27
28
    /**
29
     * @var TranslatorConfig
30
     */
31
    private $config = null;
32
33
    /**
34
     * @var FilesInterface
35
     */
36
    protected $files = null;
37
38
    /**
39
     * @param TranslatorConfig $config
40
     * @param FilesInterface   $files
41
     */
42
    public function __construct(TranslatorConfig $config, FilesInterface $files)
43
    {
44
        $this->config = $config;
45
        $this->files = $files;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function hasLocale(string $locale): bool
52
    {
53
        $locale = preg_replace("/[^a-zA-Z_]/", '', mb_strtolower($locale));
54
55
        return $this->files->isDirectory($this->config->localeDirectory($locale));
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getLocales(): array
62
    {
63
        $finder = new Finder();
64
        $finder->in($this->config->localesDirectory())->directories();
65
66
        $locales = [];
67
68
        /**
69
         * @var SplFileInfo $directory
70
         */
71
        foreach ($finder->directories()->getIterator() as $directory) {
72
            $locales[] = $directory->getFilename();
73
        }
74
75
        return $locales;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function loadLocale(string $locale): array
82
    {
83
        $domains = [];
84
85
        $finder = new Finder();
86
        $finder->in($this->config->localeDirectory($locale));
87
88
        /**
89
         * @var SplFileInfo $file
90
         */
91
        foreach ($finder->getIterator() as $file) {
92
93
            $this->logger()->info("Found locale domain file '{file}'", [
94
                'file' => $file->getFilename()
95
            ]);
96
97
            //Per application agreement domain name must present in filename
98
            $domain = strstr($file->getFilename(), '.', true);
99
100
            if ($this->config->hasLoader($file->getExtension())) {
101
                $loader = $this->config->loaderClass($file->getExtension());
102
                $domains[$domain] = $this->loadCatalogue($locale, $domain, $file, new $loader());
103
            } else {
104
                $this->logger()->warning("Unable to load domain file '{file}', no loader found", [
105
                    'file' => $file->getFilename()
106
                ]);
107
            }
108
        }
109
110
        return $domains;
111
    }
112
113
    /**
114
     * Load domain data from file.
115
     *
116
     * @param string          $locale
117
     * @param string          $domain
118
     * @param SplFileInfo     $file
119
     * @param LoaderInterface $loader
120
     *
121
     * @return MessageCatalogue
122
     */
123
    protected function loadCatalogue($locale, $domain, SplFileInfo $file, LoaderInterface $loader)
124
    {
125
        return $loader->load((string)$file, $locale, $domain);
126
    }
127
}