Passed
Push — master ( 1ebbf4...fce103 )
by Thomas
13:47
created

ConfigurableI18nTextCollectorTask::run()   B

Complexity

Conditions 8
Paths 20

Size

Total Lines 46
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 36
c 2
b 0
f 0
nc 20
nop 1
dl 0
loc 46
rs 8.0995
1
<?php
2
3
namespace LeKoala\Base\i18n;
4
5
use SilverStripe\i18n\i18n;
6
use SilverStripe\Dev\BuildTask;
7
use LeKoala\Base\i18n\TextCollector;
0 ignored issues
show
Bug introduced by
The type LeKoala\Base\i18n\TextCollector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use LeKoala\Multilingual\LangHelper;
9
use SilverStripe\Control\Director;
10
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
11
12
/**
13
 * A better task for collecting text
14
 *
15
 * Use our TextCollector under the hood that actually works...
16
 */
17
class ConfigurableI18nTextCollectorTask extends BuildTask
18
{
19
    use BuildTaskTools;
20
21
    /**
22
     * @var string
23
     */
24
    private static $segment = 'ConfigurableI18nTextCollectorTask';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
25
26
    /**
27
     * @var string
28
     */
29
    protected $title = "Configurable i18n Textcollector Task";
30
31
    /**
32
     * @var string
33
     */
34
    protected $description = "
35
		Traverses through files in order to collect the 'entity master tables'
36
		stored in each module. Provides the ability to choose modules and clear/merge translations.
37
	";
38
39
    /**
40
     * This is the main method to build the master string tables with the original strings.
41
     * It will search for existent modules that use the i18n feature, parse the _t() calls
42
     * and write the resultant files in the lang folder of each module.
43
     *
44
     * @uses DataObject->collectI18nStatics()
45
     * @param \SilverStripe\Control\HTTPRequest $request
46
     * @return void
47
     */
48
    public function run($request)
49
    {
50
        HTTPCacheControlMiddleware::singleton()->disableCache(true);
51
52
        $this->request = $request;
53
        $this->increaseTimeLimitTo();
54
55
        $modules = $this->getModules();
56
57
        $this->addOption("locale", "Locale to use", LangHelper::get_lang());
58
        $this->addOption("merge", "Merge with previous translations", true);
59
        $this->addOption("auto_translate", "Translate new strings using google api (1s per translation)", false);
60
        $this->addOption("clear_unused", "Remove keys that are not used anymore", false);
61
        $this->addOption("debug", "Show debug messages and prevent write", false);
62
        $this->addOption("module", "Module", 'default', $modules);
63
64
        $options = $this->askOptions();
65
66
        $locale = $options['locale'];
67
        $merge = $options['merge'];
68
        $module = $options['module'];
69
        $clearUnused = $options['clear_unused'];
70
        $debug = $options['debug'];
71
        $auto_translate = $options['auto_translate'];
72
73
        $themes = Director::baseFolder() . '/themes';
74
        $folders = glob($themes . '/*');
75
        $toCollect = ['app'];
76
        foreach ($folders as $f) {
77
            $toCollect[] = 'themes:' . basename($f);
78
        }
79
        if ($module && $module != 'default') {
80
            $toCollect = [$module];
81
        }
82
        if ($locale) {
83
            foreach ($toCollect as $module) {
84
                $this->message("Proceeding with locale $locale for module $module");
85
                $collector = MultilingualTextCollector::create($locale);
86
                $collector->setMergeWithExisting($merge);
87
                $collector->setClearUnused($clearUnused);
88
                $collector->setDebug($debug);
89
                $collector->setAutoTranslate($auto_translate);
90
                $result = $collector->run([$module], $merge);
91
                if ($result) {
92
                    foreach ($result as $module => $entities) {
93
                        $this->message("Collected " . count($entities) . " messages for module $module");
94
                    }
95
                }
96
            }
97
        }
98
    }
99
}
100