Completed
Push — master ( 250f6a...af675f )
by Sam
07:31
created

Lang   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 94
rs 10
c 0
b 0
f 0
1
<?php namespace Savannabits\JetstreamInertiaGenerator\Generators;
2
3
use Symfony\Component\Console\Input\InputOption;
4
5
class Lang extends FileAppender {
6
7
    /**
8
     * The name and signature of the console command.
9
     *
10
     * @var string
11
     */
12
    protected $name = 'jig:generate:lang';
13
14
    /**
15
     * The console command description.
16
     *
17
     * @var string
18
     */
19
    protected $description = 'Append admin translations into a admin lang file';
20
21
    /**
22
     * Path for view
23
     *
24
     * @var string
25
     */
26
    protected string $view = 'lang';
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
27
28
    /**
29
     * Lang has also export translation
30
     *
31
     * @return mixed
32
     */
33
    protected bool $export = false;
34
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return void
39
     */
40
    public function handle()
41
    {
42
//        //TODO check if exists
43
//        //TODO make global for all generator
44
//        //TODO also with prefix
45
        if(!empty($template = $this->option('template'))) {
46
            $this->view = 'templates.'.$template.'.lang';
47
        }
48
49
        if(empty($locale = $this->option('locale'))) {
50
            $locale = 'en';
51
        }
52
53
        if($this->option('with-export')){
54
            $this->export = true;
55
        }
56
57
        if(!empty($belongsToMany = $this->option('belongs-to-many'))) {
58
            $this->setBelongToManyRelation($belongsToMany);
59
        }
60
61
        // TODO what if a file has been changed? this will append it again (because the content is not present anymore -> we should probably check only for a root key for existence)
62
63
        // TODO name-spaced model names should be probably inserted as a sub-array in a translation file..
64
65
        if ($this->replaceIfNotPresent(resource_path('lang/'.$locale.'/admin.php'),  "// Do not delete me :) I'm used for auto-generation".PHP_EOL,$this->buildClass().PHP_EOL, "<?php".PHP_EOL.PHP_EOL."return [".PHP_EOL."    // Do not delete me :) I'm used for auto-generation".PHP_EOL."];")){
66
            $this->info('Appending translations finished');
67
        }
68
    }
69
70
    protected function buildClass(): string
71
    {
72
        return view('jig::'.$this->view, [
73
            'modelLangFormat' => $this->modelLangFormat,
74
            'modelBaseName' => $this->modelBaseName,
75
            'modelPlural' => $this->modelPlural,
76
            'titleSingular' => $this->titleSingular,
77
            'titlePlural' => $this->titlePlural,
78
            'export' => $this->export,
79
            'containsPublishedAtColumn' => in_array("published_at", array_column($this->readColumnsFromTable($this->tableName)->toArray(), 'name')),
80
81
            'columns' => $this->getVisibleColumns($this->tableName, $this->modelVariableName)->map(function ($column) {
82
                $column['defaultTranslation'] = $this->valueWithoutId($column['name']);
83
                return $column;
84
            }),
85
            'relations' => $this->relations,
86
        ])->render();
87
    }
88
89
    protected function getOptions() {
90
        return [
91
            ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a controller for the given model'],
92
            ['locale', 'c', InputOption::VALUE_OPTIONAL, 'Specify custom locale'],
93
            ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'],
94
            ['belongs-to-many', 'btm', InputOption::VALUE_OPTIONAL, 'Specify belongs to many relations'],
95
            ['with-export', 'e', InputOption::VALUE_NONE, 'Generate an option to Export as Excel'],
96
        ];
97
    }
98
99
}
100