Passed
Pull Request — master (#81)
by Dante
01:06
created

Ttag   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B extract() 0 41 9
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2023 Atlas Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
16
namespace BEdita\I18n\Filesystem;
17
18
use Cake\Core\App;
19
use Cake\Core\Plugin;
20
use Cake\Utility\Hash;
21
use Cake\View\View;
22
23
class Ttag
24
{
25
    /**
26
     * Extract ttag strings from javascript files
27
     *
28
     * @param string $localePath The locale path
29
     * @param string|null $plugin The plugin name, if any
30
     * @return array
31
     */
32
    public static function extract(string $localePath, ?string $plugin = null): array
33
    {
34
        $extracted = false;
35
        $info = [];
36
37
        // check ttag command exists
38
        $ttag = 'node_modules/ttag-cli/bin/ttag';
39
        if (!file_exists($ttag)) {
40
            $info[] = sprintf('Skip javascript parsing - %s command not found', $ttag);
41
42
            return compact('extracted', 'info');
43
        }
44
        // check template folder exists
45
        $appDir = !empty($plugin) ? Plugin::templatePath($plugin) : Hash::get(App::path(View::NAME_TEMPLATE), 0);
46
        if (!file_exists($appDir)) {
0 ignored issues
show
Bug introduced by
It seems like $appDir can also be of type null; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        if (!file_exists(/** @scrutinizer ignore-type */ $appDir)) {
Loading history...
47
            $info[] = sprintf('Skip javascript parsing - %s folder not found', $appDir);
48
49
            return compact('extracted', 'info');
50
        }
51
        // Path to the resources directory defined in cakephp app config/paths.php
52
        // Do not add RESOURCES path when it's a plugin
53
        if (empty($plugin) && defined('RESOURCES') && file_exists(RESOURCES)) {
0 ignored issues
show
Bug introduced by
The constant BEdita\I18n\Filesystem\RESOURCES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
54
            $appDir = sprintf('%s %s', $appDir, RESOURCES);
55
        }
56
57
        // do extract translation strings from js files using ttag
58
        $info[] = 'Extracting translation string from javascript files using ttag';
59
        $defaultJs = sprintf('%s/default-js.pot', $localePath);
60
        exec(sprintf('%s extract --extractLocation never --o %s --l en %s', $ttag, $defaultJs, $appDir));
61
62
        // merge default-js.pot and <plugin>.pot|default.pot
63
        $potFile = !empty($plugin) && is_string($plugin) ? sprintf('%s.pot', $plugin) : 'default.pot';
64
        $default = sprintf('%s/%s', $localePath, $potFile);
65
        exec(sprintf('msgcat --use-first %s %s -o %s', $default, $defaultJs, $default));
66
67
        // remove default-js.pot
68
        unlink($defaultJs);
69
70
        $extracted = true;
71
72
        return compact('extracted', 'info');
73
    }
74
}
75