ExportCommand::generateMessageFiles()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 12
cp 0
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
namespace JsLocalization\Console;
3
4
use Config;
5
use Illuminate\Console\Command;
6
use File;
7
use JsLocalization\Exceptions\ConfigException;
8
use JsLocalization\Facades\ConfigCachingService;
9
use JsLocalization\Facades\MessageCachingService;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Input\InputArgument;
12
13
class ExportCommand extends Command
14
{
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'js-localization:export';
21
22
    /**
23
     * The console command signature.
24
     *
25
     * @var string
26
     */
27
    protected $signature = 'js-localization:export {--no-cache : Ignores cache completely}';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = "Refresh message cache and export to static files";
35
36
    /**
37
     *  Options defined for Laravel < 5.1
38
     *
39
     * @return array
40
     */
41 3
    protected function getOptions()
42
    {
43
        return [
44 3
            ['no-cache', 'd', InputOption::VALUE_NONE, 'Ignores cache completely'],
45 3
        ];
46
    }
47
48
    /**
49
     * Execute the console command.
50
     *
51
     * @return void
52
     * @throws ConfigException
53
     */
54
    public function handle()
55
    {
56
        $noCache = (bool)$this->option('no-cache');
57
        if ($noCache === true) $this->line('Exporting messages and config...');
58
        else $this->line('Refreshing and exporting the message and config cache...');
59
60
        $locales = Config::get('js-localization.locales');
61
62
        if(!is_array($locales)) {
63
          throw new ConfigException('Please set the "locales" config! See https://github.com/andywer/laravel-js-localization#configuration');
64
        }
65
66
        if ($noCache === false) MessageCachingService::refreshCache();
67
        $messagesFilePath = $this->createPath('messages.js');
68
        $this->generateMessagesFile($messagesFilePath, $noCache);
69
70
        if ($noCache === false) ConfigCachingService::refreshCache();
71
        $configFilePath = $this->createPath('config.js');
72
        $this->generateConfigFile($configFilePath, $noCache);
73
    }
74
    
75
    /**
76
     * Execute the console command.
77
     * Compatibility with previous Laravel 5.x versions.
78
     *
79
     * @return void
80
     * @throws ConfigException
81
     */
82
    public function fire()
83
    {
84
        $this->handle();
85
    }
86
87
    /**
88
     * Create full file path.
89
     * This method will also generate the directories if they don't exist already.
90
     *
91
     * @var string $filename
92
     *
93
     * @return string $path
94
     */
95
    public function createPath($filename)
96
    {
97
        $dir = Config::get('js-localization.storage_path');
98
        if (!is_dir($dir)) {
99
            mkdir($dir, 0777, true);
100
        }
101
102
        return $dir . $filename;
103
    }
104
105
    /**
106
     * Generate the messages file.
107
     *
108
     * @param string $path
109
     * @param bool $noCache
110
     */
111
    public function generateMessagesFile($path, $noCache = false)
112
    {
113
        $splitFiles = Config::get('js-localization.split_export_files');
114
        $messages = MessageCachingService::getMessagesJson($noCache);
115
116
        if ($splitFiles) {
117
            $this->generateMessageFiles(File::dirname($path), $messages);
118
        }
119
120
        $contents = 'Lang.addMessages(' . $messages . ');';
121
122
        File::put($path, $contents);
123
124
        $this->line("Generated $path");
125
    }
126
127
    /**
128
     * Generate the lang-{locale}.js files
129
     *
130
     * @param string $path Directory to where we will store the files
131
     * @param string $messages JSON string of messages
132
     */
133
    protected function generateMessageFiles(string $path, string $messages)
134
    {
135
        $locales = Config::get('js-localization.locales');
136
        $messages = json_decode($messages, true);
137
138
        foreach ($locales as $locale) {
139
            $fileName = $path . "/lang-{$locale}.js";
140
141
            if (key_exists($locale, $messages)) {
142
                $content = 'Lang.addMessages(' . json_encode([$locale => $messages[$locale]], JSON_PRETTY_PRINT) . ');';
143
144
                File::put($fileName, $content);
145
                $this->line("Generated $fileName");
146
            }
147
        }
148
    }
149
150
    /**
151
     * Generate the config file.
152
     *
153
     * @param string $path
154
     * @param bool $noCache
155
     */
156
    public function generateConfigFile($path, $noCache = false)
157
    {
158
        $config = ConfigCachingService::getConfigJson($noCache);
159
        if ($config === '{}') {
160
            $this->line('No config specified. Config not written to file.');
161
            return;
162
        }
163
164
        $contents = 'Config.addConfig(' . $config . ');';
165
166
        File::put($path, $contents);
167
168
        $this->line("Generated $path");
169
    }
170
}
171