JsTranslationExtractor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Util;
4
5
use Symfony\Component\Translation\Extractor\ExtractorInterface;
6
use Symfony\Component\Translation\MessageCatalogue;
7
8
class JsTranslationExtractor implements ExtractorInterface
9
{
10
    public function __construct(string $directory)
11
    {
12
        $this->directory = $directory;
0 ignored issues
show
Bug Best Practice introduced by
The property directory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
13
    }
14
15
    private string $prefix = '';
16
17
    public function setPrefix(string $prefix): void
18
    {
19
        $this->prefix = $prefix ?: '';
20
    }
21
22
    public function extract($directory, MessageCatalogue $catalogue): void
23
    {
24
        if (!is_dir($this->directory)) {
25
            echo "Invalid directory: $this->directory" . PHP_EOL;
26
            return;
27
        }
28
        $iterator = new \RecursiveIteratorIterator(
29
            new \RecursiveDirectoryIterator($this->directory)
30
        );
31
        foreach ($iterator as $file) {
32
            if ($file->getExtension() !== 'js') {
33
                continue;
34
            }
35
            $content = file_get_contents($file->getPathname());
36
            preg_match_all('/\$t\([\'"]([^\'"]+)[\'"]\)/', $content, $matches);
37
            foreach ($matches[1] as $key) {
38
                $catalogue->set($this->prefix . $key, $key);
39
            }
40
        }
41
    }
42
}
43