LarouteGeneratorCommand::getOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Te7aHoudini\Laroute\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Config\Repository as Config;
7
use Symfony\Component\Console\Input\InputOption;
8
use Te7aHoudini\Laroute\Routes\Collection as Routes;
9
use Te7aHoudini\Laroute\Generators\GeneratorInterface as Generator;
10
11
class LarouteGeneratorCommand extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'laroute:generate';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Generate a laravel routes file';
26
27
    /**
28
     * Config.
29
     *
30
     * @var Config
31
     */
32
    protected $config;
33
34
    /**
35
     * An array of all the registered routes.
36
     *
37
     * @var \Te7aHoudini\Laroute\Routes\Collection
38
     */
39
    protected $routes;
40
41
    /**
42
     * The generator instance.
43
     *
44
     * @var \Te7aHoudini\Laroute\Generators\GeneratorInterface
45
     */
46
    protected $generator;
47
48
    /**
49
     * Create a new command instance.
50
     *
51
     * @param Config $config
52
     * @param Routes $routes
53
     * @param Generator $generator
54
     */
55
    public function __construct(Config $config, Routes $routes, Generator $generator)
56
    {
57
        $this->config = $config;
58
        $this->routes = $routes;
59
        $this->generator = $generator;
60
61
        parent::__construct();
62
    }
63
64
    /**
65
     * Execute the console command.
66
     *
67
     * @return void
68
     */
69
    public function handle()
70
    {
71
        try {
72
            $filePath = $this->generator->compile(
73
                $this->getTemplatePath(),
74
                $this->getTemplateData(),
75
                $this->getFileGenerationPath()
76
            );
77
78
            $this->info("Created: {$filePath}");
79
        } catch (\Exception $e) {
80
            $this->error($e->getMessage());
81
        }
82
    }
83
84
    /**
85
     * Get path to the template file.
86
     *
87
     * @return string
88
     */
89
    protected function getTemplatePath()
90
    {
91
        return $this->config->get('laroute.template');
92
    }
93
94
    /**
95
     * Get the data for the template.
96
     *
97
     * @return array
98
     */
99
    protected function getTemplateData()
100
    {
101
        $namespace = $this->getOptionOrConfig('namespace');
102
        $routes = $this->routes->toJSON();
103
        $absolute = $this->config->get('laroute.absolute', false);
104
        $rootUrl = $this->config->get('app.url', '');
105
        $prefix = $this->config->get('laroute.prefix', '');
106
107
        return compact('namespace', 'routes', 'absolute', 'rootUrl', 'prefix');
108
    }
109
110
    /**
111
     * Get the path where the file will be generated.
112
     *
113
     * @return string
114
     */
115
    protected function getFileGenerationPath()
116
    {
117
        $path = $this->getOptionOrConfig('path');
118
        $filename = $this->getOptionOrConfig('filename');
119
120
        return "{$path}/{$filename}.js";
121
    }
122
123
    /**
124
     * Get an option value either from console input, or the config files.
125
     *
126
     * @param $key
127
     *
128
     * @return array|mixed|string
129
     */
130
    protected function getOptionOrConfig($key)
131
    {
132
        if ($option = $this->option($key)) {
133
            return $option;
134
        }
135
136
        return $this->config->get("laroute.{$key}");
137
    }
138
139
    /**
140
     * Get the console command options.
141
     *
142
     * @return array
143
     */
144
    protected function getOptions()
145
    {
146
        return [
147
            [
148
                'path',
149
                'p',
150
                InputOption::VALUE_OPTIONAL,
151
                sprintf('Path to the javscript assets directory (default: "%s")', $this->config->get('laroute.path')),
152
            ],
153
            [
154
                'filename',
155
                'f',
156
                InputOption::VALUE_OPTIONAL,
157
                sprintf('Filename of the javascript file (default: "%s")', $this->config->get('laroute.filename')),
158
            ],
159
            [
160
                'namespace',
161
                null,
162
                InputOption::VALUE_OPTIONAL, sprintf('Javascript namespace for the functions (think _.js) (default: "%s")', $this->config->get('laroute.namespace')),
163
            ],
164
            [
165
                'prefix',
166
                'pr',
167
                InputOption::VALUE_OPTIONAL, sprintf('Prefix for the generated URLs (default: "%s")', $this->config->get('laroute.prefix')),
168
            ],
169
        ];
170
    }
171
}
172