Completed
Push — laravel-5 ( 2ca47f...0e4947 )
by Andy
04:08
created

src/Console/ExportCommand.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 handle()
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
     * Execute the console command.
56
     * Compatibility with previous Laravel 5.x versions.
57
     *
58
     * @return void
59
     * @throws ConfigException
60
     */
61
    public function fire()
62
    {
63
        $this->handle();
64
    }
65
66
    /**
67
     * Create full file path.
68
     * This method will also generate the directories if they don't exist already.
69
     *
70
     * @var string $filename
71
     *
72
     * @return string $path
73
     */
74
    public function createPath($filename)
75
    {
76
        $dir = Config::get('js-localization.storage_path');
77
        if (!is_dir($dir)) {
78
            mkdir($dir, '0777', true);
79
        }
80
81
        return $dir . $filename;
82
    }
83
84
    /**
85
     * Generate the messages file.
86
     *
87
     * @param string $path
88
     */
89
    public function generateMessagesFile($path)
90
    {
91
        $splitFiles = Config::get('js-localization.split_export_files');
92
        $messages = MessageCachingService::getMessagesJson();
93
94
        if ($splitFiles) {
95
            $this->generateMessageFiles(File::dirname($path), $messages);
96
        }
97
98
        $contents = 'Lang.addMessages(' . $messages . ');';
99
100
        File::put($path, $contents);
101
102
        $this->line("Generated $path");
103
    }
104
105
    /**
106
     * Generate the lang-{locale}.js files
107
     *
108
     * @param string $path Directory to where we will store the files
109
     * @param string $messages JSON string of messages
110
     */
111
    protected function generateMessageFiles(string $path, string $messages)
112
    {
113
        $locales = Config::get('js-localization.locales');
114
        $messages = json_decode($messages, true);
115
116
        foreach ($locales as $locale) {
117
            $fileName = $path . "/lang-{$locale}.js";
118
119
            if (key_exists($locale, $messages)) {
120
                $content = 'Lang.addMessages(' . json_encode([$locale => $messages[$locale]], JSON_PRETTY_PRINT) . ');';
121
            }
122
123
            File::put($fileName, $content);
0 ignored issues
show
The variable $content does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
124
            $this->line("Generated $fileName");
125
        }
126
    }
127
128
    /**
129
     * Generage the config file.
130
     *
131
     * @param string $path
132
     */
133
    public function generateConfigFile($path)
134
    {
135
        $config = ConfigCachingService::getConfigJson();
136
        if ($config === '{}') {
137
            $this->line('No config specified. Config not written to file.');
138
            return;
139
        }
140
141
        $contents = 'Config.addConfig(' . $config . ');';
142
143
        File::put($path, $contents);
144
145
        $this->line("Generated $path");
146
    }
147
}
148