Ttag   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 29
c 1
b 0
f 0
dl 0
loc 84
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B doExtract() 0 32 8
A extract() 0 24 6
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
use Throwable;
23
24
/**
25
 * Ttag utility class.
26
 *
27
 * Extract ttag strings from javascript files.
28
 * Requires ttag-cli to be installed.
29
 */
30
class Ttag
31
{
32
    /**
33
     * Extract ttag strings from javascript files.
34
     * Returns an array with two keys:
35
     *
36
     * - extracted: true if extraction was successful, false otherwise
37
     * - info: an array of strings with info messages
38
     *
39
     * @param array $locales The locales
40
     * @param string $localePath The locale path
41
     * @param string|null $plugin The plugin name, if any
42
     * @return array
43
     */
44
    public static function extract(array $locales, string $localePath, ?string $plugin = null): array
45
    {
46
        $skip = false;
47
        $info = [];
48
49
        // check ttag command exists
50
        $ttag = 'node_modules/ttag-cli/bin/ttag';
51
        if (!file_exists($ttag)) {
52
            $info[] = sprintf('Skip javascript parsing - %s command not found', $ttag);
53
            $skip = true;
54
        }
55
56
        // check template folder exists
57
        $appDir = !empty($plugin) ? Plugin::templatePath($plugin) : Hash::get(App::path(View::NAME_TEMPLATE), 0);
58
        if (!file_exists($appDir)) {
59
            $info[] = sprintf('Skip javascript parsing - %s folder not found', $appDir);
60
            $skip = true;
61
        }
62
63
        // do extract translation strings from js files using ttag
64
        $info[] = 'Extracting translation string from javascript files using ttag' . ($skip ? ' (skipped)' : '');
65
        $extracted = $skip ? false : self::doExtract($ttag, (string)$appDir, $localePath, $locales, $plugin);
66
67
        return compact('extracted', 'info');
68
    }
69
70
    /**
71
     * Perform ttag extract.
72
     * Returns true if extraction was successful, false otherwise.
73
     *
74
     * @param string $ttag Ttag command
75
     * @param string $appDir Path to the app directory
76
     * @param string $localePath Path to the locale directory
77
     * @param array $locales The locales
78
     * @param string|null $plugin The plugin name, if any
79
     * @return bool
80
     * @codeCoverageIgnore
81
     */
82
    public static function doExtract(
83
        string $ttag,
84
        string $appDir,
85
        string $localePath,
86
        array $locales,
87
        ?string $plugin = null
88
    ): bool {
89
        $result = true;
90
        try {
91
            // Path to the resources directory defined in cakephp app config/paths.php
92
            // Do not add RESOURCES path when it's a plugin
93
            $useResources = 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...
94
            $appDir = $useResources ? sprintf('%s %s', $appDir, RESOURCES) : $appDir;// @phpstan-ignore-line
95
96
            $defaultJs = sprintf('%s/default-js.pot', $localePath);
97
            foreach ($locales as $locale) {
98
                $lang = substr($locale, 0, 2);
99
                exec(sprintf('%s extract --extractLocation never --o %s --l %s %s', $ttag, $defaultJs, $lang, $appDir));
100
            }
101
102
            // merge default-js.pot and <plugin>.pot|default.pot
103
            $potFile = !empty($plugin) && is_string($plugin) ? sprintf('%s.pot', $plugin) : 'default.pot';
104
            $default = sprintf('%s/%s', $localePath, $potFile);
105
            exec(sprintf('msgcat --use-first %s %s -o %s', $default, $defaultJs, $default));
106
107
            // remove default-js.pot
108
            unlink($defaultJs);
109
        } catch (Throwable $e) {
110
            $result = false;
111
        }
112
113
        return $result;
114
    }
115
}
116