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

Ttag::extract()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 14
c 2
b 1
f 0
nc 16
nop 3
dl 0
loc 25
rs 9.4888
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 array $locales The locales
29
     * @param string $localePath The locale path
30
     * @param string|null $plugin The plugin name, if any
31
     * @return array
32
     */
33
    public static function extract(array $locales, string $localePath, ?string $plugin = null): array
34
    {
35
        $skip = false;
36
        $extracted = false;
37
        $info = [];
38
39
        // check ttag command exists
40
        $ttag = 'node_modules/ttag-cli/bin/ttag';
41
        if (!file_exists($ttag)) {
42
            $info[] = sprintf('Skip javascript parsing - %s command not found', $ttag);
43
            $skip = true;
44
        }
45
46
        // check template folder exists
47
        $appDir = !empty($plugin) ? Plugin::templatePath($plugin) : Hash::get(App::path(View::NAME_TEMPLATE), 0);
48
        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

48
        if (!file_exists(/** @scrutinizer ignore-type */ $appDir)) {
Loading history...
49
            $info[] = sprintf('Skip javascript parsing - %s folder not found', $appDir);
50
            $skip = true;
51
        }
52
53
        if (!$skip) {
54
            $extracted = self::doExtract($ttag, $appDir, $localePath, $locales);
0 ignored issues
show
Bug introduced by
It seems like $appDir can also be of type null; however, parameter $appDir of BEdita\I18n\Filesystem\Ttag::doExtract() 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

54
            $extracted = self::doExtract($ttag, /** @scrutinizer ignore-type */ $appDir, $localePath, $locales);
Loading history...
55
        }
56
57
        return compact('extracted', 'info');
58
    }
59
60
    /**
61
     * Perform ttag extract
62
     *
63
     * @param string $ttag Ttag command
64
     * @param string $appDir Path to the app directory
65
     * @param string $localePath Path to the locale directory
66
     * @param array $locales The locales
67
     * @param string|null $plugin The plugin name, if any
68
     * @return bool
69
     */
70
    public static function doExtract(
71
        string $ttag,
72
        string $appDir,
73
        string $localePath,
74
        array $locales,
75
        ?string $plugin = null
76
    ): bool {
77
        $result = true;
78
        try {
79
            // Path to the resources directory defined in cakephp app config/paths.php
80
            // Do not add RESOURCES path when it's a plugin
81
            if (empty($plugin) && defined('RESOURCES') && file_exists(RESOURCES)) {
82
                $appDir = sprintf('%s %s', $appDir, RESOURCES);
83
            }
84
85
            // do extract translation strings from js files using ttag
86
            $info[] = 'Extracting translation string from javascript files using ttag';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$info was never initialized. Although not strictly required by PHP, it is generally a good practice to add $info = array(); before regardless.
Loading history...
87
88
            $defaultJs = sprintf('%s/default-js.pot', $localePath);
89
            foreach ($locales as $locale) {
90
                $lang = substr($locale, 0, 2);
91
                exec(sprintf('%s extract --extractLocation never --o %s --l %s %s', $ttag, $defaultJs, $lang, $appDir));
92
            }
93
94
            // merge default-js.pot and <plugin>.pot|default.pot
95
            $potFile = !empty($plugin) && is_string($plugin) ? sprintf('%s.pot', $plugin) : 'default.pot';
96
            $default = sprintf('%s/%s', $localePath, $potFile);
97
            exec(sprintf('msgcat --use-first %s %s -o %s', $default, $defaultJs, $default));
98
99
            // remove default-js.pot
100
            unlink($defaultJs);
101
        } catch (\Throwable $e) {
102
            $result = false;
103
        }
104
105
        return $result;
106
    }
107
}
108