Completed
Pull Request — laravel-5 (#35)
by
unknown
03:05
created

ExportCommand::fire()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 0
crap 6
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 description.
24
     *
25
     * @var string
26
     */
27
    protected $description = "Refresh message cache and export to static files";
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @return void
33
     * @throws ConfigException
34
     */
35
    public function fire()
36
    {
37
        $this->line('Refreshing and exporting the message cache...');
38
39
        $locales = Config::get('js-localization.locales');
40
41
        if(!is_array($locales)) {
42
          throw new ConfigException('Please set the "locales" config! See https://github.com/andywer/laravel-js-localization#configuration');
43
        }
44
45
        MessageCachingService::refreshCache();
46
        $messagesFilePath = $this->createPath('messages.js');
47
        $this->generateMessagesFile($messagesFilePath);
48
49
        ConfigCachingService::refreshCache();
50
        $configFilePath = $this->createPath('config.js');
51
        $this->generateConfigFile($configFilePath);
52
    }
53
54
    /**
55
     * Create full file path.
56
     * This method will also generate the directories if they don't exist already.
57
     *
58
     * @var string $filename
59
     *
60
     * @return string $path
61
     */
62
    public function createPath($filename)
63
    {
64
        $dir = Config::get('js-localization.storage_path');
65
        if (!is_dir($dir)) {
66
            mkdir($dir, '0777', true);
67
        }
68
69
        return $dir . $filename;
70
    }
71
72
    /**
73
     * Generate the messages file.
74
     *
75
     * @param string $path
76
     */
77
    public function generateMessagesFile($path)
78
    {
79
        $messages = MessageCachingService::getMessagesJson();
80
81
        $contents  = 'Lang.addMessages(' . $messages . ');';
82
83
        File::put($path, $contents);
84
85
        $this->line("Generated $path");
86
    }
87
88
    /**
89
     * Generage the config file.
90
     *
91
     * @param string $path
92
     */
93
    public function generateConfigFile($path)
94
    {
95
        $config = ConfigCachingService::getConfigJson();
96
        if ($config === '{}') {
97
            $this->line('No config specified. Config not written to file.');
98
            return;
99
        }
100
101
        $contents = 'Config.addConfig(' . $config . ');';
102
103
        File::put($path, $contents);
104
105
        $this->line("Generated $path");
106
    }
107
}
108