GenerateJavascriptI18nTask::setModulePath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Bummzack\SsOmnipayUI\Tasks;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Dev\BuildTask;
7
8
/**
9
 * Creates JavaScript files ready for consumption by framework/javascript/i18n.js,
10
 * based on source files in JSON format. This is necessary in order to support
11
 * translations in a format which our collaborative translation service (Transifex)
12
 * supports, while retaining the ability to combine JavaScript files in SilverStripe
13
 * without resorting to JSONP or other means of processing raw JSON files.
14
 */
15
class GenerateJavascriptI18nTask extends BuildTask
16
{
17
18
    private $modulePath;
19
    private $sourceDir = 'client/dist/javascript/lang/src';
20
    private $targetDir = 'client/dist/javascript/lang';
21
22
    public function __construct()
23
    {
24
        parent::__construct();
25
        $this->modulePath = BASE_PATH . '/omnipay-ui';
26
    }
27
28
    public function setModulePath($modulePath)
29
    {
30
        $this->modulePath = $modulePath;
31
    }
32
33
    public function getTemplate()
34
    {
35
        $tmpl = <<<TMPL
36
// This file was generated by GenerateJavaScriptI18nTask from %FILE%.
37
// See https://github.com/silverstripe/silverstripe-buildtools for details
38
if(typeof(ss) == 'undefined' || typeof(ss.i18n) == 'undefined') {
39
	if(typeof(console) != 'undefined') console.error('Class ss.i18n not defined');
40
} else {
41
	ss.i18n.addDictionary('%LOCALE%', %TRANSLATIONS%);
42
}
43
TMPL;
44
        return $tmpl;
45
    }
46
47
    /**
48
     * @param HTTPRequest $request
49
     * @throws \Exception
50
     */
51
    public function run($request)
52
    {
53
        $ds = DIRECTORY_SEPARATOR;
54
        if ($request->getVar('module')) {
55
            $this->setModulePath(BASE_PATH . $ds . $request->getVar('module'));
56
        }
57
58
        if (!is_dir($this->modulePath)) {
59
            throw new \Exception("Invalid module path: $this->modulePath");
60
        }
61
        $iterator = new \GlobIterator(
62
            $this->modulePath . $ds . $this->sourceDir . $ds . '*.json'
63
        );
64
        foreach ($iterator as $item) {
65
            $translations = file_get_contents($item->getPathName());
66
            $locale = preg_replace('/\.json/', '', $item->getFilename());
67
            $targetPath = $this->modulePath . $ds . $this->targetDir . $ds . $locale . '.js';
68
            echo "Generating $targetPath\n";
69
            file_put_contents(
70
                $targetPath,
71
                str_replace(
72
                    array(
73
                        '%TRANSLATIONS%',
74
                        '%FILE%',
75
                        '%LOCALE%'
76
                    ),
77
                    array(
78
                        $translations,
79
                        $this->sourceDir . $ds . $item->getFilename(),
80
                        $locale
81
                    ),
82
                    $this->getTemplate()
83
                )
84
            );
85
        }
86
    }
87
}
88