Completed
Push — master ( 2d7ea1...6bf93f )
by Craig
08:35
created

ZikulaJsFileExtractor::visitFile()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 5
nop 2
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
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 Zikula\Bundle\CoreBundle\Translation;
13
14
use JMS\TranslationBundle\Model\FileSource;
15
use JMS\TranslationBundle\Model\Message;
16
use JMS\TranslationBundle\Model\MessageCatalogue;
17
use JMS\TranslationBundle\Translation\Extractor\FileVisitorInterface;
18
19
class ZikulaJsFileExtractor implements FileVisitorInterface
20
{
21
    const JAVASCRIPT_DOMAIN = 'zikula_javascript'; // @todo figure out way to compute the bundle's translation domain?
22
    const SINGULAR_CAPTURE_REGEX = '\s?[\'"]([^"\'),]+)[\'"]\s?';
23
    const PLURAL_CAPTURE_REGEX = '\s?[\'"]([^"\'),]+)[\'"]\s?,\s?[\'"]([^"\'),]+)[\'"]';
24
    const REGEX_DELIMITER = '/';
25
26
    private $singularFunctions = [
27
        'trans', // should be replaced by vendor eventually
28
        'transChoice', // should be replaced by vendor eventually
29
        '__',
30
        '__f',
31
    ];
32
    private $pluralFunctions = [
33
        '_n',
34
        '_fn'
35
    ];
36
37
    public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue)
38
    {
39
        if ('.js' !== substr($file, -3)) {
40
            return;
41
        }
42
43
        // singular type
44
        $argumentsRegex = self::REGEX_DELIMITER
45
            .'\.(?:' . implode('|', $this->singularFunctions) . ')\('
46
            .self::SINGULAR_CAPTURE_REGEX
47
            .self::REGEX_DELIMITER;
48
        preg_match_all($argumentsRegex, file_get_contents($file), $matches);
49
        foreach ($matches[1] as $string) {
50
            $message = new Message($string, self::JAVASCRIPT_DOMAIN);
51
            $message->addSource(new FileSource((string) $file));
52
            $catalogue->add($message);
53
        }
54
        // plural type
55
        $argumentsRegex = self::REGEX_DELIMITER
56
            .'\.(?:' . implode('|', $this->pluralFunctions) . ')\('
57
            .self::PLURAL_CAPTURE_REGEX
58
            .self::REGEX_DELIMITER;
59
        preg_match_all($argumentsRegex, file_get_contents($file), $matches);
60
        foreach ($matches[1] as $key => $singluar) {
61
            $fullString = $singluar . '|' . $matches[2][$key];
62
            $message = new Message($fullString, self::JAVASCRIPT_DOMAIN);
63
            $message->addSource(new FileSource((string) $file));
64
            $catalogue->add($message);
65
        }
66
    }
67
68
    public function visitPhpFile(\SplFileInfo $file, MessageCatalogue $catalogue, array $ast)
69
    {
70
    }
71
72
    public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \Twig_Node $node)
73
    {
74
    }
75
}
76